/*
 * Platform.js
 * Description: Basic system Front functions plugin. Profile attributes management and control
 * Author: Mikhael Aktsyzniy
 * Date: 09.10.2015
 * Date changed: 14.10.2015
 */
(function ($) {

    var methods = {
        
        init: function () {

            $("body").delegate(".jsAction", "click", function (event) {
                controller._aClick(this, event);
                if ($(this).attr("type") != "file") {
                    return false;
                }
            });

            $(".jsAction").on("change", function () {
                controller._aChange(this);
            });
        },
        getModal: function (message, type) {
            front.modal(message, type);
        },
    };

    var controller = {

        _aClick: function (obj, event) {

            var dataType = $(obj).attr("data-type");
            var dataAction = "_" + $(obj).attr("data-action");

            switch (dataType){
                case "node":
                    node[dataAction](obj);
                    break;
                case "namespace":
                    namespace[dataAction](obj);
                    break;
                case "profile":
                    profile[dataAction](obj, event);
                    break;
                default:
                    break;
            }
        },
        _aChange: function (obj) {
            profile["_" + $(obj).attr("data-action")](obj);
        },
    };

    var node = {

        _add: function () {

            var data = { "tempID": $("#tempID").val(), "nodeName": $("#nodeName").val(), "action": "_add" };
            var targets = { "noresults": "noresults.node", "results": "st_nodes" };

            request["_ajax"](data, targets, "/ajax/addnewnode/", "application/json; charset=utf-8", "text");
        },
        _delete: function (obj) {
            var data = { "tempID": $("#tempID").val(), "nodeName": $(obj).attr("data-value"), "action": "_delete", "route": $(obj).attr("data-route"), "reference": $(obj).attr("data-ref") };
            request["_ajax"](data, null, "/ajax/deletenode/", "application/json; charset=utf-8", "json");
        },
    };

    var namespace = {

        _add: function () {
            var data = { "tempID": $("#tempID").val(), "nodeName": $("#spaceName").val(), "nodeValue": $("#spaceValue").val(), "action": "_add" };
            var fields = { "noresults": "noresults.ns", "results": "st_namespace" };

            request["_ajax"](data, fields, "/ajax/addnewnamespace/", "application/json; charset=utf-8", "text");
        },
        _delete: function (obj) {
            var data = { "tempID": $("#tempID").val(), "nodeName": $(obj).attr("data-value"), "action": "_delete", "route": $(obj).attr("data-route"), "reference": $(obj).attr("data-ref") };
            request["_ajax"](data, null, "/ajax/deletenamespace/", "application/json; charset=utf-8", "json");
        },
    };

    var profile = {
        
        _addFiles: function (obj) {

            $("#st_fileSet").hide();

            if (($(obj)[0].files).length > 0) {
                $("#st_fileSet").empty();

                $.each($(obj)[0].files, function (index, value) {
                    $("#st_fileSet").append(index + 1 + ". " + value.name + "<br>");
                });

                $("#st_fileSet").show();
            }
        },
        _delete: function (obj, event) {
            event.preventDefault();
            var url = $(obj).attr("href");
            var dataTarget = $(obj).attr("data-target");

            front["dialog"](url, dataTarget);
        },
    };

    var front = {

        _add: function (_data, _targets) {
            $("." + _targets.noresults).hide();
            $("#" + _targets.results).append(_data);
        },
        _delete: function (_data) {

            if (Boolean(_data.isDeleted)) {
                $("#" + _data.id).fadeToggle("fast", function () {
                    $(this).remove();
                });
            } else {
                this.modal(_data.message, "error");
            }
            
        },
        dialog: function (url, target) {

            $("#" + target).dialog({
                autoOpen: false,
                modal: true,
                dialogClass: "no-close",
                buttons: {
                    "Delete": function () {
                        window.location.href = url;
                    },
                    "Cancel": function () {
                        $(this).dialog("close");
                    }
                }
            });
            $("#" + target).dialog("open");
        },
        modal: function (message, type) {

            var boxId = "#notice";
            $("#msg").addClass(type).html(message);

            $(boxId).fadeToggle("fast", function () {
                setTimeout(function () {
                    $(boxId).fadeToggle("fast");
                }, 2000);
            });
        },
    };

    var request = {

        _ajax: function (_data, _targets, uri, cntType, dataType) {
            $.ajax({
                url: uri,
                type: 'POST',
                data: JSON.stringify(_data),
                dataType: dataType,
                contentType: cntType
            }).done(function (data) {
                front[_data.action](data, _targets);
            });
        },
    };

    $.fn.platform = function (method) {

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('The ' + method + ' is not exist');
        }

    };
}(jQuery));