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 __());
};
// Simple SourceId Setup Control
var Settings;
(function (Settings) {
    var Gadget;
    (function (Gadget) {
        // SettingGroup.SourceSelection
        Gadget.SettingGroup = $.extend(Gadget.SettingGroup, {
            SingleSourceSelection: "b8c97c16-069b-4436-b81d-7013b0bcdf94"
        });
        Gadget.SourceId = { id: Gadget.SettingGroup.SingleSourceSelection, name: "SourceId" };
        Gadget.SourceDisplayName = { id: Gadget.SettingGroup.SingleSourceSelection, name: "SourceDisplayName" };
        Gadget.SelectionType = { id: Gadget.SettingGroup.SingleSourceSelection, name: "SelectionType" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var SingleSourceSelection = (function (_super) {
            __extends(SingleSourceSelection, _super);
            function SingleSourceSelection() {
                _super.call(this, Settings.Gadget.SettingGroup.SingleSourceSelection);
                this.title = i18n.loc.s('Gadgets', 'SingleSourceControl_Title');
            }
            SingleSourceSelection.CreateInstance = function () {
                var instance = new SingleSourceSelection();
                return instance;
            };
            SingleSourceSelection.prototype.draw = function () {
                var _this = this;
                this.$container.empty();
                TemplateUtil.LocAndLoad(SingleSourceSelection.setupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.html(template);
                    _this.render();
                }, this));
            };
            // If the control performs validation expose the state here.
            SingleSourceSelection.prototype.isValid = function () {
                return true; // If you require a forced selection, please implement.
            };
            ;
            // When the control has a "live" configuration that will allow
            // correct operation return true from this function. 
            SingleSourceSelection.prototype.isConfigured = function () {
                // TODO - How do we know if we require a selection?
                //var gadget = $.grep(GadgetConfigurationDialog.controls, function (el, i) {
                //    return el.id === Settings.Gadget.SettingGroup.SourceSelection;
                //})[0];
                //var sourceId = gadget.setting(Settings.Gadget.SourceId.name);
                //var isConfigured = sourceId && sourceId.length > 0;
                //return isConfigured && this.ready;
                return true;
            };
            SingleSourceSelection.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.SourceId.name:
                    case Settings.Gadget.SourceDisplayName.name:
                    case Settings.Gadget.SelectionType.name:
                        managed = true;
                        break;
                }
                ;
                return managed;
            };
            /* Private methods
            /*********************************************************************/
            SingleSourceSelection.prototype.updateSourceSelection = function (selectedSource) {
                this.setting(Settings.Gadget.SourceId.name, selectedSource.DeviceId);
                this.setting(Settings.Gadget.SourceDisplayName.name, selectedSource.DisplayName);
                SingleSourceSelection._updateSourceDisplay(this.$container.find('.selectedSourceDisplay'), selectedSource);
            };
            SingleSourceSelection.prototype.render = function () {
                var _this = this;
                this.$container.find('.selectSourceButton').click(function () {
                    var sourceId = _this.setting(Settings.Gadget.SourceId.name);
                    var selectionType = _this.setting(Settings.Gadget.SelectionType.name);
                    SetupControl.SingleSourceSelectSetupControlDialog.ShowDialog(sourceId, selectionType, new Core.CallBackPayload(function (valid, selectedSource) {
                        if (valid) {
                            _this.updateSourceSelection(selectedSource);
                            _this.$container.find('.clearSelectionButton').show();
                        }
                    }));
                });
                this.$container.find('.clearSelectionButton').click(function () {
                    _this.setting(Settings.Gadget.SourceId.name, '');
                    _this.setting(Settings.Gadget.SourceDisplayName.name, '');
                    _this.$container.find('.selectedSourceDisplay').html(i18n.loc.s('Gadgets', 'SingleSourceControl_NoSourceSelected'));
                    _this.$container.find('.clearSelectionButton').hide();
                });
                var sourceId = this.setting(Settings.Gadget.SourceId.name);
                if (sourceId) {
                    var handle = new WaitHandle.PollingHandle({
                        onCompleteCallback: new Core.CallBack(function () {
                            if (!_this.$container)
                                return; // Control closed?
                            var currentSourceId = _this.setting(Settings.Gadget.SourceId.name);
                            var source = Controls.Smp.SmpSelector.getSource(currentSourceId);
                            SingleSourceSelection._updateSourceDisplay(_this.$container.find('.selectedSourceDisplay'), source);
                            handle = null;
                        }),
                        testForComplete: function () {
                            return Controls.Smp.SmpSelector.isDataLoaded();
                        },
                        testIntervalMs: 500
                    });
                    handle.Start();
                    this.$container.find('.clearSelectionButton').show();
                }
                else {
                    // no selection
                    this.$container.find('.selectedSourceDisplay').html(i18n.loc.s('Gadgets', 'SingleSourceControl_NoSourceSelected'));
                    this.$container.find('.clearSelectionButton').hide();
                }
            };
            SingleSourceSelection._updateSourceDisplay = function ($element, source) {
                var displayName = source ? source.DisplayName : i18n.loc.s('Gadgets', 'SingleSourceControl_NoSourceSelected');
                $element.html(displayName.encodeHtml());
            };
            SingleSourceSelection.setupControlTemplateUri = 'Content/Gadgets/_baseSetupControls/SingleSourceSelectSetupControlTemplate.html';
            return SingleSourceSelection;
        }(Dashboards.Models.SetupControl));
        SetupControl.SingleSourceSelection = SingleSourceSelection;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.SingleSourceSelection.CreateInstance,
    id: Settings.Gadget.SettingGroup.SingleSourceSelection
});
//# sourceMappingURL=SingleSourceSelectSetupControl.js.map