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) {
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            TargetLines: "d720b45e-fa56-4e85-b9a0-d6b27c2eda9f"
        });
        Gadget.TargetLineData = { id: Gadget.SettingGroup.TargetLines, name: "TargetLineType" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var TargetLines = (function (_super) {
            __extends(TargetLines, _super);
            function TargetLines() {
                _super.call(this, Settings.Gadget.SettingGroup.TargetLines);
                this.title = i18n.loc.s('Gadgets', 'TargetLineControl_Title');
            }
            TargetLines.CreateInstance = function () {
                var instance = new TargetLines();
                return instance;
            };
            TargetLines.prototype.draw = function () {
                var _this = this;
                TemplateUtil.LocAndLoad(TargetLines.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.empty();
                    _this.$container.html(template);
                    _this.$targetLineTemplate = _this.$container.find('.targetLineEntryTemplate');
                    _this.$targetLineEntriesTemplate = _this.$container.find('.tagetLineEntries');
                    _this.render();
                }, this));
            };
            TargetLines.prototype.isConfigured = function () {
                return true;
            };
            TargetLines.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.TargetLineData.name:
                        managed = true;
                        break;
                }
                ;
                return managed;
            };
            TargetLines.prototype.destroy = function () {
                var $targetLineEntries = $(this.$container.find('.targetLineEntry'));
                _.each($targetLineEntries, function (targetEntry) {
                    var colorPicker = $(targetEntry).data('color');
                    if (colorPicker) {
                        colorPicker.destroy();
                    }
                });
                _super.prototype.destroy.call(this);
            };
            // If the control performs validation expose the state here.
            TargetLines.prototype.isValid = function () {
                var _this = this;
                var isValid = this.$container.find('.' + Dashboards.Models.SetupControl.ValidationErrorClass).length == 0;
                var validatedTargetLines = [];
                if (isValid) {
                    var $targetLineEntries = $(this.$container.find('.targetLineEntry'));
                    // "every" will loop through and run the callback until the
                    // callback returns false
                    isValid = _.every($targetLineEntries, function (targetLineEntry) {
                        var targetLine = _this.targetLineIsValid($(targetLineEntry));
                        if (!targetLine) {
                            return false;
                        }
                        validatedTargetLines.push(targetLine);
                        return true;
                    });
                }
                if (isValid) {
                    this.setting(Settings.Gadget.TargetLineData.name, JSON.stringify(validatedTargetLines));
                }
                return isValid;
            };
            TargetLines.prototype.render = function () {
                var _this = this;
                var newTargetLine = {
                    TargetLineType: Dashboards.Models.TargetLineType.Fixed,
                    TargetLineValue: 0,
                    TargetLineLabel: '',
                    TargetLineColor: '',
                    TargetLineLocation: 'left'
                };
                var $buttonAdd = this.$container.find('.buttonAddTargetLine');
                $buttonAdd.click(function (event) {
                    _this.addTargetLine(newTargetLine);
                    _this.updateUI();
                });
                var targetLineDataString = this.setting(Settings.Gadget.TargetLineData.name);
                if (!targetLineDataString)
                    return;
                var targetLineData = JSON.parse(targetLineDataString);
                _.each(targetLineData, function (targetLine) {
                    _this.addTargetLine(targetLine);
                });
                this.updateUI();
            };
            TargetLines.prototype.addTargetLine = function (targetLine) {
                var _this = this;
                var $targetLineEntry = this.$targetLineTemplate.clone();
                var $populatedTargetLine = $(TemplateUtil.ApplyLocalModel('targetLineEntryTemplate', $targetLineEntry.html(), targetLine));
                var targetColorPickerOptions = {
                    $container: $populatedTargetLine.find('.targetLineColorSelect'),
                    color: targetLine.TargetLineColor,
                    palette: Dashboards.Views.Application.DataSeriesSpectrum,
                    localStorageKey: "TargetLine"
                };
                var targetColorPicker = new Controls.ColorPicker.ColorPickerControl(targetColorPickerOptions, new Core.CallBack(function () { }));
                $populatedTargetLine.data('color', targetColorPicker);
                var $buttonRemove = $populatedTargetLine.find('.buttonRemoveTargetLine');
                $buttonRemove.click(function (event) {
                    var targetColorPicker = $(event.delegateTarget).parents('.targetLineEntry').data('color');
                    if (targetColorPicker) {
                        targetColorPicker.destroy();
                    }
                    $(event.delegateTarget).parents('.targetLineEntry').remove();
                    _this.updateUI();
                });
                if (targetLine) {
                    var $targetLineType = $populatedTargetLine.find('.targetLineType');
                    $targetLineType.val(targetLine.TargetLineType);
                    var $targetLineValue = $populatedTargetLine.find('.targetLineValue');
                    $targetLineValue.val(i18n.formatNumber(targetLine.TargetLineValue));
                    var $targetLineLocation = $populatedTargetLine.find('.targetLineLocation');
                    $targetLineLocation.val(targetLine.TargetLineLocation);
                }
                $populatedTargetLine.appendTo(this.$targetLineEntriesTemplate);
            };
            TargetLines.prototype.targetLineIsValid = function ($targetLineEntry) {
                var $targetLineType = $targetLineEntry.find('.targetLineType');
                var $targetLineValue = $targetLineEntry.find('#setup-control-TargetLineValue-input');
                var targetLineVal = NumberUtil.parseUserInput($targetLineValue.val());
                if (targetLineVal === '') {
                    this.addValidationMessage($targetLineValue, i18n.loc.s('General', 'ValidationWarning_InvalidDecimal'));
                    return null;
                }
                var $targetLineLabel = $targetLineEntry.find('#setup-control-TargetLineLabel-input');
                var validationResult = Validation.Text.validateUserText($targetLineLabel, { allowEmpty: true });
                if (!validationResult.valid) {
                    this.addValidationMessage($targetLineLabel, validationResult.message);
                    return null;
                }
                var $colorPickerInput = $targetLineEntry.find('.targetLineColorSelect');
                var newColor = $targetLineEntry.data('color');
                var $targetLineLocation = $targetLineEntry.find('.targetLineLocation');
                var targetLine = {
                    TargetLineType: parseInt($targetLineType.val()),
                    TargetLineColor: newColor.getColor(),
                    TargetLineLabel: $targetLineLabel.val(),
                    TargetLineValue: targetLineVal,
                    TargetLineLocation: $targetLineLocation.val()
                };
                return targetLine;
            };
            TargetLines.prototype.updateUI = function () {
                if (this.$container.find('.targetLineEntry').length === 0) {
                    this.$container.find('.tagetLineEntries').hide();
                }
                else {
                    this.$container.find('.tagetLineEntries').show();
                }
            };
            TargetLines.SetupControlTemplateUri = 'Content/Gadgets/_baseSetupControls/TargetLineSetupControlTemplate.html';
            return TargetLines;
        }(Dashboards.Models.SetupControl));
        SetupControl.TargetLines = TargetLines;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.TargetLines.CreateInstance,
    id: Settings.Gadget.SettingGroup.TargetLines
});
//# sourceMappingURL=TargetLineSetupControl.js.map