/*jslint         browser : true, continue : true,
  devel  : true, indent  : 2,    maxerr   : 50,
  newcap : true, nomen   : true, plusplus : true,
  regexp : true, sloppy  : true, vars     : false,
  white  : true
*/

/*global $, app */

app.license_request_edit = (function () {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var
        stateMap = {},
        onSend, onCancel, onRegenerate, configModule, initModule;
    //----------------- END MODULE SCOPE VARIABLES ---------------

    //------------------- BEGIN UTILITY METHODS ------------------
    //-------------------- END UTILITY METHODS -------------------

    //------------------- BEGIN EVENT HANDLERS -------------------

    ////////////////////
    //
    onSend = function (event) {

        event.preventDefault();
        $('#app-error-message').text('');
        //get form data
        var formData = $("form").serializeArray({
            checkboxesAsBools: true
        });

        var submitData = app.util_b.objectifyFormDataArray(formData);

        app.api.license_email_response(submitData, function (res) {
            if (res.error) {
                $('#app-error-message').text(res.msg);
            } else {
                //navigate back to license_requests
                //alert("key has been sent!");
                window.location.href="#!/license_requests/";

                //$('#key').val(res);
                return false;
            }
        });

        return false; //prevent default
    };


    //-------------------- END EVENT HANDLERS --------------------

    //------------------- BEGIN PUBLIC METHODS -------------------
    //CONFIGMODULE
    //
    configModule = function (context) {
        stateMap.context = context.context;
        if (stateMap.context.params.id) {
            stateMap.id = stateMap.context.params.id;
        }
    };

    //INITMODULE
    //
    initModule = function ($container) {
        if (typeof $container === 'undefined') {
            $container = $('#app-shell-main-content');
        }

        $container.html(Handlebars.templates['app.license_request_edit']({}));
        var title = "Request";


        //Append key hidden values for submit and processing by server
        //This is set from the original click to open this form
        $('<input />').attr('type', 'hidden')
            .attr('name', "request_email_uid")
            .attr('value', stateMap.id)
            .appendTo('#frm');

        //These are empty but will be filled in when the server responds with the "record"
        //They are for re-submission back to the server to save a step of refetching the original info
        //and recalculating stuff that was already done

        $('<input />').attr('type', 'hidden')
            .attr('name', "requestReplyToAddress")
            .attr('value', '')
            .appendTo('#frm');

        $('<input />').attr('type', 'hidden')
            .attr('name', "requestFromReplySubject")
            .attr('value', '')
            .appendTo('#frm');

        $('<input />').attr('type', 'hidden')
            .attr('name', "uid")
            .attr('value', '')
            .appendTo('#frm');

        $('<input />').attr('type', 'hidden')
            .attr('name', "requestKeyIsLite")
            .attr('value', '')
            .appendTo('#frm');



        //fetch existing record
        app.api.generate_from_request(stateMap.id, function (res) {
            if (res.error) {
                $('#app-error-message').text(res.msg);
            } else {
                //fill out form
                app.util_b.formData(res);
            }
        });





        //make context menu

        $(".app-context-nav").append('<li><a href="#!/license_requests/">Back</a></li>');

        $(".app-context-nav").append('<li><a href="#" id="btn-generate">Send</a></li>');
        $('#btn-generate').bind('click', onSend);

        // $(".app-context-nav").append('<li><a href="#" id="btn-select-all-addons">Regenerate</a></li>');
        // $('#btn-select-all-addons').bind('click', onRegenerate);


    };




    // return public methods
    return {
        configModule: configModule,
        initModule: initModule
    };
    //------------------- END PUBLIC METHODS ---------------------
}());