/// class ProspectDirector.Web.UI.Queries.ConstraintsEditor
/// using ProspectDirector.Web.UI.Controls.Control

ProspectDirector.Web.UI.Queries.ConstraintsEditor = ProspectDirector.Web.UI.Controls.Control.extend({

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

    _constraints: null,
    _DOM: null,

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

    init: function (options) {

        this._options = $.extend(this._options, options);

        this._constraints = new ProspectDirector.Web.UI.Queries.Constraints({
            data: options.condition
        });

        this.DOM().render();

    },

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

    get_options: function () {
        return this._options;
    },

    get_featureSet: function () {
        return this.get_options().featureSet;
    },

    get_constraints: function () {
        return this._constraints;
    },

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

    addCondition: function (condition, parentId) {

        this.get_constraints().addCondition(condition, parentId);

        $(this).trigger("addCondition", {
            condition: condition,
            parentId: condition.get_parent() ? condition.get_parent().get_id() : undefined
        });

    },

    addFilter: function (filter, parentId) {

        var hasRoot = this._constraints.hasRoot();

        this.get_constraints().addFilter(filter, parentId);

        if (!hasRoot) {

            $(this).trigger("addCondition", {
                condition: this._constraints.get_root(),
                parentId: undefined
            });

        }

        $(this).trigger("addFilter", {
            filter: filter,
            parentId: filter.get_parent() ? filter.get_parent().get_id() : undefined
        });

    },

    editFilter: function (filter) {

        var command = new ProspectDirector.Web.UI.Queries.Commands.EditFilter({
            editor: this,
            selectedConstraint: filter,
            featureSet: this.get_featureSet()
        });

        command.execute();

    },

    update: function (data) {

        var constraint = this.get_constraints().findById(data.id);

        if (constraint) {
            constraint.update(data);
            constraint = constraint.toJSON();
        }

        $(this).trigger("updateConstraint", constraint);

    },

    remove: function (id) {

        this.get_constraints().remove(id);

        $(this).trigger("removeConstraint", {
            id: id
        });

    },

    move: function (srcId, dstId) {
        this.get_constraints().move(srcId, dstId);
    },

    clear: function () {

        this.get_constraints().clear();

        $(this).trigger("clear");

    },

    getSelection: function () {
        return this.DOM().getSelection();
    },

    select: function (id) {

        $(this).trigger("removeConstraint", {
            filter: constraint
        });

    },

    getConstraints: function () {
        return this.get_constraints().toJSON();
    },

    filterCount: function () {

        var result = this.get_constraints().getCounts();
        return result.filters;

    },

    conditionCount: function () {

        var result = this.get_constraints().getCounts();
        return result.conditions;

    },

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

    DOM: function () {

        var that = this,
            $DOM = null;

        if (!this._DOM) {

            this._DOM = new ProspectDirector.Web.UI.Queries.ConstraintsEditorDOM({
                editor: this
            });

            $DOM = $(this._DOM);

            $DOM.bind("moveConstraint", function (e, data) {
                that.move(data.srcId, data.dstId);
            });

            $DOM.bind("editFilter", function (e, data) {
                that.editFilter(data);
            });

        }

        return this._DOM;

    }

});
