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.ChartAxis
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            ChartAxis: "b98cb4f1-b762-434a-9f15-6e3ae333480c"
        });
        Gadget.LeftAxisTitle = { id: Gadget.SettingGroup.ChartAxis, name: "LeftAxisTitle" };
        Gadget.LeftAxisType = { id: Gadget.SettingGroup.ChartAxis, name: "LeftAxisType" };
        Gadget.LeftAxisMin = { id: Gadget.SettingGroup.ChartAxis, name: "LeftAxisMin" };
        Gadget.LeftAxisMinType = { id: Gadget.SettingGroup.ChartAxis, name: "LeftAxisMinType" };
        Gadget.LeftAxisMax = { id: Gadget.SettingGroup.ChartAxis, name: "LeftAxisMax" };
        Gadget.LeftAxisMaxType = { id: Gadget.SettingGroup.ChartAxis, name: "LeftAxisMaxType" };
        Gadget.RightAxisTitle = { id: Gadget.SettingGroup.ChartAxis, name: "RightAxisTitle" };
        Gadget.RightAxisType = { id: Gadget.SettingGroup.ChartAxis, name: "RightAxisType" };
        Gadget.RightAxisMin = { id: Gadget.SettingGroup.ChartAxis, name: "RightAxisMin" };
        Gadget.RightAxisMinType = { id: Gadget.SettingGroup.ChartAxis, name: "RightAxisMinType" };
        Gadget.RightAxisMax = { id: Gadget.SettingGroup.ChartAxis, name: "RightAxisMax" };
        Gadget.RightAxisMaxType = { id: Gadget.SettingGroup.ChartAxis, name: "RightAxisMaxType" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var ChartAxis = (function (_super) {
            __extends(ChartAxis, _super);
            function ChartAxis() {
                _super.call(this, Settings.Gadget.SettingGroup.ChartAxis);
                this.title = i18n.loc.s('Gadgets', 'ChartAxisControl_Title');
            }
            ChartAxis.CreateInstance = function () {
                var instance = new ChartAxis();
                return instance;
            };
            ChartAxis.prototype.draw = function () {
                var _this = this;
                TemplateUtil.LocAndLoad(ChartAxis.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.empty();
                    _this.$container.html(template);
                    _this.render();
                }, this));
            };
            ChartAxis.prototype.isConfigured = function () {
                return true;
            };
            ChartAxis.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.LeftAxisTitle.name:
                    case Settings.Gadget.LeftAxisType.name:
                    case Settings.Gadget.LeftAxisMin.name:
                    case Settings.Gadget.LeftAxisMinType.name:
                    case Settings.Gadget.LeftAxisMax.name:
                    case Settings.Gadget.LeftAxisMaxType.name:
                    case Settings.Gadget.RightAxisTitle.name:
                    case Settings.Gadget.RightAxisType.name:
                    case Settings.Gadget.RightAxisMin.name:
                    case Settings.Gadget.RightAxisMinType.name:
                    case Settings.Gadget.RightAxisMax.name:
                    case Settings.Gadget.RightAxisMaxType.name:
                        managed = true;
                        break;
                }
                ;
                return managed;
            };
            ChartAxis.prototype.isValid = function () {
                var isValid = this.$container.find('.' + Dashboards.Models.SetupControl.ValidationErrorClass).length == 0;
                if (isValid) {
                    // left axis title
                    var $leftTitle = this.$container.find('#setup-control-LeftAxisTitle-input');
                    var titleValidation = Validation.Text.validateUserText($leftTitle, { allowEmpty: true });
                    if (!titleValidation.valid) {
                        isValid = false;
                        this.addValidationMessage($leftTitle, titleValidation.message);
                    }
                    if (isValid) {
                        // Left axis Max
                        var leftAxisMaxType = this.setting(Settings.Gadget.LeftAxisMaxType.name) || 'auto';
                        if (leftAxisMaxType != 'auto') {
                            isValid = this.isDecimalValid(this.$container.find('#setup-control-LeftAxisMax-input'), Settings.Gadget.LeftAxisMax.name);
                        }
                    }
                    if (isValid) {
                        // Left axis Min
                        var leftAxisMinType = this.setting(Settings.Gadget.LeftAxisMinType.name) || 'auto';
                        if (leftAxisMinType != 'auto') {
                            isValid = this.isDecimalValid(this.$container.find('#setup-control-LeftAxisMin-input'), Settings.Gadget.LeftAxisMin.name);
                        }
                        // If we have valid numbers then make sure the min is not greater than the max
                        if (isValid && (leftAxisMaxType != 'auto' && leftAxisMinType != 'auto')) {
                            var leftAxisMax = parseFloat(this.setting(Settings.Gadget.LeftAxisMax.name));
                            var leftAxisMin = parseFloat(this.setting(Settings.Gadget.LeftAxisMin.name));
                            if (leftAxisMin >= leftAxisMax) {
                                isValid = false;
                                this.addValidationMessageMulti([this.$container.find('#setup-control-LeftAxisMax-input'), this.$container.find('#setup-control-LeftAxisMin-input')], i18n.loc.s('Dashboard', 'ValidationWarning_AxisMaxMustBeLargerThanMin'));
                            }
                        }
                    }
                }
                if (isValid) {
                    // right axis title
                    var $rightTitle = this.$container.find('#setup-control-RightAxisTitle-input');
                    var titleValidation = Validation.Text.validateUserText($rightTitle, { allowEmpty: true });
                    if (!titleValidation.valid) {
                        isValid = false;
                        this.addValidationMessage($rightTitle, titleValidation.message);
                    }
                    if (isValid) {
                        // Right axis Max
                        var rightAxisMaxType = this.setting(Settings.Gadget.RightAxisMaxType.name) || 'auto';
                        if (rightAxisMaxType != 'auto') {
                            isValid = this.isDecimalValid(this.$container.find('#setup-control-RightAxisMax-input'), Settings.Gadget.RightAxisMax.name);
                        }
                    }
                    if (isValid) {
                        // Right axis Min
                        var rightAxisMinType = this.setting(Settings.Gadget.RightAxisMinType.name) || 'auto';
                        if (rightAxisMinType != 'auto') {
                            isValid = this.isDecimalValid(this.$container.find('#setup-control-RightAxisMin-input'), Settings.Gadget.RightAxisMin.name);
                        }
                        // If we have valid numbers then make sure the min is not greater than the max
                        if (isValid && (rightAxisMaxType != 'auto' && rightAxisMinType != 'auto')) {
                            var rightAxisMax = parseFloat(this.setting(Settings.Gadget.RightAxisMax.name));
                            var rightAxisMin = parseFloat(this.setting(Settings.Gadget.RightAxisMin.name));
                            if (rightAxisMin >= rightAxisMax) {
                                isValid = false;
                                this.addValidationMessageMulti([this.$container.find('#setup-control-RightAxisMax-input'), this.$container.find('#setup-control-RightAxisMin-input')], i18n.loc.s('Dashboard', 'ValidationWarning_AxisMaxMustBeLargerThanMin'));
                            }
                        }
                    }
                }
                return isValid;
            };
            ChartAxis.prototype.render = function () {
                var _this = this;
                // Left Axis
                var $leftTitle = this.$container.find('#setup-control-LeftAxisTitle-input');
                var leftTitle = this.setting(Settings.Gadget.LeftAxisTitle.name);
                $leftTitle.val(leftTitle);
                $leftTitle.change(function (event) {
                    _this.stringValueChanged(event, Settings.Gadget.LeftAxisTitle.name);
                });
                var $leftAxisMax = this.$container.find('#setup-control-LeftAxisMax-input');
                var leftAxisMax = this.setting(Settings.Gadget.LeftAxisMax.name);
                $leftAxisMax.val(i18n.formatNumber(leftAxisMax) || '0');
                var $leftAxisMaxType = this.$container.find('.setup-control-left-max-value-type');
                var leftAxisMaxType = this.setting(Settings.Gadget.LeftAxisMaxType.name) || 'auto';
                this.$container.find('#setup-control-left-max-value-type-' + leftAxisMaxType).prop('checked', true);
                $leftAxisMaxType.change(function (event) {
                    _this.radioValueChanged(event, $leftAxisMax, Settings.Gadget.LeftAxisMaxType.name);
                });
                this.$container.find('.setup-control-left-max-value-type:checked').change();
                var $leftAxisMin = this.$container.find('#setup-control-LeftAxisMin-input');
                var leftAxisMin = this.setting(Settings.Gadget.LeftAxisMin.name);
                $leftAxisMin.val(i18n.formatNumber(leftAxisMin) || '0');
                var $leftAxisMinType = this.$container.find('.setup-control-left-min-value-type');
                var leftAxisMinType = this.setting(Settings.Gadget.LeftAxisMinType.name) || 'fixed';
                this.$container.find('#setup-control-left-min-value-type-' + leftAxisMinType).prop('checked', true);
                $leftAxisMinType.change(function (event) {
                    _this.radioValueChanged(event, $leftAxisMin, Settings.Gadget.LeftAxisMinType.name);
                });
                this.$container.find('.setup-control-left-min-value-type:checked').change();
                var $leftAxisType = this.$container.find('#setup-control-LeftAxisType-input');
                var leftAxisType = this.setting(Settings.Gadget.LeftAxisType.name);
                $leftAxisType.val(leftAxisType);
                $leftAxisType.change(function (event) {
                    _this.stringValueChanged(event, Settings.Gadget.LeftAxisType.name);
                });
                // Right Axis (if applicable)
                var $rightAxisType = this.$container.find('#setup-control-RightAxisType-input');
                var rightAxisType = this.setting(Settings.Gadget.RightAxisType.name);
                if (rightAxisType === 'none') {
                    this.$container.find('.basicChart_SetupControl .chartRightAxisWrapper').hide();
                }
                else {
                    $rightAxisType.val(rightAxisType);
                    $rightAxisType.change(function (event) {
                        _this.stringValueChanged(event, Settings.Gadget.RightAxisType.name);
                    });
                    var $rightAxisMax = this.$container.find('#setup-control-RightAxisMax-input');
                    var rightAxisMax = this.setting(Settings.Gadget.RightAxisMax.name);
                    $rightAxisMax.val(i18n.formatNumber(rightAxisMax) || '0');
                    var $rightAxisMaxType = this.$container.find('.setup-control-right-max-value-type');
                    var rightAxisMaxType = this.setting(Settings.Gadget.RightAxisMaxType.name) || 'auto';
                    this.$container.find('#setup-control-right-max-value-type-' + rightAxisMaxType).prop('checked', true);
                    $rightAxisMaxType.change(function (event) {
                        _this.radioValueChanged(event, $rightAxisMax, Settings.Gadget.RightAxisMaxType.name);
                    });
                    this.$container.find('.setup-control-right-max-value-type:checked').change();
                    var $rightAxisMin = this.$container.find('#setup-control-RightAxisMin-input');
                    var rightAxisMin = this.setting(Settings.Gadget.RightAxisMin.name);
                    $rightAxisMin.val(i18n.formatNumber(rightAxisMin) || '0');
                    var $rightAxisMinType = this.$container.find('.setup-control-right-min-value-type');
                    var rightAxisMinType = this.setting(Settings.Gadget.RightAxisMinType.name) || 'fixed';
                    this.$container.find('#setup-control-right-min-value-type-' + rightAxisMinType).prop('checked', true);
                    $rightAxisMinType.change(function (event) {
                        _this.radioValueChanged(event, $rightAxisMin, Settings.Gadget.RightAxisMinType.name);
                    });
                    this.$container.find('.setup-control-right-min-value-type:checked').change();
                    var $rightTitle = this.$container.find('#setup-control-RightAxisTitle-input');
                    var rightTitle = this.setting(Settings.Gadget.RightAxisTitle.name);
                    $rightTitle.val(rightTitle);
                    $rightTitle.change(function (event) {
                        _this.stringValueChanged(event, Settings.Gadget.RightAxisTitle.name);
                    });
                }
            };
            ChartAxis.prototype.radioValueChanged = function (event, $controlToDisable, settingName) {
                this.setting(settingName, $(event.delegateTarget).val());
                if (!$controlToDisable) {
                    return;
                }
                if ($(event.delegateTarget).val() === 'auto') {
                    $controlToDisable.attr("disabled", "disabled");
                }
                else {
                    $controlToDisable.removeAttr("disabled");
                }
                $controlToDisable.change(); // Capture current value.
            };
            ChartAxis.SetupControlTemplateUri = 'Content/Gadgets/_baseSetupControls/ChartAxisSetupControlTemplate.html';
            return ChartAxis;
        }(Dashboards.Models.SetupControl));
        SetupControl.ChartAxis = ChartAxis;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.ChartAxis.CreateInstance,
    id: Settings.Gadget.SettingGroup.ChartAxis
});
//# sourceMappingURL=ChartAxisSetupControl.js.map