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.Summary
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            HeatMapSetup: "c77de0ff-f64d-4b89-a121-72b96b0b8291"
        });
        Gadget.Cutoff1 = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "Cutoff1" };
        Gadget.Cutoff2 = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "Cutoff2" };
        Gadget.Cutoff3 = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "Cutoff3" };
        Gadget.AutoColor = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "AutoColor" };
        Gadget.ClearLastLogAfterGap = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "ClearLastLogAfterGap" };
        Gadget.RemoveOutliers = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "RemoveOutliers" };
        Gadget.ThresholdMax = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "ThresholdMax" };
        Gadget.ThresholdMin = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "ThresholdMin" };
        Gadget.ShowTotal = { id: Settings.Gadget.SettingGroup.HeatMapSetup, name: "ShowTotal" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var HeatMapSetup = (function (_super) {
            __extends(HeatMapSetup, _super);
            function HeatMapSetup() {
                _super.call(this, Settings.Gadget.SettingGroup.HeatMapSetup);
                this.title = i18n.loc.s('ISO50001Gadgets', 'Heat_Map_Setup');
            }
            HeatMapSetup.CreateInstance = function () {
                var instance = new HeatMapSetup();
                return instance;
            };
            HeatMapSetup.prototype.draw = function () {
                var _this = this;
                this.$container.empty();
                TemplateUtil.LocAndLoad(HeatMapSetup.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.html(template);
                    _this.render();
                }, this));
            };
            HeatMapSetup.prototype.isConfigured = function () {
                return true;
            };
            //////////////////////////////////////////////////////////////////////
            //Validate the page and add error messages if page is not valid.
            //////////////////////////////////////////////////////////////////////
            HeatMapSetup.prototype.isValid = function () {
                //If remove outliers is enabled then validate threshold values.            
                var removeOutliersChecked = this.$container.find('#remove-outliers').prop('checked');
                if (true == removeOutliersChecked) {
                    var thresholdMax = NumberUtil.parseUserInput(this.$container.find('#threshold-max').val(), '50');
                    var thresholdMin = NumberUtil.parseUserInput(this.$container.find('#threshold-min').val(), '0');
                    if (thresholdMax < thresholdMin) {
                        var removeOutliers = this.$container.find('#remove-outliers');
                        this.addValidationMessage(removeOutliers, i18n.loc.s('ISO50001Gadgets', 'Invalid_Thresholds'));
                        return false;
                    }
                }
                //If auto color is disabled, validate the cut off ranges
                var autoColor = this.$container.find('#auto-color-heatmap').prop('checked');
                if (false == autoColor) {
                    var cutOff1 = NumberUtil.parseUserInput(this.$container.find('#cutoff-range1').val(), '');
                    if (typeof cutOff1 !== 'number') {
                        this.addValidationMessage(this.$container.find('#cutoff-range1'), i18n.loc.s('ISO50001Gadgets', 'Invalid_Cutoff1'));
                        return false;
                    }
                    var cutOff2 = NumberUtil.parseUserInput(this.$container.find('#cutoff-range2').val(), '');
                    if (typeof cutOff2 !== 'number') {
                        this.addValidationMessage(this.$container.find('#cutoff-range2'), i18n.loc.s('ISO50001Gadgets', 'Invalid_Cutoff2'));
                        return false;
                    }
                    var cutOff3 = NumberUtil.parseUserInput(this.$container.find('#cutoff-range3').val(), '');
                    if (typeof cutOff3 !== 'number') {
                        this.addValidationMessage(this.$container.find('#cutoff-range3'), i18n.loc.s('ISO50001Gadgets', 'Invalid_Cutoff3'));
                        return false;
                    }
                    if (!(cutOff1 < cutOff2 && cutOff2 < cutOff3)) {
                        this.addValidationMessage(this.$container.find('#cutoff-range2'), i18n.loc.s('ISO50001Gadgets', 'Invalid_Cutoffs'));
                        return false;
                    }
                }
                return true;
            };
            HeatMapSetup.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.Cutoff1.name:
                    case Settings.Gadget.Cutoff2.name:
                    case Settings.Gadget.Cutoff3.name:
                    case Settings.Gadget.AutoColor.name:
                    case Settings.Gadget.ClearLastLogAfterGap.name:
                    case Settings.Gadget.RemoveOutliers.name:
                    case Settings.Gadget.ThresholdMax.name:
                    case Settings.Gadget.ThresholdMin.name:
                    case Settings.Gadget.ShowTotal.name:
                        managed = true;
                        break;
                }
                ;
                return managed;
            };
            HeatMapSetup.prototype.beforeCommit = function () {
                var cutOff1 = this.$container.find('#cutoff-range1').val();
                this.setting(Settings.Gadget.Cutoff1.name, cutOff1);
                var cutOff2 = this.$container.find('#cutoff-range2').val();
                this.setting(Settings.Gadget.Cutoff2.name, cutOff2);
                var cutOff3 = this.$container.find('#cutoff-range3').val();
                this.setting(Settings.Gadget.Cutoff3.name, cutOff3);
                var autoColor = this.$container.find('#auto-color-heatmap').prop('checked');
                this.setting(Settings.Gadget.AutoColor.name, autoColor.toString());
                var clearLastLogAfterGap = this.$container.find('#clear-data-gap').prop('checked');
                this.setting(Settings.Gadget.ClearLastLogAfterGap.name, clearLastLogAfterGap.toString());
                var removeOutliers = this.$container.find('#remove-outliers').prop('checked');
                this.setting(Settings.Gadget.RemoveOutliers.name, removeOutliers.toString());
                var thresholdMax = this.$container.find('#threshold-max').val();
                this.setting(Settings.Gadget.ThresholdMax.name, thresholdMax);
                var thresholdMin = this.$container.find('#threshold-min').val();
                this.setting(Settings.Gadget.ThresholdMin.name, thresholdMin);
                var showTotal = this.$container.find('#show-total').prop('checked');
                this.setting(Settings.Gadget.ShowTotal.name, showTotal.toString());
            };
            HeatMapSetup.prototype.render = function () {
                var _this = this;
                var cutOff1 = this.$container.find('#cutoff-range1');
                cutOff1.val(this.setting(Settings.Gadget.Cutoff1.name).toString());
                var cutOff2 = this.$container.find('#cutoff-range2');
                cutOff2.val(this.setting(Settings.Gadget.Cutoff2.name).toString());
                var cutOff3 = this.$container.find('#cutoff-range3');
                cutOff3.val(this.setting(Settings.Gadget.Cutoff3.name).toString());
                var clearLastLogChecked = this.setting(Settings.Gadget.ClearLastLogAfterGap.name).toString().toLowerCase() === 'true';
                this.$container.find('#clear-data-gap').prop('checked', clearLastLogChecked);
                var autoColorValue = this.setting(Settings.Gadget.AutoColor.name).toString().toLowerCase() === 'true';
                var autoColor = this.$container.find('#auto-color-heatmap');
                autoColor.prop('checked', autoColorValue);
                cutOff1.prop('disabled', autoColorValue);
                cutOff2.prop('disabled', autoColorValue);
                cutOff3.prop('disabled', autoColorValue);
                var removeOutliersValue = this.setting(Settings.Gadget.RemoveOutliers.name).toString().toLowerCase() === 'true';
                var removeOutliers = this.$container.find('#remove-outliers');
                removeOutliers.prop('checked', removeOutliersValue);
                var thresholdMax = this.$container.find('#threshold-max');
                thresholdMax.val(this.setting(Settings.Gadget.ThresholdMax.name).toString());
                thresholdMax.prop('disabled', !removeOutliersValue);
                var thresholdMin = this.$container.find('#threshold-min');
                thresholdMin.val(this.setting(Settings.Gadget.ThresholdMin.name).toString());
                thresholdMin.prop('disabled', !removeOutliersValue);
                //Handle the checked event for 
                autoColor.change(function (event) {
                    _this.globalAutoColorChanged(event);
                });
                removeOutliers.change(function (event) {
                    _this.globalRemoveOutliersChanged(event);
                });
                var showTotalChecked = this.setting(Settings.Gadget.ShowTotal.name);
                var showTotal = _.isUndefined(showTotalChecked) || _.isNull(showTotalChecked) ? true : showTotalChecked.toString().toLowerCase() === 'true';
                this.$container.find('#show-total').prop('checked', showTotal);
            };
            HeatMapSetup.prototype.globalAutoColorChanged = function (event) {
                var autoColor = $(event.delegateTarget).prop('checked');
                this.$container.find('#cutoff-range1').prop('disabled', autoColor);
                this.$container.find('#cutoff-range2').prop('disabled', autoColor);
                this.$container.find('#cutoff-range3').prop('disabled', autoColor);
            };
            HeatMapSetup.prototype.globalRemoveOutliersChanged = function (event) {
                var removeOutliers = $(event.delegateTarget).prop('checked');
                this.$container.find('#threshold-max').prop('disabled', !removeOutliers);
                this.$container.find('#threshold-min').prop('disabled', !removeOutliers);
            };
            HeatMapSetup.SetupControlTemplateUri = 'Content/Gadgets/HeatMap/HeatMapSetup.html';
            return HeatMapSetup;
        }(Dashboards.Models.SetupControl));
        SetupControl.HeatMapSetup = HeatMapSetup;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.HeatMapSetup.CreateInstance,
    id: Settings.Gadget.SettingGroup.HeatMapSetup
});
//# sourceMappingURL=HeatMapSetup.js.map