var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Settings;
(function (Settings) {
    var Gadget;
    (function (Gadget) {
        // Settings.Gadget.SettingGroup.ViewingPeriod
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            ViewingPeriod: "9b785fd2-daf8-11df-944e-f6f3dfd72085"
        });
        Gadget.SelectedPeriodId = { id: Settings.Gadget.SettingGroup.ViewingPeriod, name: "SelectedPeriodId" };
        Gadget.ViewingPeriodFormulaOne = { id: Settings.Gadget.SettingGroup.ViewingPeriod, name: "ViewingPeriodForumulaOne" };
        Gadget.ViewingPeriodFormulaTwo = { id: Settings.Gadget.SettingGroup.ViewingPeriod, name: "ViewingPeriodForumulaTwo" };
        Gadget.SelectedAggregationId = { id: Settings.Gadget.SettingGroup.ViewingPeriod, name: "SelectedAggregationId" };
        Gadget.DefaultRefreshSeconds = { id: Settings.Gadget.SettingGroup.ViewingPeriod, name: "DefaultRefreshSeconds" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var ViewPeriod = (function (_super) {
            __extends(ViewPeriod, _super);
            function ViewPeriod() {
                _super.call(this, Settings.Gadget.SettingGroup.ViewingPeriod);
                this.$viewingPeriodSelect = null;
                this.$aggregationOptionsWrapper = null;
                this.viewingPeriodGroup = null;
                this.originalAggregation = null;
                this.originalViewingPeriod = null;
                this.isAggregationEnabled = false;
                this.title = i18n.loc.s('Gadgets', 'ViewingPeriodControl_Title');
            }
            ViewPeriod.CreateInstance = function () {
                var instance = new ViewPeriod();
                return instance;
            };
            ViewPeriod.prototype.draw = function () {
                var _this = this;
                this.$container.empty();
                TemplateUtil.LocAndLoad(ViewPeriod.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.html(template);
                    _this.render();
                }, this));
            };
            ViewPeriod.prototype.isConfigured = function () {
                return true;
            };
            ViewPeriod.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.SelectedPeriodId.name:
                    case Settings.Gadget.SelectedAggregationId.name:
                    case Settings.Gadget.ViewingPeriodFormulaOne.name:
                    case Settings.Gadget.ViewingPeriodFormulaTwo.name:
                    case Settings.Gadget.DefaultRefreshSeconds.name:
                        managed = true;
                        break;
                }
                return managed;
            };
            /* Private methods
            /*********************************************************************/
            ViewPeriod.prototype.render = function () {
                var _this = this;
                // setup viewing periods
                this.$viewingPeriodSelect = this.$container.find('.viewing-period');
                this.$aggregationOptionsWrapper = this.$container.find('.aggregationOptions');
                var currentViewingPeriodId = this.setting(Settings.Gadget.SelectedPeriodId.name);
                this.viewingPeriodGroup = Settings.ViewingPeriod.GetViewingPeriodGroupByPeriodId(currentViewingPeriodId);
                if (!this.viewingPeriodGroup) {
                    fwk.logError(StringUtil.format(i18n.loc.s('Gadgets', 'ViewingPeriodControl_Error_CannotLocateViewPeriod_1'), currentViewingPeriodId));
                    return;
                }
                this.originalViewingPeriod = Settings.ViewingPeriod.GetViewingPeriod(this.viewingPeriodGroup.Name, currentViewingPeriodId);
                this.originalAggregation = this.setting(Settings.Gadget.SelectedAggregationId.name);
                this.isAggregationEnabled = this.viewingPeriodGroup.AllowAggregation;
                if (!this.isAggregationEnabled) {
                    this.$container.find('.aggregationWrapper').hide();
                }
                _.each(this.viewingPeriodGroup.Entries, function (entry) {
                    $("<option/>", {
                        value: entry.Key,
                        text: i18n.loc.unencoded('DateListEntryDisplayNameResources', entry.Name)
                    })
                        .data("periodId", entry.Key)
                        .data("refreshSec", entry.MinimumRefreshSeconds)
                        .data("formula1", entry.Formulas[0])
                        .data("formula2", entry.Formulas[1])
                        .appendTo(_this.$viewingPeriodSelect);
                });
                this.$viewingPeriodSelect.val(currentViewingPeriodId);
                this.$viewingPeriodSelect.change(function (event) {
                    _this.viewingPeriodChanged(event);
                });
                // trigger event to populate aggregation selector
                this.$viewingPeriodSelect.change();
            };
            // Called when the viewing period selection has been changed.
            // Updates the list of aggregation options to match the period
            // and selects a value.
            ViewPeriod.prototype.viewingPeriodChanged = function (event) {
                // Grab the viewing period entry that is selected
                var periodId = $(event.delegateTarget).val();
                var selectedViewPeriodEntry = _.find(this.viewingPeriodGroup.Entries, function (entry) {
                    return entry.Key == periodId;
                });
                if (!selectedViewPeriodEntry) {
                    fwk.logError(StringUtil.format(i18n.loc.s('Gadgets', 'ViewingPeriodControl_Error_CannotLocateViewPeriod_1'), periodId));
                    return;
                }
                // update the settings to ensure that we have captured the options shown in the selectors
                var option = $(event.delegateTarget).children(":selected");
                this.updateViewingPeriodSettings(option);
                if (this.isAggregationEnabled) {
                    var selectedAggregationEntry = this.populateAggregations(selectedViewPeriodEntry);
                    this.updateAggregationSetting(selectedAggregationEntry);
                }
            };
            ViewPeriod.prototype.populateAggregations = function (selectedViewPeriodEntry) {
                var _this = this;
                if (!selectedViewPeriodEntry.AllowedAggregations) {
                    fwk.logError(i18n.loc.s('Gadgets', 'ViewingPeriodControl_Error_CannotLocateAggregation'));
                    return null;
                }
                var defaultValue, restoreValue;
                // Clear the list, and populate based on the Viewing Period
                this.$aggregationOptionsWrapper.empty();
                _.each(selectedViewPeriodEntry.AllowedAggregations, function (aggregation) {
                    var $aggEntry = _this.$container.find('#aggregationEntryTemplate').clone();
                    var populatedAggregation = TemplateUtil.ApplyLocalModel('aggregationEntryTemplate', $aggEntry.html(), {
                        "Key": aggregation.Key,
                        "DisplayName": i18n.loc.unencoded('Gadgets', 'ViewingPeriodControl_Aggregation' + aggregation.Name)
                    });
                    var $populatedAggregation = $(populatedAggregation);
                    $populatedAggregation.appendTo(_this.$aggregationOptionsWrapper);
                    $populatedAggregation.find('input').change(function (event) {
                        _this.aggregationChanged(event);
                    });
                    // Always set a default, incase the DB is missing a default.  It will be overwritten by the true 
                    // default if one exists.
                    if (_.isNull(defaultValue))
                        defaultValue = aggregation.Key;
                    // If this was not our original selection, use the default agg
                    if (aggregation.IsDefaultSelection === true) {
                        defaultValue = aggregation.Key;
                    }
                });
                var selectedAggvalue;
                if (selectedViewPeriodEntry.Key == this.originalViewingPeriod.Key && !_.isNull(this.originalAggregation)) {
                    // This entry is our original selection, use the last setting
                    selectedAggvalue = this.originalAggregation;
                }
                else {
                    // This entry is not our original, so use the default
                    selectedAggvalue = defaultValue;
                }
                // Restore the selection
                this.$container.find('.aggregation-entry-' + selectedAggvalue).prop('checked', true);
                return selectedAggvalue;
            };
            ViewPeriod.prototype.updateViewingPeriodSettings = function ($option) {
                this.setting(Settings.Gadget.SelectedPeriodId.name, $option.data("periodId"));
                this.setting(Settings.Gadget.ViewingPeriodFormulaOne.name, $option.data("formula1"));
                this.setting(Settings.Gadget.ViewingPeriodFormulaTwo.name, $option.data("formula2") || "");
                this.setting(Settings.Gadget.DefaultRefreshSeconds.name, $option.data("refreshSec"));
            };
            ViewPeriod.prototype.aggregationChanged = function (event) {
                var selectedValue = $(event.delegateTarget).val();
                this.updateAggregationSetting(selectedValue);
            };
            ViewPeriod.prototype.updateAggregationSetting = function (aggregationId) {
                this.setting(Settings.Gadget.SelectedAggregationId.name, aggregationId);
            };
            ViewPeriod.SetupControlTemplateUri = 'Content/Gadgets/_baseSetupControls/ViewingPeriodSetupControlTemplate.html';
            return ViewPeriod;
        }(Dashboards.Models.SetupControl));
        SetupControl.ViewPeriod = ViewPeriod;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.ViewPeriod.CreateInstance,
    id: Settings.Gadget.SettingGroup.ViewingPeriod
});
//# sourceMappingURL=ViewingPeriodSetupControl.js.map