/**
The module for the New Column Dialog.
@class FolderCreateDialog
@namespace ProspectDirector.Modules.ExplorerModel.Folders
*/
ProspectDirector.Modules.ExplorerModel.Folders.FolderCreateDialog = (function (options) {

    var 

    //---------------------------
    //--- PRIVATE MEMBERS
    //---------------------------

    PRIVATE = {

        init: function (options) {

            PRIVATE.options = $.extend({
                selector: null,
                data: {
                    parentFolderId: null,
                    formTypes: []
                }
            }, options);

            PRIVATE.init_types();
            PRIVATE.registerEventHandlers();

        },

        //---------------------------
        //--- PRIVATE PROPERTIES
        //---------------------------

        get_dialog: function () {
            return $(PRIVATE.options.selector);
        },

        get_form: function () {
            return PRIVATE.get_dialog().find("FORM");
        },

        get_types: function () {
            return PRIVATE.get_dialog().find("ul[data-role=listview]");
        },

        get_type: function () {
            return PRIVATE.get_dialog().find("input[type=hidden][data-id=folder-type]");
        },

        get_createButton: function () {
            return PRIVATE.get_dialog().find("[data-command=create]");
        },

        get_cancelButton: function () {
            return PRIVATE.get_dialog().find("[data-command=cancel]");
        },

        //---------------------------
        //--- PRIVATE INTIALIZERS
        //---------------------------

        init_types: function () {

            PRIVATE.get_types().kendoListView({
                dataSource: PRIVATE.options.data.formTypes,
                template: kendo.template(this.get_dialog().find("[data-id=listViewTemplate]").html()),
                selectable: true,
                change: PRIVATE.types_onChange
            });

            PRIVATE.get_types().data("kendoListView").select(PRIVATE.get_types().children().first());

        },

        //---------------------------
        //--- PRIVATE EVENT HANDLERS
        //---------------------------

        types_onChange: function (e) {

            var data = e.sender.dataSource.getByUid(e.sender.select().data().uid);

            PRIVATE.get_type().val(data.id);

        },

        cancelButton_onClick: function () {
            PRIVATE.get_dialog().data("dialog").hide();
        },

        createButton_onClick: function () {

            var typeId = PRIVATE.getSelectedTypeId();

            if (typeId === null) {
                alert("No selection has been made.");
                return;
            }

            PRIVATE.get_createButton().disable();

            PD.ajax({

                type: PRIVATE.get_form().attr('method'),

                url: PRIVATE.get_form().attr('action'),

                data: {
                    parentFolderId: this.options.data.parentFolderId,
                    formTypeId: typeId
                },

                contextData: {
                    container: PRIVATE.get_form(),
                    control: PRIVATE.get_createButton()
                },

                success: PRIVATE.create_onSuccess

            });

        },

        create_onSuccess: function (a, b, c) {

            //redirect to prequisite action
            if (c.status === 202) {

                ProspectDirector.Types.Core.Notifications.success("Additional information required.");

                PRIVATE.get_dialog().data("kendoWindow").refresh({

                    url: ProspectDirector.RUNTIME.resolveUrl("~/" + a.controller + "/" + a.action),

                    data: {
                        dialogId: PRIVATE.options.selector.replace(/#/g, ""),
                        parentFolderId: a.parentFolderId,
                        folderTypeId: a.folderTypeId
                    }

                });

            }
            //else creation was successful
            else {

                PRIVATE.get_dialog().data("dialog").hide({
                    command: "ok",
                    data: a
                });

            }

        },

        //---------------------------
        //--- PRIVATE METHODS
        //---------------------------

        registerEventHandlers: function () {

            PRIVATE.get_cancelButton().on("click", $.proxy(PRIVATE.cancelButton_onClick, PRIVATE));
            PRIVATE.get_createButton().on("click", $.proxy(PRIVATE.createButton_onClick, PRIVATE));

        },

        getSelectedTypeId: function () {

            var listView = PRIVATE.get_types().data("kendoListView"),
                item = listView.select(),
                result = null;

            if (item.length > 0) {
                result = listView.dataSource.getByUid(item.data().uid).id;
            }

            return result;

        }

    },

    //---------------------------
    //--- PUBLIC MEMBERS
    //---------------------------

    PUBLIC = {

    };

    PRIVATE.init(options);

    return PUBLIC;

});
