var g_deviceName = JSON.parse(sessionStorage.getItem("configData"));
var g_status_settings = [];
var selectedMetric = "I"


angular.module('statusModule', [])
    .controller('statusController', function ($scope, $filter, $timeout) {
        $scope.headerTitle = "Current Settings";
        $scope.ConfigureStatusSettings = function ConfigureStatusSettings() {
            var request = new Request();
            request.url = '../devScripts/pilotUIConfig.xml';
            request.parameters = {};
            InvokePythonService(request, "xml").then(function (response) {
                var selectedDevice = $(response).find("Device[code=" + g_deviceName + "]");
                $scope.GetCurrentSettings(selectedDevice);
                $scope.GetLEDSettings(selectedDevice);
            });
        }

        $scope.GetCurrentSettings = function (selectedDevice) {
            var request = new Request();
            request.url = '../cgi/cgi_statusPageManager.py';
            request.parameters = { "requestType": "read_currentSettings" };
            InvokePythonService(request).then(function (response) {
                $scope.BindSettings(selectedDevice, response[0])
            });
        }

        $scope.BindSettings = function (selectedDevice, dbData) {
            var statusPage = selectedDevice.find('Page[name="status"]');
            statusPage.find('setting').each(function () {
                var setting = {
                    name: $(this).attr('name'),
                    description: $(this).attr('description'),
                    guiName: $(this).attr('gui_name'),
                    dbName: $(this).attr('db_name'),
                    dbValue: dbData[$(this).attr('db_name')]
                }
                g_status_settings.push(setting);
            });
            $scope.currentSettings = g_status_settings;
            $scope.$apply()
        };

        $scope.GetLEDSettings = function (selectedDevice) {
            var request = new Request();
            request.url = '../cgi/cgi_Setup_ModuleControl.py';
            request.parameters = {};
            InvokePythonService(request).then(function (response) {
                $scope.BindLED(selectedDevice, response)
            });
        }

        $scope.BindLED = function (selectedDevice, dbData) {
            var ledSettings = [];
            selectedDevice.find('led').each(function () {
                var settingItem = {
                    name: $(this).attr('name'),
                    status: String($(this).attr('db_value')) == "true",
                    imgPath: $(this).attr('imagePath')
                }
                ledSettings.push(settingItem);
            });
            $scope.ledSettings = ledSettings;
            $scope.$apply()
        }
        $scope.ConfigureStatusSettings();
    });

$('.clsChartType').change(function () {
    selectedMetric = $(this).val();
    ShowChart();
});

function ShowChart() {
    var scale = $("input[name='chartSclae']:checked").val();
    InitializeChart(g_deviceName, selectedMetric, scale);
}
$('.clsChartScale').change(function(){
	ShowChart();
});