/// class ProspectDirector.Web.UI.Queries.SelectColumnsDialog
/// using ProspectDirector.Web.UI.SelectionDialogBase

/**
@class SelectColumnsDialog
@namespace Web.UI.Queries
@constructor
**/
ProspectDirector.Web.UI.Queries.SelectColumnsDialog = ProspectDirector.Web.UI.SelectionDialogBase.extend({

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

    _DOM: null,

    //================================
    // CONSTRUCTOR
    //================================

    init: function (options) {

        options = $.extend(true, {} ,{
            config: {
                modal: true,
                height: 500,
                minHeight: 250,
                width: 600,
                minWidth: 400,
                iframe: false,
                resizable: true,
                title: "Select Columns"
            }
        }, options);

        this._ajaxRequest($.proxy(this._super, this), options);

    },

    //================================
    // PUBLIC METHODS
    //================================

    DOM: function () {

        if (!this._DOM) {

            this._DOM = new ProspectDirector.Web.UI.Queries.SelectColumnsDialogDOM({
                windowDOM: this.get_window().DOM(),
                close: $.proxy(this.close, this),
                closeAndReturn: $.proxy(this.closeAndReturn, this),
                depths: this._getDepth(),
                anchor: this.options.data.anchor,
                templateId: this.options.data.templateId
            });

        }

        return this._DOM;

    },

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

    _init_dialog: function () {
        this.DOM().render();
        this.DOM().activate();
    },

    _getDepth: function () {

        var result = new ProspectDirector.Web.List(),
            anchor = this.options.data.anchor.entityTypes,
            isDocumentBased = this.options.data.templateIsDocumentBased;

        if (anchor == null || anchor.groups.length === 0) {
            result = this._getDepthWithNoAnchor(isDocumentBased);
        }
        else {
            result = this._getDepthWithAnchor(anchor.groups, isDocumentBased);
        }

        if (isDocumentBased) {

            result.insert({
                name: "Document",
                index: 0
            }, 0);

        }

        return result;

    },

    _getDepthWithNoAnchor: function (isDocumentBased) {

        var that = this,
            result = new ProspectDirector.Web.List(),
            index = 0,
            maxDepth = this.options.data.maxDepth;

        for (index; index <= maxDepth; index++) {
            result.add({
                name: that._getDepthNames(index),
                value: isDocumentBased ? index + 1 : index
            });
        }

        return result;

    },

    _getDepthWithAnchor: function (anchorNodes, isDocumentBased) {

        var that = this,
            result = new ProspectDirector.Web.List(),
            list = new ProspectDirector.Web.List(anchorNodes);

        list.each(function (index, node) {
            result.add({
                name: that._getDepthNames(index, node.types),
                value: isDocumentBased ? index + 1 : index
            });
        });

        return result;

    },

    _getDepthNames: function (depth, types) {

        var result = "",
            names = new ProspectDirector.Web.List(types);

        if (depth === 0) {
            result = "Target";
        }
        else {
            result = "Ancestor " + depth;
        }

        names = names.transform(function (item) {
            return item.name;
        });

        if (names.count() > 0) {
            result += " - " + names.toArray().join(", ");
        }

        return result;

    },

    _ajaxRequest: function (fn, options) {

        PD.ajax({
            type: "GET",
            url: ProspectDirector.RUNTIME.resolveUrl("~/Query/FindColumnsDialog"),
            success: function (e) {
                options.data.maxDepth = e.maxDepth;
                fn(options);
            },
            error: function () {
                ProspectDirector.Web.UI.MessageDialog.error({
                    message: "Could not complete request."
                });
            }
        });

    }

});

//================================
// STATIC METHODS
//================================

ProspectDirector.Web.UI.Queries.SelectColumnsDialog.open = function (options) {
    return new ProspectDirector.Web.UI.Queries.SelectColumnsDialog(options);
};