/// class PD.Manage.Modules.Resources.ActionSelectionDialog

PD.Manage.Modules.Actions.ActionSelectionDialog =
PD.Web.UI.Dialog.extend({

    //===================================
    // FIELDS
    //===================================

    _state: null,
    _splitter: null,
    _explorer: null,
    _browser: null,
    _selection: null,
    
    //===================================
    // CONSTRUCTOR
    //===================================

    init: function (options) {

        this._super($.extend(true, {
            selectMode: {
                type: null,
                multiple: false
            },
            config: {
                height: "400px",
                minHeight: "350px",
                width: "700px",
                minWidth: "350px",
                title: "Select Action",

            }
        }, options));

        this._selection = [];

    },

    //===================================
    // PROPERTIES
    //===================================

    get_okButton: function () {
        return this.get_node().find("BUTTON[data-id=ok]");
    },

    get_canelButton: function () {
        return this.get_node().find("BUTTON[data-id=cancel]");
    },

    get_selectionTextBox: function () {
        return this.get_node().find("INPUT[type=text][data-id=selections]");
    },

    //===================================
    // PROTECTED EVENT HANDLERS
    //===================================

    onInit: function () {
        this._explorer_init();
        this._browser_init();
    },

    onActivate: function (options) {
        
        this.get_okButton().on("click", _.bind(this._okButton_onClick, this));
        this.get_canelButton().on("click", _.bind(this._cancelButton_onClick, this));

        this.subscriptions.add("action/selected", _.bind(this._action_onSelected, this));
        this.subscriptions.add("action/select", _.bind(this._action_onSelect, this));
        this.subscriptions.add("folder/select", _.bind(this._folder_onSelect, this));
    
        this._splitter_activate();

    },

    onResize: function () {
        if (this._location != null) {
            this._location.resize();
        }
    },

    onRemove: function () {
    
        if (this._splitter != null) {
            this._splitter.destroy();
        }

    },

    //===================================
    // PRIVATE EVENT HANDLERS
    //===================================

    _okButton_onClick: function () {
        this._commitSelection(null, true)
    },

    _cancelButton_onClick: function () {
        this.close();
    },

    _folder_onSelect: function e() {
        this.get_okButton().prop("disabled", true);
        this._selection = null;
    },

    _action_onSelect: function (e) {
        this._commitSelection(e, false);
    },

    _action_onSelected: function (e) {
        this._commitSelection(e, true);
    },

    _commitSelection: function (selection, closeDialog) {
    
        var definition = this._selection;

        if (selection != null) {
            definition = PD.Actions.ActionDefinition.getActionDefinition(selection.pluginId, selection.id);
        }
        
        if (definition != null) {
            this.get_okButton().prop("disabled", false);
            this.get_selectionTextBox().val(definition.get_name());
            this._selection = definition;
        }

        if (closeDialog) {
            this.close({
                command: "ok",
                data: this._selection
            });
        }

    },

    //===================================
    // PROTECTED METHODS
    //===================================

    template: function (options) {
        return [
            "<div style='position:absolute;top:0;left:0;bottom:0;right:0;overflow:hidden;'>",
                "<div style='position:absolute;top:10px;left:10px;bottom:74px;right:10px;overflow:hidden;'>",
                    "<div data-id='splitter' class='-pd-minimal-splitter' style='height:100%;box-sizing:border-box;'>",
                    "</div>",
                "</div>",
                "<div style='position:absolute;height:22px;left:10px;bottom:42px;right:10px;overflow:hidden;'>",
                    "<div style='position:absolute;top:0;left:0;width:100px;height:22px;box-sizing:border-box;border:0 none;font-size:12px;line-height:22px;padding:0 10px 0 0;text-align:right;'>",
                        "Action:",
                    "</div>",
                    "<div style='position:absolute;top:0;left:100px;right:0;height:22px;box-sizing:border-box;border:0 none;'>",
                        "<input type='text' data-id='selections' style='height:20px;width:100%;box-sizing:border-box;border:1px solid #CCCCCC;padding-left:5px;line-height:17px;color:#AAAAAA;' readonly/>",
                    "</div>",
                "</div>",
                "<div class='-pd-dialog-button-deck'>",
                    "<button class='-pd-button' data-id='ok' disabled>OK</button>",
                    "<button class='-pd-button' data-id='cancel'>Cancel</button>",
                "</div>",
            "</div>"
        ];
    },

    //===================================
    // PRIVATE METHODS
    //===================================

    _splitter_activate: function () {

        var that = this;

        this._splitter = this.get_node().find("[data-id=splitter]").kendoSplitter({
            orientation: "horizontal",
            panes: [
                { size: "200px" },
                {}
            ],
            resize: PD.BCL.Util.debounce(function () {
                that._splitter._resize();
            }, 100)
        }).data("kendoSplitter");

    },

    _explorer_init: function () {

        this._explorer = new PD.Manage.Modules.Actions.ActionSelectionExplorer({
            insertAt: "DIV[data-id=splitter]"
        });

        this.get_children().add(this._explorer);

    },

    _browser_init: function () {

        this._browser = new PD.Manage.Modules.Actions.ActionSelectionBrowser({
            insertAt: "DIV[data-id=splitter]"
        });

        this.get_children().add(this._browser);
    }

});

PD.Manage.Modules.Actions.ActionSelectionDialog.open = function (options) {
    return new PD.Manage.Modules.Actions.ActionSelectionDialog(options);
};