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, {
            PQGroupSelectSetup: "3d4d7e16-71a0-439d-b2ba-f290d375cbb7"
        });
        Gadget.PQGroupId = { id: Gadget.SettingGroup.PQGroupSelectSetup, name: "PQGroupId" };
    })(Gadget = Settings.Gadget || (Settings.Gadget = {}));
})(Settings || (Settings = {}));
var Dashboards;
(function (Dashboards) {
    var SetupControl;
    (function (SetupControl) {
        var PQGroupSelectSetup = (function (_super) {
            __extends(PQGroupSelectSetup, _super);
            function PQGroupSelectSetup() {
                _super.call(this, Settings.Gadget.SettingGroup.PQGroupSelectSetup);
                this.$list = null;
                this.$rowEntryTemplate = null;
                this.title = i18n.loc.s(PQGroupSelectSetup.ResxName, 'PowerQualityGroupSelectSetupControl_Title');
            }
            PQGroupSelectSetup.CreateInstance = function () {
                var instance = new PQGroupSelectSetup();
                return instance;
            };
            PQGroupSelectSetup.prototype.draw = function () {
                var _this = this;
                this.$container.empty();
                TemplateUtil.LocAndLoad(PQGroupSelectSetup.SetupControlTemplateUri, new Core.CallBack(function (template) {
                    _this.$container.html(template);
                    _this.render();
                }, this));
            };
            PQGroupSelectSetup.prototype.isValid = function () {
                return true;
            };
            PQGroupSelectSetup.prototype.beforeCommit = function () {
                var pqGroup = this.isConfigured()
                    ? this.$list.find('.customListItemSelect').data().pqGroupId
                    : null;
                this.setting(Settings.Gadget.PQGroupId.name, pqGroup);
            };
            PQGroupSelectSetup.prototype.isConfigured = function () {
                // We can't configure if there are no groups.
                return PQGroupSelectSetup.pqGroupList.length > 0;
            };
            PQGroupSelectSetup.prototype.isManagedSetting = function (name) {
                var managed = false;
                switch (name) {
                    case Settings.Gadget.PQGroupId.name:
                        managed = true;
                        break;
                }
                ;
                return managed;
            };
            PQGroupSelectSetup.prototype.render = function () {
                this.$list = this.$container.find('.pqGroupTable');
                this.$rowEntryTemplate = this.$container.find('.pqGroupRowEntryTemplate');
                this.GetPqGroupList();
            };
            PQGroupSelectSetup.prototype.GetPqGroupList = function () {
                this.PopulatePqGroupList();
            };
            PQGroupSelectSetup.prototype.PopulatePqGroupList = function () {
                if (PQGroupSelectSetup.pqGroupList == null) {
                    return;
                }
                // the default value is "All Groups" by default, or whatever the user saved.
                var defaultValue = this.setting(Settings.Gadget.PQGroupId.name || '');
                var itemList = new Array();
                if (PQGroupSelectSetup.pqGroupList.length === 0) {
                    // display "must be configured" message
                    this.$container.find('.pqGroupsExist').hide();
                    this.$container.find('.noPqGroupMessage').show();
                }
                else if (PQGroupSelectSetup.pqGroupList.length === 1) {
                    // display the only item
                    this.$container.find('.noPqGroupMessage').hide();
                    this.$container.find('.pqGroupsExist').show();
                    itemList = [
                        {
                            id: PQGroupSelectSetup.pqGroupList[0],
                            name: PQGroupSelectSetup.pqGroupList[0]
                        }
                    ];
                }
                else {
                    // display an "All Groups" item first, then the rest below that
                    this.$container.find('.noPqGroupMessage').hide();
                    this.$container.find('.pqGroupsExist').show();
                    itemList.push({ id: '', name: i18n.loc.s(PQGroupSelectSetup.ResxName, 'AllPowerQualityGroups') });
                    _.each(PQGroupSelectSetup.pqGroupList, function (item) {
                        itemList.push({ id: item, name: item });
                    });
                }
                this.populateList(defaultValue, itemList);
            };
            // populate the list of PQ groups
            PQGroupSelectSetup.prototype.populateList = function (initialValue, pqGroupList) {
                var _this = this;
                this.$list.empty();
                _.each(pqGroupList, function (pqGroup) {
                    var $entryTemplate = _this.$rowEntryTemplate.clone();
                    var $populatedRowEntry = $(TemplateUtil.ApplyLocalModel('pqGroupRowEntryTemplate', $entryTemplate.html(), pqGroup));
                    $populatedRowEntry.data('pqGroupId', pqGroup.id);
                    if (initialValue === pqGroup.id) {
                        $populatedRowEntry.addClass('customListItemSelect');
                    }
                    if (pqGroup.id === '') {
                        $populatedRowEntry.addClass('pqGroupRowAllGroups');
                    }
                    $populatedRowEntry.click(function (eventObj) {
                        var pqGroupRow = $(eventObj.delegateTarget);
                        _this.setUIState(pqGroupRow);
                    });
                    _this.$list.append($populatedRowEntry);
                });
                // select the first item, or the one saved
                if (this.$list.find('.customListItemSelect').length === 0) {
                    this.setUIState(this.$list.find('.customListItem').first());
                }
            };
            PQGroupSelectSetup.prototype.setUIState = function ($pqGroupRow) {
                var $lastSelected = this.$list.find('.' + Dashboards.Views.Application.ClassCustomListItemSelected);
                if ($lastSelected) {
                    $lastSelected.removeClass(Dashboards.Views.Application.ClassCustomListItemSelected);
                }
                $pqGroupRow.addClass(Dashboards.Views.Application.ClassCustomListItemSelected);
                this.lastSelected = $pqGroupRow.data('pqGroupId');
            };
            PQGroupSelectSetup.LoadPqGroupList = function (callBack) {
                callBack = callBack || Core.CallBack.None;
                // This is only required for Dashboards, not Slideshows or anything else
                if (fwk.ApplicationName !== 'Dashboards') {
                    callBack.call();
                    return;
                }
                var pqGroupListUrl = fwk.url.getAppUrl("/data/pq/groups");
                $.getJSON(pqGroupListUrl)
                    .done(function (data) {
                    PQGroupSelectSetup.pqGroupList = data;
                    callBack.call();
                })
                    .fail(function (jqXHR, textStatus, errorThrown) {
                    var msg = i18n.loc.s(PQGroupSelectSetup.ResxName, 'PQGroupSetup_CannotLoadPQGroups');
                    fwk.logError(msg);
                    fwk.logAjaxError(jqXHR, textStatus, errorThrown);
                    // We call the callback, we do not want to hold up the entire application.  Dependants
                    // of PQ groups will fail hard.
                    PQGroupSelectSetup.pqGroupList = [];
                    callBack.call();
                });
            };
            PQGroupSelectSetup.SetupControlTemplateUri = 'Content/Gadgets/_baseSetupControls/PQGroupSelectSetupControlTemplate.html';
            PQGroupSelectSetup.ResxName = 'PowerQualityGadgets';
            return PQGroupSelectSetup;
        }(Dashboards.Models.SetupControl));
        SetupControl.PQGroupSelectSetup = PQGroupSelectSetup;
    })(SetupControl = Dashboards.SetupControl || (Dashboards.SetupControl = {}));
})(Dashboards || (Dashboards = {}));
// Register the Setup Control
Dashboards.Controllers.SetupControl.Register({
    create: Dashboards.SetupControl.PQGroupSelectSetup.CreateInstance,
    id: Settings.Gadget.SettingGroup.PQGroupSelectSetup
});
// Get the list of PQ Groups to display on the control
fwk.AddInitializeMethod(function () {
    var callback = new Core.CallBack(fwk.InitializeMethodComplete);
    Dashboards.SetupControl.PQGroupSelectSetup.LoadPqGroupList(callback);
});
//# sourceMappingURL=PQGroupSelectSetupControl.js.map