
ProspectDirector.Web.UI.FeatureSets.ValidationErrorHandler = Class.extend({

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

    _options: null,
    _errors: null,
    _globalErrors: null,
    _validationErrors: null,

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

    init: function (options) {
        this._options = options;
        this._validationErrors = new ProspectDirector.Web.UI.FeatureSets.ValidationErrorCollection();
        this._globalErrors = new ProspectDirector.Web.List();
        this._fill(options.validationErrors);
    },

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

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

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

    get_validationErrors: function () {
        return this._validationErrors;
    },

    get_globalErrors: function () {
        return this._globalErrors;
    },

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

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

    exec: function () {

        var that = this;

        this.get_eventBus().publish("validationErrors", {
            validationErrors: this.get_validationErrors()
        });

        this._checkForFeatureErrors();

    },

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

    _fill: function (data) {

        var that = this,
            errors = new ProspectDirector.Web.List(data);

        errors.each(function (index, error) {

            var feature = ProspectDirector.Web.UI.FeatureSets.ValidationErrorKey.resolve(error.key, that.get_featureSet()),
                validationError = null;

            if (feature) {

                validationError = new ProspectDirector.Web.UI.FeatureSets.ValidationError({
                    error: error,
                    feature: feature
                });

                that.get_validationErrors().add(validationError);

                // this section was added specifically for canvas in a feature set
                // consider a new method when converting FeatureSets to TypeScript
                that.get_featureSet().get_editors().each(function (index, editor) {
                    if (editor.get_feature().get_name().binding === feature.get_name().binding) {
                        editor.onValidationError(validationError);
                    }
                });
                // end
            }

            if (validationError === null || validationError.get_propertyName() === null) {
                that.get_globalErrors().add(error);
            }

        });

    },

    _checkForFeatureErrors: function (errors) {

        if (this.get_globalErrors().count() > 0) {

            // this section was added specifically for canvas in a feature set
            // consider a new method when converting FeatureSets to TypeScript
            var silent = false;

            _.each(this.get_globalErrors().toArray(), function (item) {
                silent = item.tags.hasOwnProperty("silent");
            });

            if (silent) {
                return;
            }
            // end

            PD.Web.UI.ErrorDialog.show({
                title: "Feature Error",
                message: "The following errors were encountered:",
                errors: this.get_globalErrors().toArray()
            });

        }

    }

});

//================================
// SHARED METHODS
//================================

ProspectDirector.Web.UI.FeatureSets.ValidationErrorHandler.getId = function (feature, name) {
    
    var group = feature.get_parent(),
        featureSet = group.get_parent();

    return "FeatureSet.Groups[" + featureSet.get_groups().indexOf(group) + "].Features[" + group.get_features().indexOf(feature) + "]." + name;

};