protected void OnGroupComboChanged(object sender, ComboBoxChangedEventArgs e)
        {
            if (e.activeText == "New group...")
            {
                CallWaterLevelGroupSettingsDialog(true);
            }
            else
            {
                groupName = e.activeText;

                analogSensorWidget.sensorCombo.comboList.Clear();
                if (groupName.IsNotEmpty())
                {
                    var groupsWaterLevelSensors = WaterLevel.GetAllWaterLevelSensorsForWaterLevelGroup(groupName);
                    analogSensorWidget.sensorCombo.comboList.AddRange(groupsWaterLevelSensors);
                    if (groupsWaterLevelSensors.Length > 0)
                    {
                        analogSensorWidget.sensorName = groupsWaterLevelSensors[0];
                    }
                    else
                    {
                        analogSensorWidget.sensorName = string.Empty;
                    }
                }
                analogSensorWidget.sensorCombo.comboList.Add("New level sensor...");
                analogSensorWidget.sensorCombo.activeIndex = 0;
                analogSensorWidget.sensorCombo.QueueDraw();

                switchCombo.comboList.Clear();
                if (groupName.IsNotEmpty())
                {
                    var groupsFloatSwitches = WaterLevel.GetAllFloatSwitchesForWaterLevelGroup(groupName);
                    switchCombo.comboList.AddRange(groupsFloatSwitches);
                    if (groupsFloatSwitches.Length > 0)
                    {
                        switchName = groupsFloatSwitches[0];
                    }
                    else
                    {
                        switchName = string.Empty;
                    }
                }
                switchCombo.comboList.Add("New switch...");
                switchCombo.activeIndex = 0;
                switchCombo.QueueDraw();
            }

            GetGroupData();
            analogSensorWidget.GetSensorData();
            GetSwitchData();
        }
        protected void CallFloatSwitchSettingsDialog(bool forceNew = false)
        {
            FloatSwitchSettings settings;

            if (switchName.IsNotEmpty() && !forceNew)
            {
                settings = (FloatSwitchSettings)Sensors.FloatSwitches.GetGadgetSettings(switchName);
            }
            else
            {
                settings = new FloatSwitchSettings();
            }
            var parent = Toplevel as Window;
            var s      = new FloatSwitchSettingsDialog(settings, parent);

            s.Run();
            var newSwitchName = s.floatSwitchName;
            var outcome       = s.outcome;

            if ((outcome == TouchSettingsOutcome.Modified) && (newSwitchName != switchName))
            {
                var index = switchCombo.comboList.IndexOf(switchName);
                switchCombo.comboList[index] = newSwitchName;
                switchName = newSwitchName;
            }
            else if (outcome == TouchSettingsOutcome.Added)
            {
                switchCombo.comboList.Insert(switchCombo.comboList.Count - 1, newSwitchName);
                switchCombo.activeText = newSwitchName;
                switchName             = newSwitchName;
            }
            else if (outcome == TouchSettingsOutcome.Deleted)
            {
                switchCombo.comboList.Remove(switchName);
                var groupsFloatSwitches = WaterLevel.GetAllFloatSwitchesForWaterLevelGroup(groupName);
                if (groupsFloatSwitches.Length > 0)
                {
                    switchName             = groupsFloatSwitches[0];
                    switchCombo.activeText = switchName;
                }
                else
                {
                    switchName = string.Empty;
                    switchCombo.activeIndex = 0;
                }
            }

            switchCombo.QueueDraw();
            GetSwitchData();
        }
        public WaterLevelWindow(params object[] options)
        {
            sceneTitle   = "Water Level";
            ExposeEvent += OnExpose;

            /******************************************************************************************************/
            /* Water Level Groups                                                                                 */
            /******************************************************************************************************/
            groupName = WaterLevel.firstWaterLevelGroup;

            if (options.Length >= 3)
            {
                var requestedGroup = options[2] as string;
                if (requestedGroup != null)
                {
                    if (WaterLevel.WaterLevelGroupNameExists(requestedGroup))
                    {
                        groupName = requestedGroup;
                    }
                }
            }

            var label = new TouchLabel();

            label.text          = "Groups";
            label.WidthRequest  = 118;
            label.textColor     = "seca";
            label.textSize      = 12;
            label.textAlignment = TouchAlignment.Left;
            Put(label, 37, 80);
            label.Show();

            label = new TouchLabel();
            label.WidthRequest  = 329;
            label.text          = "Level";
            label.textColor     = "grey3";
            label.textAlignment = TouchAlignment.Center;
            Put(label, 60, 155);
            label.Show();

            levelLabel = new TouchLabel();
            levelLabel.WidthRequest  = 329;
            levelLabel.textSize      = 20;
            levelLabel.textAlignment = TouchAlignment.Center;
            levelLabel.textRender.unitOfMeasurement = UnitsOfMeasurement.Inches;
            Put(levelLabel, 60, 120);
            levelLabel.Show();

            var globalSettingsBtn = new TouchButton();

            globalSettingsBtn.text = "\u2699";
            globalSettingsBtn.SetSizeRequest(30, 30);
            globalSettingsBtn.buttonColor         = "pri";
            globalSettingsBtn.ButtonReleaseEvent += OnGroupSettingsButtonReleaseEvent;
            Put(globalSettingsBtn, 358, 77);
            globalSettingsBtn.Show();

            /******************************************************************************************************/
            /* ATO Groups                                                                                         */
            /******************************************************************************************************/
            atoGroupName = AutoTopOff.firstAtoGroup;

            label           = new TouchLabel();
            label.text      = "ATO";
            label.textColor = "seca";
            label.textSize  = 12;
            Put(label, 37, 280);
            label.Show();

            atoStateTextBox = new TouchLabel();
            atoStateTextBox.WidthRequest  = 329;
            atoStateTextBox.textSize      = 20;
            atoStateTextBox.textAlignment = TouchAlignment.Center;
            Put(atoStateTextBox, 60, 320);
            atoStateTextBox.Show();

            var atoSettingsBtn = new TouchButton();

            atoSettingsBtn.text = "\u2699";
            atoSettingsBtn.SetSizeRequest(30, 30);
            atoSettingsBtn.buttonColor         = "pri";
            atoSettingsBtn.ButtonReleaseEvent += OnAtoSettingsButtonReleaseEvent;
            Put(atoSettingsBtn, 358, 277);
            atoSettingsBtn.Show();

            atoClearFailBtn = new TouchButton();
            atoClearFailBtn.SetSizeRequest(100, 60);
            atoClearFailBtn.text                = "Reset ATO";
            atoClearFailBtn.buttonColor         = "compl";
            atoClearFailBtn.ButtonReleaseEvent += (o, args) => {
                if (atoGroupName.IsNotEmpty())
                {
                    if (AutoTopOff.GetAtoGroupState(atoGroupName) == AutoTopOffState.Cooldown)
                    {
                        AutoTopOff.ResetCooldownTime(atoGroupName);
                    }
                    else
                    {
                        if (!AutoTopOff.ClearAtoAlarm(atoGroupName))
                        {
                            MessageBox.Show("Please acknowledge alarms first");
                        }
                    }
                }
            };
            Put(atoClearFailBtn, 70, 405);

            /**************************************************************************************************************/
            /* Analog water sensor                                                                                        */
            /**************************************************************************************************************/
            analogSensorWidget = new WaterLevelSensorWidget();
            Put(analogSensorWidget, 415, 77);
            analogSensorWidget.Show();

            analogSensorWidget.sensorCombo.comboList.Clear();
            if (groupName.IsNotEmpty())
            {
                var groupsWaterLevelSensors = WaterLevel.GetAllWaterLevelSensorsForWaterLevelGroup(groupName);
                analogSensorWidget.sensorCombo.comboList.AddRange(groupsWaterLevelSensors);
                if (groupsWaterLevelSensors.Length > 0)
                {
                    analogSensorWidget.sensorName = groupsWaterLevelSensors[0];
                }
                else
                {
                    analogSensorWidget.sensorName = string.Empty;
                }
            }
            analogSensorWidget.sensorCombo.comboList.Add("New level sensor...");
            analogSensorWidget.sensorCombo.activeIndex = 0;
            analogSensorWidget.sensorCombo.QueueDraw();

            /**************************************************************************************************************/
            /* Float Switches                                                                                             */
            /**************************************************************************************************************/
            label           = new TouchLabel();
            label.text      = "Probes";
            label.textColor = "seca";
            label.textSize  = 12;
            Put(label, 415, 280);
            label.Show();

            switchStateLabel               = new TouchLabel();
            switchStateLabel.text          = "Current Switch State";
            switchStateLabel.textAlignment = TouchAlignment.Center;
            switchStateLabel.textColor     = "grey3";
            switchStateLabel.WidthRequest  = 370;
            Put(switchStateLabel, 415, 355);

            switchStateTextBox = new TouchLabel();
            switchStateTextBox.WidthRequest  = 370;
            switchStateTextBox.textSize      = 20;
            switchStateTextBox.textAlignment = TouchAlignment.Center;
            Put(switchStateTextBox, 415, 320);
            switchStateTextBox.Show();

            //Type
            switchTypeLabel = new TouchLabel();
            switchTypeLabel.WidthRequest  = 370;
            switchTypeLabel.textAlignment = TouchAlignment.Center;
            switchTypeLabel.textColor     = "grey3";
            Put(switchTypeLabel, 415, 370);
            switchTypeLabel.Show();

            var switchSetupBtn = new TouchButton();

            switchSetupBtn.text = "\u2699";
            switchSetupBtn.SetSizeRequest(30, 30);
            switchSetupBtn.buttonColor         = "pri";
            switchSetupBtn.ButtonReleaseEvent += OnFloatSwitchSettingsButtonReleaseEvent;
            Put(switchSetupBtn, 755, 277);
            switchSetupBtn.Show();

            groupCombo = new TouchComboBox(WaterLevel.GetAllWaterLevelGroupNames());
            if (groupName.IsNotEmpty())
            {
                groupCombo.activeText = groupName;
            }
            else
            {
                groupCombo.activeIndex = 0;
            }
            groupCombo.WidthRequest = 200;
            groupCombo.comboList.Add("New group...");
            groupCombo.ComboChangedEvent += OnGroupComboChanged;
            Put(groupCombo, 153, 77);
            groupCombo.Show();

            atoGroupCombo = new TouchComboBox(AutoTopOff.GetAllAtoGroupNames());
            if (atoGroupName.IsNotEmpty())
            {
                atoGroupCombo.activeText = atoGroupName;
            }
            else
            {
                atoGroupCombo.activeIndex = 0;
            }
            atoGroupCombo.WidthRequest = 200;
            atoGroupCombo.comboList.Add("New ATO...");
            atoGroupCombo.ComboChangedEvent += OnAtoGroupComboChanged;
            Put(atoGroupCombo, 153, 277);
            atoGroupCombo.Show();

            switchCombo = new TouchComboBox();
            switchCombo.WidthRequest = 200;
            if (groupName.IsNotEmpty())
            {
                var groupsFloatSwitches = WaterLevel.GetAllFloatSwitchesForWaterLevelGroup(groupName);
                switchCombo.comboList.AddRange(groupsFloatSwitches);
                if (groupsFloatSwitches.Length > 0)
                {
                    switchName = groupsFloatSwitches[0];
                }
                else
                {
                    switchName = string.Empty;
                }
            }
            switchCombo.comboList.Add("New switch...");
            switchCombo.activeIndex        = 0;
            switchCombo.ComboChangedEvent += OnSwitchComboChanged;
            Put(switchCombo, 550, 277);
            switchCombo.Show();

            GetGroupData();
            GetAtoGroupData();
            analogSensorWidget.GetSensorData();
            GetSwitchData();

            Show();
        }
        protected void CallWaterLevelGroupSettingsDialog(bool forceNew = false)
        {
            WaterLevelGroupSettings settings;

            if (groupName.IsNotEmpty() && !forceNew)
            {
                settings = WaterLevel.GetWaterLevelGroupSettings(groupName);
            }
            else
            {
                settings = new WaterLevelGroupSettings();
            }

            var parent = Toplevel as Window;
            var s      = new WaterGroupSettings(WaterLevel.GetWaterLevelGroupSettings(groupName), parent);

            s.Run();
            var newGroupName = s.groupName;
            var outcome      = s.outcome;

            if ((outcome == TouchSettingsOutcome.Modified) && (newGroupName != groupName))
            {
                var index = groupCombo.comboList.IndexOf(groupName);
                groupCombo.comboList[index] = newGroupName;
                groupCombo.activeText       = newGroupName;
            }
            else if (outcome == TouchSettingsOutcome.Added)
            {
                groupName = newGroupName;
                groupCombo.comboList.Insert(groupCombo.comboList.Count - 1, groupName);
                groupCombo.activeText = groupName;

                analogSensorWidget.sensorCombo.comboList.Clear();
                analogSensorWidget.sensorCombo.comboList.Add("New level sensor...");
                analogSensorWidget.sensorCombo.activeIndex = 0;
                analogSensorWidget.sensorCombo.QueueDraw();
                analogSensorWidget.sensorName = string.Empty;

                switchCombo.comboList.Clear();
                switchCombo.comboList.Add("New switch...");
                switchCombo.activeIndex = 0;
                switchCombo.QueueDraw();
                switchName = string.Empty;
            }
            else if (outcome == TouchSettingsOutcome.Deleted)
            {
                groupCombo.comboList.Remove(groupName);
                groupName             = WaterLevel.firstWaterLevelGroup;
                groupCombo.activeText = groupName;

                analogSensorWidget.sensorCombo.comboList.Clear();
                if (groupName.IsNotEmpty())
                {
                    var groupsWaterLevelSensors = WaterLevel.GetAllWaterLevelSensorsForWaterLevelGroup(groupName);
                    analogSensorWidget.sensorCombo.comboList.AddRange(groupsWaterLevelSensors);
                    if (groupsWaterLevelSensors.Length > 0)
                    {
                        analogSensorWidget.sensorName = groupsWaterLevelSensors[0];
                    }
                    else
                    {
                        analogSensorWidget.sensorName = string.Empty;
                    }
                }
                analogSensorWidget.sensorCombo.comboList.Add("New level sensor...");
                analogSensorWidget.sensorCombo.activeIndex = 0;
                analogSensorWidget.sensorCombo.QueueDraw();

                switchCombo.comboList.Clear();
                if (groupName.IsNotEmpty())
                {
                    var groupsFloatSwitches = WaterLevel.GetAllFloatSwitchesForWaterLevelGroup(groupName);
                    switchCombo.comboList.AddRange(groupsFloatSwitches);
                    if (groupsFloatSwitches.Length > 0)
                    {
                        switchName = groupsFloatSwitches[0];
                    }
                    else
                    {
                        switchName = string.Empty;
                    }
                }
                switchCombo.comboList.Add("New switch...");
                switchCombo.activeIndex = 0;
                switchCombo.QueueDraw();
            }

            groupCombo.QueueDraw();
            GetGroupData();
            analogSensorWidget.GetSensorData();
            GetSwitchData();
        }