/*
 * app.license.js
 * License key generator
 */

/*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 = (function () {
    'use strict';
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var

        stateMap = {},
        configModule, initModule, onGenerate, onSelectAllAddOns;
    //----------------- END MODULE SCOPE VARIABLES ---------------

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


    //------------------- BEGIN EVENT HANDLERS -------------------
    onGenerate = 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.createLicense(submitData, function (res) {
            if (res.error) {
                $('#app-error-message').text(res.msg);
            } else {
                $('#key').val(res);
                return false;
            }
        });

        return false; //prevent default
    };


    onSelectAllAddOns = function (event) {
        event.preventDefault();
        $('#wbi').prop('checked', true);
        $('#mbi').prop('checked', true);
        $('#ri').prop('checked', true);
        $('#qbi').prop('checked', true);
        $('#pti').prop('checked', true);
        $('#quickNotification').prop('checked', true);
        $('#exportToXls').prop('checked', true);
        $('#outlookSchedule').prop('checked', true);
        $('#oli').prop('checked', true);
        $('#importExportCSVDuplicate').prop('checked', true);



        return false; //prevent default
    };

    // onTemplates = function(event) {
    //     event.preventDefault();
    //     alert("STUB: templates");


    //     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']({}));


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

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

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

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




        //set all date inputs to today plus one year
        var oneYearFromNow = moment().add(1, 'years').toISOString().substring(0, 10);
        var oneMonthFromNow = moment().add(1, 'months').toISOString().substring(0, 10);
        $('input[type="date"]').val(oneYearFromNow);
        $('#lockoutDate').val(oneMonthFromNow);

    };

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