/*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.rfsettings = (function () {
    "use strict";
    //---------------- BEGIN MODULE SCOPE VARIABLES --------------
    var stateMap = {},
        configModule,
        onChangePassword,
        initModule,
        onTest;
    //----------------- END MODULE SCOPE VARIABLES ---------------

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

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

    ///////////////////////////////
    //ONUPDATE
    //
    onChangePassword = function (event) {
        event.preventDefault();
        $.gevent.publish("app-clear-error");
        //get form data
        var formData = $("form").serializeArray({
            checkboxesAsBools: true
        });

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

        app.api.create(
            "user/" + app.shell.stateMap.user.id + "/changepassword",
            submitData,
            function (res) {
                if (res.error) {
                    $.gevent.publish("app-show-error", res.msg);
                } else {
                    page("#!/logout");
                }
            }
        );

        return false; //prevent default?
    };

    ///////////////////////////////
    //ONTEST
    //
    onTest = function (event) {
        event.preventDefault();
        //debugger;
        var submitData = {
            creation_date: "2018-06-19T11:08:09.0000000Z", // <-- NEW
            id: 283832781, // <-- NEW
            order_notification: {
                // <-- NEW - Type of the message (same as in XML with out the "e5"-prefix)
                purchase: {
                    customer_data: {
                        billing_contact: {},
                        customer_payment_data: {},
                        delivery_contact: {},
                        language: "English",
                        language_iso: "en", // <-- NEW
                        reg_name: "Test",
                        shopper_id: "5678", // <-- NEW, also known as Customer ID
                        subscribe_newsletter: false,
                        user_id: "abc@test.com-100"
                    },
                    payment_status: "complete",
                    payment_status_id: "PCA", // <-- NEW, our status ID
                    purchase_id: 1234567890,
                    purchase_date: "2018-03-29T10:25:12.0000000Z",
                    purchase_item: [
                        // <-- NEW, this is now an array
                        {
                            running_no: 1,
                            your_product_id:
                                "this is the same as product_code in the API"
                        },
                        {
                            running_no: 2
                        }
                    ],
                    purchase_origin: "online"
                }
            }
        };
        app.api.test(submitData, function (res) {
            if (res.error) {
                alert("ERROR");
                $.gevent.publish("app-show-error", res.msg);
            } else {
                //do nothing, success!
                alert("SUCCESS!");
                //$('#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.rfsettings"]({}));

        // bind actions
        $("#btn-change-password").bind("click", onChangePassword);
        $("#btn-test").bind("click", onTest);

        //Context menu
        app.nav.contextClear();

        app.api.get("meta/server_version/", function (res) {
            if (res.error) {
                $.gevent.publish("app-show-error", res.msg);
            } else {
                $("#about").append(
                    "<p>Rockfish client version: " +
                        app.api.RockFishVersion +
                        "</p><p>Rockfish server version: " +
                        res.server_version +
                        "</p>"
                );
            }
        });

        ////app.nav.setContextTitle("Search");
    };

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