/// class ProspectDirector.Modules.Explorer.Contents.FileMoveDialog
/// using ProspectDirector.Types.Core.Dialog

ProspectDirector.Modules.Explorer.Contents.FileMoveDialog = Class.extend({

    //---------------------------
    //--- CONSTRUCTORS
    //---------------------------

    init: function (options) {

        this.options = $.extend({
            selector: null
        }, options);

        this.init_dialog();
        this.init_tree();

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

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

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

    get_tree: function () {
        return this.get_dialog().find("DIV[data-role=tree]");
    },

    get_okButton: function () {
        return this.get_dialog().find("INPUT[type=button][data-command=ok]");
    },

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

    get_destinationFolderId: function () {
        return this.get_dialog().find("INPUT[type=hidden][data-id=destination-folder-id]");
    },

    //---------------------------
    //--- INITIALIZERS
    //---------------------------

    init_dialog: function () {

        this.get_okButton().on("click", $.proxy(this.okButton_onClick, this));
        this.get_cancelButton().on("click", $.proxy(this.cancelButton_onClick, this));

    },

    init_tree: function () {

        this.get_tree().kendoTreeView({
            dataSource: new kendo.data.HierarchicalDataSource({
                transport: {
                    read: $.proxy(this.tree_onRead, this)
                },
                schema: {
                    model: {
                        id: "id",
                        hasChildren: "hasChildren"
                    }
                }
            }),
            dataBound: $.proxy(this.tree_onDataBound, this),
            select: $.proxy(this.tree_onSelect, this)
        });

        this.get_tree().data("filler", new ProspectDirector.Types.Core.ElementFill({
            element: this.get_tree()
        }));

        this.get_tree().on("mouseover", "LI.k-item", $.proxy(this.tree_onMouseOver, this));

        this.get_tree().disableTextSelection();

    },

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

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

    okButton_onClick: function (e) {

        var tree = this.get_tree().data("kendoTreeView"),
            node = tree.select(),
            item = null;

        if (node.length === 0) {
            alert("A destination folder must be selected.");
            return;
        }

        item = tree.dataItem(node);

        if (!item.supportsFiles) {
            alert("Files cannot be moved into the selected folder.");
            return;
        }

        this.get_destinationFolderId().val(item.id);

        PD.ajax({
            type: "POST",
            url: ProspectDirector.RUNTIME.resolveUrl("~/ExplorerContent/FileMoveDialog"),
            data: this.get_form().serialize(),
            success: $.proxy(this.submit_onSuccess, this)
        });

    },

    submit_onSuccess: function (e) {

        this.get_dialog().data("dialog").hide({
            command: "ok",
            data: e
        });

    },

    tree_onDataBound: function (e) {

        var tree = this.get_tree().data("kendoTreeView"),
                data = null,
                item = null,
                node = null,
                index = 0,
                arrIndex = 0;

        if (e.node === undefined || e.node.length === 0) {
            data = e.sender.dataSource._data;
        }
        else {
            data = tree.dataItem(e.node).children._data;
        }

        if (data !== undefined) {

            for (index = 0; index < data.length; index++) {

                item = data[index];
                node = this.nodeTemplate(item);
                arrIndex = $.inArray(item.id, this.options.data.expandPath);
                if (arrIndex > -1) {
                    tree.select(node);
                    if (arrIndex < this.options.data.expandPath.length - 1) {
                        tree.expand(node);
                    }
                }

            }

        }

    },

    tree_onRead: function (settings) {

        var localDataSource = this.options.data.preloadNodes,
            tree = this.get_tree().data("kendoTreeView"),
            data = [];

        if ($.isEmptyObject(settings.data)) {
            data = ProspectDirector.Util.traverseAndFind(localDataSource);
        }
        else {
            data = ProspectDirector.Util.traverseAndFind(localDataSource, {
                filters: {
                    id: settings.data.id
                }
            });
        }

        if (data) {
            settings.success(data);
        }
        else {

            PD.ajax({
                url: ProspectDirector.RUNTIME.resolveUrl("~/Explorer/GetChildren"),
                data: {
                    entityId: tree.dataSource.get(settings.data.id).entityId,
                    folderId: settings.data.id
                },
                success: function (result) {
                    settings.success(result);
                }
            });

        }

    },

    tree_onSelect: function (e) {

        var tree = this.get_tree().data("kendoTreeView"),
            node = $(e.node),
            data = tree.dataItem(node);

        if (!data || !data.supportsFiles) {
            e.preventDefault();
        }

    },

    tree_onMouseOver: function (e) {

        var tree = this.get_tree().data("kendoTreeView"),
            node = $(e.target).closest("LI.k-item"),
            data = tree.dataItem(node);
        kin = node.find(".k-in").first();

        if (node.data("role") !== "ProcessStage") {
            kin.removeClass("k-state-hover");
        }

    },

    //---------------------------
    //--- METHODS
    //---------------------------

    nodeTemplate: function (data) {

        var tree = this.get_tree().data("kendoTreeView"),
                    $span = $("<span class=\"node-label\"></span>"),
                    $node = tree.findByUid(data.uid),
                    $inner = $node.find(".k-in").first(),
                    innerHTML = data.name;

        if (data.ref !== undefined) {
            $node.attr("data-ref", data.ref);
        }

        if (data.role !== undefined) {
            $node.attr("data-role", data.role);
        }

        if (!data.supportsFiles) {
            $span.css({
                opacity: .40
            });
        }

        if (data.supportsFiles && data.fileCount > 0) {
            innerHTML += " <b>(" + data.fileCount + ")</b>";
        }

        if (data.icon !== undefined && data.icon != null) {
            innerHTML = "<img src=\"" + ProspectDirector.RUNTIME.resolveUrl("~/application/icons/16x16/" + data.icon) + "\" style=\"vertical-align:top;\" />&nbsp;" + innerHTML;
        }

        $span.html(innerHTML);
        $inner.empty();
        $inner.html($span[0].outerHTML);

        return $node;

    }

});

ProspectDirector.Modules.Explorer.Contents.FileMoveDialog.Dialog =
new (ProspectDirector.Types.Core.Dialog.extend({

    init: function () {

        this._super("FileMoveDialog",
                    "Move Files",
                    ProspectDirector.RUNTIME.resolveUrl("~/ExplorerContent/FileMoveDialog"));

    }

}));