private void AdapterListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ControlsStkPnl.Children.Clear();

            var adapter = (Adapter)AdapterListView.SelectedItem;
            if (adapter == null) return;
            //ADD THE ENABLED BUTTON
            var c = new CheckboxControl(
                async isChecked =>
                {
                    //Save to the database
                    adapter.IsEnabled = isChecked;

                    var result = await Context.TrySaveChangesAsync(App.Cts.Token);
                    if (result.HasError)
                        await Log.ReportErrorFormatAsync(App.Cts.Token, "Error enabling adapter. {0}", result.Message);

                    //STOP OR START
                    if (isChecked)
                        await App.ZvsEngine.AdapterManager.EnableAdapterAsync(adapter.AdapterGuid, App.Cts.Token);
                    else
                        await App.ZvsEngine.AdapterManager.DisableAdapterAsync(adapter.AdapterGuid, App.Cts.Token);
                },
                                        _icon)
            {
                Header = string.Format("{0} is enabled", adapter.Name),
                Description = "Starts and stops the selected adapter",
                Value = adapter.IsEnabled
            };
            ControlsStkPnl.Children.Add(c);


            //Add all the settings
            foreach (var a in adapter.Settings)
            {
                var adapterSetting = a;

                switch (adapterSetting.ValueType)
                {
                    case DataType.BOOL:
                        {
                            bool defaultValue;
                            bool.TryParse(adapterSetting.Value, out defaultValue);

                            var control = new CheckboxControl(async isChecked =>
                                {
                                    adapterSetting.Value = isChecked.ToString();
                                    var result = await Context.TrySaveChangesAsync(App.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(App.Cts.Token, "Error saving adapter setting. {0}", result.Message);
                                },
                                        _icon)
                            {
                                Header = adapterSetting.Name,
                                Description = adapterSetting.Description,
                                Value = defaultValue
                            };
                            ControlsStkPnl.Children.Add(control);
                            break;
                        }
                    case DataType.BYTE:
                    case DataType.DECIMAL:
                    case DataType.SHORT:
                    case DataType.INTEGER:
                    case DataType.COMPORT:
                        {
                            var control = new NumericControl(async value =>
                                {
                                    adapterSetting.Value = value;
                                    var result = await Context.TrySaveChangesAsync(App.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(App.Cts.Token, "Error saving adapter setting. {0}", result.Message);
                                },
                                        _icon, adapterSetting.ValueType)
                            {
                                Header = adapterSetting.Name,
                                Description = adapterSetting.Description,
                                Value = adapterSetting.Value
                            };
                            ControlsStkPnl.Children.Add(control);
                            break;
                        }
                    case DataType.STRING:
                        {
                            var control = new StringControl(
                                async value =>
                                {
                                    adapterSetting.Value = value;
                                    var result = await Context.TrySaveChangesAsync(App.Cts.Token);
                                    if (result.HasError)
                                        await
                                            Log.ReportErrorFormatAsync(App.Cts.Token,
                                                "Error saving adapter setting. {0}", result.Message);
                                },
                                _icon)
                            {
                                Header = adapterSetting.Name,
                                Description = adapterSetting.Description,
                                Value = adapterSetting.Value
                            };


                            ControlsStkPnl.Children.Add(control);
                            break;
                        }
                    case DataType.LIST:
                        {
                            var control = new ComboboxControl(async value =>
                                {
                                    adapterSetting.Value = value.ToString();
                                    var result = await Context.TrySaveChangesAsync(App.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(App.Cts.Token, "Error saving adapter setting. {0}", result.Message);
                                },
                                 _icon,
                             adapterSetting.Options.Select(o => o.Name).ToList())
                            {
                                Header = adapterSetting.Name,
                                Description = adapterSetting.Description,
                                SelectedItem = adapterSetting.Value
                            };
                            ControlsStkPnl.Children.Add(control);
                            break;
                        }
                }
            }
        }
        private async Task LoadCommandsAsync()
        {
            DeviceCommandsStkPnl.Children.Clear();
            TypeCommandsStkPnl.Children.Clear();

            var d = await _context.Devices
                .Include(o => o.Values)
                .Include(o => o.Type.Commands)
                .FirstOrDefaultAsync(dv => dv.Id == _deviceId);

            if (d != null)
            {
                #region Device Commands
                foreach (var dc in d.Commands.OrderByDescending(c => c.SortOrder))
                {
                    var deviceCommand = dc;
                    switch (deviceCommand.ArgumentType)
                    {
                        case DataType.NONE:
                            {
                                var bc = new ButtonControl(async () =>
                                {
                                    await _app.ZvsEngine.RunCommandAsync(deviceCommand.Id, string.Empty, string.Empty, CancellationToken.None);
                                }, _icon)
                                {
                                    Header = deviceCommand.Name,
                                    ButtonContent = deviceCommand.Name,
                                    Description = deviceCommand.Description
                                };
                                DeviceCommandsStkPnl.Children.Add(bc);
                                break;
                            }
                        case DataType.BOOL:
                            {
                                //get the current value from the value table list
                                var defaultValue = false;
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceCommand.CustomData2);
                                if (dv != null)
                                {
                                    bool.TryParse(dv.Value, out defaultValue);
                                }

                                var control = new CheckboxControl(async isChecked =>
                                {
                                    await _app.ZvsEngine.RunCommandAsync(deviceCommand.Id, isChecked.ToString(), string.Empty, CancellationToken.None);
                                },
                                    _icon)
                                {
                                    Header = deviceCommand.Name,
                                    Description = deviceCommand.Description,
                                    Value = defaultValue
                                };
                                DeviceCommandsStkPnl.Children.Add(control);

                                break;
                            }
                        case DataType.INTEGER:
                        case DataType.DECIMAL:
                        case DataType.BYTE:
                        case DataType.SHORT:
                            {
                                //get the current value from the value table list
                                var defaultValue = "0";
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceCommand.CustomData2);
                                if (dv != null)
                                    defaultValue = dv.Value;
                                var control = new NumericControl(async value =>
                                    {
                                        await _app.ZvsEngine.RunCommandAsync(deviceCommand.Id, value, string.Empty, CancellationToken.None);
                                    },
                                    _icon, deviceCommand.ArgumentType)
                                {
                                    Header = deviceCommand.Name,
                                    Description = deviceCommand.Description,
                                    Value = defaultValue
                                };
                                DeviceCommandsStkPnl.Children.Add(control);
                                break;
                            }
                        case DataType.STRING:
                            {
                                //get the current value from the value table list
                                var defaultValue = "0";
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceCommand.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }

                                var control = new StringControl(
                                    async value =>
                                    {
                                        await _app.ZvsEngine.RunCommandAsync(deviceCommand.Id, value, string.Empty, CancellationToken.None);
                                    },
                                        _icon)
                                {
                                    Header = deviceCommand.Name,
                                    Description = deviceCommand.Description,
                                    Value = defaultValue
                                };
                                DeviceCommandsStkPnl.Children.Add(control);

                                break;
                            }
                        case DataType.LIST:
                            {
                                //get the current value from the value table list
                                var defaultValue = "0";
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceCommand.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }

                                var control = new ComboboxControl(async value =>
                                    {
                                        await _app.ZvsEngine.RunCommandAsync(deviceCommand.Id, value.ToString(), string.Empty, CancellationToken.None);
                                    },
                            _icon,
                            deviceCommand.Options.Select(o => o.Name).ToList())
                                {
                                    Header = deviceCommand.Name,
                                    Description = deviceCommand.Description,
                                    SelectedItem = defaultValue
                                };
                                DeviceCommandsStkPnl.Children.Add(control);

                                break;
                            }
                    }
                }
                #endregion

                #region Device Type Commands
                foreach (var dtc in d.Type.Commands.OrderByDescending(c => c.SortOrder))
                {
                    var deviceTypeCommand = dtc;
                    switch (deviceTypeCommand.ArgumentType)
                    {
                        case DataType.NONE:
                            {
                                var bc = new ButtonControl(async () =>
                                {
                                    await _app.ZvsEngine.RunCommandAsync(deviceTypeCommand.Id, string.Empty, d.Id.ToString(CultureInfo.InvariantCulture), CancellationToken.None);
                                }, _icon)
                                {
                                    Header = deviceTypeCommand.Name,
                                    ButtonContent = deviceTypeCommand.Name,
                                    Description = deviceTypeCommand.Description
                                };
                                TypeCommandsStkPnl.Children.Add(bc);
                                break;
                            }
                        case DataType.BOOL:
                            {
                                //get the current value from the value table list
                                var defaultValue = false;
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceTypeCommand.CustomData2);
                                if (dv != null)
                                {
                                    bool.TryParse(dv.Value, out defaultValue);
                                }

                                var control = new CheckboxControl(async isChecked =>
                                {
                                    await _app.ZvsEngine.RunCommandAsync(deviceTypeCommand.Id, isChecked.ToString(), d.Id.ToString(CultureInfo.InvariantCulture), CancellationToken.None);
                                },
                                    _icon)
                                {
                                    Header = deviceTypeCommand.Name,
                                    Description = deviceTypeCommand.Description,
                                    Value = defaultValue
                                };
                                TypeCommandsStkPnl.Children.Add(control);

                                break;
                            }
                        case DataType.DECIMAL:
                        case DataType.INTEGER:
                        case DataType.SHORT:
                        case DataType.BYTE:
                            {
                                //get the current value from the value table list
                                var defaultValue = "0";
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceTypeCommand.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }

                                var control = new NumericControl(async value =>
                                   {
                                       await _app.ZvsEngine.RunCommandAsync(deviceTypeCommand.Id, value, d.Id.ToString(CultureInfo.InvariantCulture), CancellationToken.None);
                                   },
                                    _icon, deviceTypeCommand.ArgumentType)
                                {
                                    Header = deviceTypeCommand.Name,
                                    Description = deviceTypeCommand.Description,
                                    Value = defaultValue
                                };
                                TypeCommandsStkPnl.Children.Add(control);

                                break;
                            }
                        case DataType.STRING:
                            {
                                //get the current value from the value table list
                                var defaultValue = "0";
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceTypeCommand.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }

                                var control = new StringControl(
                                    async value =>
                                    {
                                        await _app.ZvsEngine.RunCommandAsync(deviceTypeCommand.Id, value, d.Id.ToString(CultureInfo.InvariantCulture), CancellationToken.None);
                                    },
                                        _icon)
                                {
                                    Header = deviceTypeCommand.Name,
                                    Description = deviceTypeCommand.Description,
                                    Value = defaultValue
                                };
                                TypeCommandsStkPnl.Children.Add(control);

                                break;
                            }
                        case DataType.LIST:
                            {
                                //get the current value from the value table list
                                var defaultValue = "0";
                                var dv = d.Values.FirstOrDefault(v => v.UniqueIdentifier == deviceTypeCommand.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }

                                var control = new ComboboxControl(async value =>
                                    {
                                        await _app.ZvsEngine.RunCommandAsync(deviceTypeCommand.Id, value.ToString(), d.Id.ToString(CultureInfo.InvariantCulture), CancellationToken.None);
                                    },
                                 _icon,
                                 deviceTypeCommand.Options.Select(o => o.Name).ToList())
                                {
                                    Header = deviceTypeCommand.Name,
                                    Description = deviceTypeCommand.Description,
                                    SelectedItem = defaultValue
                                };
                                TypeCommandsStkPnl.Children.Add(control);

                                break;
                            }
                    }
                }
                #endregion
            }
        }
        private async void DeviceCmdsCmboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            var selectedDevice = (Device)DevicesCmboBox.SelectedItem;
            DeviceArgSckPnl.Children.Clear();

            var deviceTypeCommand = DeviceCmdsCmboBox.SelectedItem as DeviceTypeCommand;
            if (deviceTypeCommand != null)
            {
                #region Device Type Commands
                switch (deviceTypeCommand.ArgumentType)
                {
                    case DataType.NONE:
                        {
                            DeviceArgSckPnl.Children.Add(new TextBlock
                            {
                                Text = "None",
                                Margin = new Thickness(30, 0, 0, 0)
                            });
                            break;
                        }
                    case DataType.BOOL:
                        {
                            //get the current value from the value table list
                            bool defaultValue;
                            if (!bool.TryParse(StoredCommand.Argument, out defaultValue))
                            {
                                var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == deviceTypeCommand.CustomData2);
                                if (dv != null)
                                {
                                    bool.TryParse(dv.Value, out defaultValue);
                                }
                            }
                            _selectedDeviceArg = defaultValue.ToString();

                            var control = new CheckboxControl(isChecked =>
                            {
                                _selectedDeviceArg = isChecked.ToString();
                                return Task.FromResult(0);
                            }, _icon)
                            {
                                Header = deviceTypeCommand.Name,
                                Description = deviceTypeCommand.Description,
                                Value = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);

                            break;
                        }
                    case DataType.DECIMAL:
                    case DataType.INTEGER:
                    case DataType.BYTE:
                    case DataType.SHORT:
                        {
                            //get the current value from the value table list
                            var defaultValue = "0";
                            var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == deviceTypeCommand.CustomData2);
                            if (dv != null)
                                defaultValue = dv.Value;
                            _selectedDeviceArg = defaultValue;
                            var control = new NumericControl(value =>
                                {
                                    _selectedDeviceArg = value;
                                    return Task.FromResult(0);
                                },
                                        _icon, deviceTypeCommand.ArgumentType)
                            {
                                Header = deviceTypeCommand.Name,
                                Description = deviceTypeCommand.Description,
                                Value = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);

                            break;
                        }
                    case DataType.STRING:
                        {
                            //get the current value from the value table list
                            var defaultValue = "0";
                            if (!string.IsNullOrEmpty(StoredCommand.Argument))
                            {
                                defaultValue = StoredCommand.Argument;
                            }
                            else
                            {
                                var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == deviceTypeCommand.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }
                            }
                            _selectedDeviceArg = defaultValue;
                            var control = new StringControl(
                                value =>
                                {
                                    _selectedDeviceArg = value;
                                    return Task.FromResult(0);
                                },
                                        _icon)
                            {
                                Header = deviceTypeCommand.Name,
                                Description = deviceTypeCommand.Description,
                                Value = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);

                            break;
                        }
                    case DataType.LIST:
                        {
                            //get the current value from the value table list
                            var defaultValue = "0";
                            if (!string.IsNullOrEmpty(StoredCommand.Argument))
                            {
                                defaultValue = StoredCommand.Argument;
                            }
                            else
                            {
                                var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == deviceTypeCommand.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }
                            }
                            _selectedDeviceArg = defaultValue;
                            var control = new ComboboxControl(value =>
                                {
                                    _selectedDeviceArg = value.ToString();
                                    return Task.FromResult(0);
                                },
                                _icon,
                                deviceTypeCommand.Options.Select(o => o.Name).ToList())
                            {
                                Header = deviceTypeCommand.Name,
                                Description = deviceTypeCommand.Description,
                                SelectedItem = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);

                            break;
                        }

                }
                #endregion
            }

            var item = DeviceCmdsCmboBox.SelectedItem as DeviceCommand;
            if (item != null)
            {
                var dCmd = item;
                #region Device Commands

                switch (dCmd.ArgumentType)
                {
                    case DataType.NONE:
                        {
                            DeviceArgSckPnl.Children.Add(new TextBlock
                            {
                                Text = "None",
                                Margin = new Thickness(30, 0, 0, 0)
                            });
                            break;
                        }
                    case DataType.BOOL:
                        {
                            //get the current value from the value table list
                            bool defaultValue;
                            if (!bool.TryParse(StoredCommand.Argument, out defaultValue))
                            {
                                var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == dCmd.CustomData2);
                                if (dv != null)
                                    bool.TryParse(dv.Value, out defaultValue);
                            }
                            _selectedDeviceArg = defaultValue.ToString();

                            var control = new CheckboxControl(isChecked =>
                            {
                                _selectedDeviceArg = isChecked.ToString();
                                return Task.FromResult(0);
                            }, _icon)
                            {
                                Header = dCmd.Name,
                                Description = dCmd.Description,
                                Value = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);

                            break;
                        }
                    case DataType.DECIMAL:
                    case DataType.INTEGER:
                    case DataType.BYTE:
                    case DataType.SHORT:
                        {
                            //get the current value from the value table list
                            var defaultValue = StoredCommand.Argument;
                            if (defaultValue == "0")
                            {
                                var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == dCmd.CustomData2);
                                if (dv != null)
                                    defaultValue = dv.Value;
                            }

                            _selectedDeviceArg = defaultValue;

                            var control = new NumericControl(value =>
                            {
                                _selectedDeviceArg = value;
                                return Task.FromResult(0);
                            },
                                        _icon, dCmd.ArgumentType)
                            {
                                Header = dCmd.Name,
                                Description = dCmd.Description,
                                Value = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);
                            break;
                        }
                    case DataType.STRING:
                        {
                            //get the current value from the value table list
                            var defaultValue = "";
                            if (!string.IsNullOrEmpty(StoredCommand.Argument))
                            {
                                defaultValue = StoredCommand.Argument;
                            }
                            else
                            {
                                var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == dCmd.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }
                            }
                            _selectedDeviceArg = defaultValue;
                            var control = new StringControl(
                                value =>
                                {
                                    _selectedDeviceArg = value;
                                    return Task.FromResult(0);
                                },
                                        _icon)
                            {
                                Header = dCmd.Name,
                                Description = dCmd.Description,
                                Value = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);

                            break;
                        }
                    case DataType.LIST:
                        {
                            //get the current value from the value table list
                            var defaultValue = "0";
                            if (!string.IsNullOrEmpty(StoredCommand.Argument))
                            {
                                defaultValue = StoredCommand.Argument;
                            }
                            else
                            {
                                var dv = await Context.DeviceValues.FirstOrDefaultAsync(o => o.DeviceId == selectedDevice.Id && o.UniqueIdentifier == dCmd.CustomData2);
                                if (dv != null)
                                {
                                    defaultValue = dv.Value;
                                }
                            }
                            _selectedDeviceArg = defaultValue;
                            var control = new ComboboxControl(value =>
                                {
                                    _selectedDeviceArg = value.ToString();
                                    return Task.FromResult(0);
                                },
                                _icon,
                                dCmd.Options.Select(o => o.Name).ToList())
                            {
                                Header = dCmd.Name,
                                Description = dCmd.Description,
                                SelectedItem = defaultValue
                            };
                            DeviceArgSckPnl.Children.Add(control);

                            break;
                        }
                }
                #endregion
            }
        }
        private async void BuiltinCmdsCmboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            BuiltinArgSckPnl.Children.Clear();
            var selectedCmd = (BuiltinCommand)BuiltinCmdsCmboBox.SelectedItem;

            switch (selectedCmd.UniqueIdentifier)
            {
                #region Do Custom things for some Builtin Commands
                case "REPOLL_ME":
                    {
                        Device selectedDevice = null;

                        //Lookup the device involved in the command
                        int deviceId = int.TryParse(StoredCommand.Argument, out deviceId) ? deviceId : 0;
                        var device = await Context.Devices.FirstOrDefaultAsync(o => o.Id == deviceId) ??
                                     await Context.Devices.FirstOrDefaultAsync();

                        if (device != null)
                        {
                            selectedDevice = device;
                            _selectedBuiltinArg = device.Id.ToString(CultureInfo.InvariantCulture);
                        }
                        var control = new ComboboxControl(obj =>
                            {
                                var d = obj as Device;
                                if (d != null)
                                    _selectedBuiltinArg = d.Id.ToString(CultureInfo.InvariantCulture);

                                return Task.FromResult(0);
                            },
                            _icon,
                            await Context.Devices.ToListAsync())
                        {
                            Header = selectedCmd.Name,
                            Description = selectedCmd.Description,
                            SelectedItem = selectedDevice
                        };
                        BuiltinArgSckPnl.Children.Add(control);
                        break;
                    }
                case "GROUP_ON":
                case "GROUP_OFF":
                    {
                        Group selectedGroup = null;

                        //Lookup the group involved in the command
                        int groupId = int.TryParse(StoredCommand.Argument, out groupId) ? groupId : 0;
                        var group = await Context.Groups.FirstOrDefaultAsync(o => o.Id == groupId) ??
                                    await Context.Groups.FirstOrDefaultAsync();

                        if (group != null)
                        {
                            selectedGroup = group;
                            _selectedBuiltinArg = group.Id.ToString(CultureInfo.InvariantCulture);
                        }

                        var control = new ComboboxControl(arg =>
                             {
                                 var g = arg as Group;
                                 if (g != null)
                                     _selectedBuiltinArg = g.Id.ToString(CultureInfo.InvariantCulture);
                                 
                                 return Task.FromResult(0);

                             },
                                 _icon,
                            await Context.Groups.ToListAsync())
                        {
                            Header = selectedCmd.Name,
                            Description = selectedCmd.Description,
                            SelectedItem = selectedGroup,
                        };
                        control.ComboBox.DisplayMemberPath = "Name";
                        BuiltinArgSckPnl.Children.Add(control);
                    }
                    break;
                case "RUN_SCENE":
                    {
                        Scene selectedScene = null;

                        //Try to match supplied arg (sceneID) with a scene
                        int sceneId = int.TryParse(StoredCommand.Argument, out sceneId) ? sceneId : 0;
                        var scene = await Context.Scenes.FirstOrDefaultAsync(o => o.Id == sceneId) ??
                                    await Context.Scenes.FirstOrDefaultAsync();

                        //If this is a new command or we cannot find the old scene, 
                        //just preselect the first scene.

                        if (scene != null)
                        {
                            selectedScene = scene;
                            _selectedBuiltinArg = scene.Id.ToString(CultureInfo.InvariantCulture);
                        }

                        var control = new ComboboxControl(arg =>
                            {
                                var s = arg as Scene;
                                if (s != null)
                                    _selectedBuiltinArg = s.Id.ToString(CultureInfo.InvariantCulture);

                                return Task.FromResult(0);

                            },
                                 _icon,
                             await Context.Scenes.ToListAsync())
                        {
                            Header = selectedCmd.Name,
                            Description = selectedCmd.Description,
                            SelectedItem = selectedScene
                        };

                        control.ComboBox.DisplayMemberPath = "Name";
                        BuiltinArgSckPnl.Children.Add(control);
                        break;
                    }
                #endregion
                default:
                    {
                        #region Built-in Commands
                        switch (selectedCmd.ArgumentType)
                        {
                            case DataType.NONE:
                                {
                                    _selectedBuiltinArg = string.Empty;
                                    BuiltinArgSckPnl.Children.Add(new TextBlock
                                    {
                                        Text = "None",
                                        Margin = new Thickness(30, 0, 0, 0)
                                    });
                                    break;
                                }
                            case DataType.BOOL:
                                {
                                    //get the current value from the value table list
                                    bool defaultValue;
                                    bool.TryParse(StoredCommand.Argument, out defaultValue);
                                    _selectedBuiltinArg = defaultValue.ToString();

                                    var control = new CheckboxControl(isChecked =>
                                        {
                                            _selectedBuiltinArg = isChecked.ToString();
                                            return Task.FromResult(0);
                                        },
                                        _icon)
                                    {
                                        Header = selectedCmd.Name,
                                        Description = selectedCmd.Description,
                                        Value = defaultValue
                                    };
                                    BuiltinArgSckPnl.Children.Add(control);

                                    break;
                                }
                            case DataType.INTEGER:
                            case DataType.DECIMAL:
                            case DataType.BYTE:
                            case DataType.SHORT:
                                {
                                    var defaultValue = "0";
                                    if (!string.IsNullOrEmpty(StoredCommand.Argument))
                                        defaultValue = StoredCommand.Argument;
                                    _selectedBuiltinArg = defaultValue;

                                    var control = new NumericControl(value =>
                                        {
                                            _selectedBuiltinArg = value;
                                            return Task.FromResult(0);
                                        },
                                        _icon, selectedCmd.ArgumentType)
                                    {
                                        Header = selectedCmd.Name,
                                        Description = selectedCmd.Description,
                                        Value = defaultValue
                                    };
                                    BuiltinArgSckPnl.Children.Add(control);
                                    break;
                                }
                            case DataType.STRING:
                                {
                                    //get the current value from the value table list
                                    var defaultValue = "0";
                                    if (!string.IsNullOrEmpty(StoredCommand.Argument))
                                        defaultValue = StoredCommand.Argument;
                                    _selectedBuiltinArg = defaultValue;

                                    var control = new StringControl(value =>
                                        {
                                            _selectedBuiltinArg = value;
                                            return Task.FromResult(0);
                                        },
                                        _icon)
                                    {
                                        Header = selectedCmd.Name,
                                        Description = selectedCmd.Description,
                                        Value = defaultValue
                                    };
                                    BuiltinArgSckPnl.Children.Add(control);

                                    break;
                                }
                            case DataType.LIST:
                                {
                                    var defaultValue = "";
                                    var firstOrDefault = selectedCmd.Options.FirstOrDefault();
                                    if (firstOrDefault != null)
                                    {
                                        var option = firstOrDefault.Name;

                                        if (option != null)
                                            defaultValue = option;
                                    }

                                    if (!string.IsNullOrEmpty(StoredCommand.Argument))
                                        defaultValue = StoredCommand.Argument;
                                    _selectedBuiltinArg = defaultValue;

                                    var control = new ComboboxControl(value =>
                                        {
                                            _selectedBuiltinArg = value.ToString();
                                            return Task.FromResult(0);
                                        },
                                 _icon,
                                 selectedCmd.Options.Select(o => o.Name).ToList())
                                    {
                                        Header = selectedCmd.Name,
                                        Description = selectedCmd.Description,
                                        SelectedItem = defaultValue
                                    };
                                    BuiltinArgSckPnl.Children.Add(control);

                                    break;
                                }
                        }
                        #endregion
                        break;
                    }
            }
        }
        private async Task LoadCommandsAsync()
        {
            PropertiesStkPnl.Children.Clear();

            var scene = await Context.Scenes
                .Include(o => o.SettingValues)
                .FirstOrDefaultAsync(sc => sc.Id == SceneId);

            if (scene == null)
                return;

            #region Scene Properties
            foreach (var sp in await Context.SceneSettings.ToListAsync())
            {
                var sceneSetting = sp;
                var sceneSettingValue = await Context.SceneSettingValues
                    .FirstOrDefaultAsync(v => v.SceneSetting.Id == sceneSetting.Id &&
                        v.SceneId == scene.Id);

                var _default = sceneSettingValue == null ? sceneSetting.Value : sceneSettingValue.Value;

                switch (sceneSetting.ValueType)
                {
                    case DataType.BOOL:
                        {
                            //get the current value from the value table list
                            bool defaultValue;
                            bool.TryParse(_default, out defaultValue);

                            var control = new CheckboxControl(async isChecked =>
                            {
                                if (sceneSettingValue != null)
                                {
                                    sceneSettingValue.Value = isChecked.ToString();
                                }
                                else
                                {
                                    sceneSettingValue = new SceneSettingValue()
                                    {
                                        SceneSetting = sceneSetting,
                                        Value = isChecked.ToString()
                                    };
                                    scene.SettingValues.Add(sceneSettingValue);
                                }

                                var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                if (result.HasError)
                                    await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving scene. {0}", result.Message);
                            }, icon)
                            {
                                Header = sceneSetting.Name,
                                Description = string.Empty,
                                Value = defaultValue
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                        }
                    case DataType.DECIMAL:
                    case DataType.INTEGER:
                    case DataType.BYTE:
                    case DataType.SHORT:
                        {
                            var control = new NumericControl(async value =>
                                {
                                    if (sceneSettingValue != null)
                                    {
                                        sceneSettingValue.Value = value;
                                    }
                                    else
                                    {
                                        sceneSettingValue = new SceneSettingValue()
                                        {
                                            SceneSetting = sceneSetting,
                                            Value = value
                                        };
                                        scene.SettingValues.Add(sceneSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving scene. {0}", result.Message);
                                },
                                    icon, sceneSetting.ValueType)
                            {
                                Header = sceneSetting.Name,
                                Description = string.Empty,
                                Value = _default
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                        }
                    case DataType.STRING:
                        {
                            var control = new StringControl(
                               async value =>
                               {
                                   if (sceneSettingValue != null)
                                   {
                                       sceneSettingValue.Value = value;
                                   }
                                   else
                                   {
                                       sceneSettingValue = new SceneSettingValue
                                       {
                                           SceneSetting = sceneSetting,
                                           Value = value
                                       };
                                       scene.SettingValues.Add(sceneSettingValue);
                                   }

                                   var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                   if (result.HasError)
                                       await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving scene. {0}", result.Message);
                               },
                                       icon)
                            {
                                Header = sceneSetting.Name,
                                Description = string.Empty,
                                Value = _default
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                        }

                    case DataType.LIST:
                        {
                            var control = new ComboboxControl(async value =>
                                {
                                    if (sceneSettingValue != null)
                                    {
                                        sceneSettingValue.Value = value.ToString();
                                    }
                                    else
                                    {
                                        //assign sceneSettingValue so above null check is ran in the case this is called 2 times before the save changes hits.
                                        sceneSettingValue = new SceneSettingValue
                                        {
                                            SceneSetting = sceneSetting,
                                            Value = value.ToString()
                                        };
                                        scene.SettingValues.Add(sceneSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving scene. {0}", result.Message);
                                },
                                 icon,
                           sceneSetting.Options.Select(o => o.Name).ToList())
                            {
                                Header = sceneSetting.Name,
                                Description = string.Empty,
                                SelectedItem = _default
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                        }
                }
            }
            #endregion
        }
        private async Task LoadCommandsAsync()
        {
            PropertiesStkPnl.Children.Clear();

            var device = await Context.Devices
                .Include(o => o.DeviceSettingValues)
                .Include(o => o.Type.Settings)
                .FirstOrDefaultAsync(dv => dv.Id == _deviceId);

            if (device == null)
                return;

            #region Device Type Settings
            foreach (var deviceTypeSetting in device.Type.Settings)
            {
                //default 
                var value = deviceTypeSetting.Value;

                //check if this settings has already been set
                var setting = deviceTypeSetting;
                var deviceTypeSettingValue = await Context.DeviceTypeSettingValues
                    .Where(o => o.DeviceId == device.Id)
                    .FirstOrDefaultAsync(o => o.DeviceTypeSettingId == setting.Id);

                if (deviceTypeSettingValue != null)
                    value = deviceTypeSettingValue.Value;

                switch (deviceTypeSetting.ValueType)
                {
                    case DataType.BOOL:
                        {
                            #region CheckboxControl
                            //get the current value from the value table list
                            bool defaultValue;
                            bool.TryParse(value, out defaultValue);

                            var control = new CheckboxControl(async isChecked =>
                            {
                                if (deviceTypeSettingValue != null)
                                    deviceTypeSettingValue.Value = isChecked.ToString();
                                else
                                {
                                    deviceTypeSettingValue = new DeviceTypeSettingValue
                                    {
                                        DeviceId = device.Id,
                                        DeviceTypeSettingId = setting.Id,
                                        Value = isChecked.ToString()
                                    };

                                    Context.DeviceTypeSettingValues.Add(deviceTypeSettingValue);
                                }

                                var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                if (result.HasError)
                                    await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device type setting. {0}", result.Message);

                            },
                                    _icon)
                            {
                                Header = deviceTypeSetting.Name,
                                Description = deviceTypeSetting.Description,
                                Value = defaultValue
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }
                    case DataType.INTEGER:
                    case DataType.DECIMAL:
                    case DataType.SHORT:
                    case DataType.BYTE:
                        {
                            #region NumericControl
                            var control = new NumericControl(
                                async v =>
                                {
                                    if (deviceTypeSettingValue != null)
                                        deviceTypeSettingValue.Value = v;
                                    else
                                    {
                                        deviceTypeSettingValue = new DeviceTypeSettingValue()
                                        {
                                            DeviceId = device.Id,
                                            DeviceTypeSettingId = setting.Id,
                                            Value = v
                                        };
                                        Context.DeviceTypeSettingValues.Add(deviceTypeSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device type setting. {0}", result.Message);
                                },
                                    _icon, deviceTypeSetting.ValueType)
                            {
                                Header = deviceTypeSetting.Name,
                                Description = deviceTypeSetting.Description,
                                Value = value
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }
                    case DataType.STRING:
                        {
                            #region StringControl
                            var control = new StringControl(async v =>
                                {
                                    if (deviceTypeSettingValue != null)
                                        deviceTypeSettingValue.Value = v;
                                    else
                                    {
                                        deviceTypeSettingValue = new DeviceTypeSettingValue()
                                        {
                                            DeviceId = device.Id,
                                            DeviceTypeSettingId = setting.Id,
                                            Value = v
                                        };
                                        Context.DeviceTypeSettingValues.Add(deviceTypeSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device type setting. {0}", result.Message);
                                },
                                        _icon)
                            {
                                Header = deviceTypeSetting.Name,
                                Description = deviceTypeSetting.Description,
                                Value = value,
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }

                    case DataType.LIST:
                        {
                            #region ComboboxControl
                            var control = new ComboboxControl(async v =>
                                {
                                    if (deviceTypeSettingValue != null)
                                        deviceTypeSettingValue.Value = v.ToString();
                                    else
                                    {
                                        deviceTypeSettingValue = new DeviceTypeSettingValue
                                        {
                                            DeviceId = device.Id,
                                            DeviceTypeSettingId = setting.Id,
                                            Value = v.ToString()
                                        };
                                        Context.DeviceTypeSettingValues.Add(deviceTypeSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device type setting. {0}", result.Message);
                                },
                                 _icon,
                           deviceTypeSetting.Options.Select(o => o.Name).ToList())
                            {
                                Header = deviceTypeSetting.Name,
                                Description = deviceTypeSetting.Description,
                                SelectedItem = value
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }
                }
            }


            #endregion
            #region Device Settings
            foreach (var s in await Context.DeviceSettings.ToListAsync())
            {
                var deviceSetting = s;

                //See if the device has a value stored for it for this property
                var deviceSettingValue = await Context.DeviceSettingValues.FirstOrDefaultAsync(v => v.DeviceSetting.Id == deviceSetting.Id &&
                    v.DeviceId == device.Id);

                var _default = deviceSettingValue == null ? deviceSetting.Value : deviceSettingValue.Value;

                switch (deviceSetting.ValueType)
                {
                    case DataType.BOOL:
                        {
                            #region CheckboxControl
                            //get the current value from the value table list
                            bool defaultValue;
                            bool.TryParse(_default, out defaultValue);

                            var control = new CheckboxControl(async isChecked =>
                            {
                                if (deviceSettingValue != null)
                                {
                                    deviceSettingValue.Value = isChecked.ToString();
                                }
                                else
                                {
                                    deviceSettingValue = new DeviceSettingValue()
                                    {
                                        DeviceSetting = deviceSetting,
                                        Value = isChecked.ToString()
                                    };
                                    device.DeviceSettingValues.Add(deviceSettingValue);
                                }

                                var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                if (result.HasError)
                                    await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device setting. {0}", result.Message);
                            },
                                        _icon)
                            {
                                Header = deviceSetting.Name,
                                Description = deviceSetting.Description,
                                Value = defaultValue
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }
                    case DataType.INTEGER:
                    case DataType.DECIMAL:
                    case DataType.SHORT:
                    case DataType.BYTE:
                        {
                            #region NumericControl
                            var control = new NumericControl(async value =>
                                {
                                    if (deviceSettingValue != null)
                                    {
                                        deviceSettingValue.Value = value;
                                    }
                                    else
                                    {
                                        deviceSettingValue = new DeviceSettingValue()
                                        {
                                            DeviceSetting = deviceSetting,
                                            Value = value
                                        };
                                        device.DeviceSettingValues.Add(deviceSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device setting. {0}", result.Message);
                                }, _icon, deviceSetting.ValueType)
                            {
                                Header = deviceSetting.Name,
                                Description = deviceSetting.Description,
                                Value = _default
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }
                    case DataType.STRING:
                        {
                            #region StringControl
                            var control = new StringControl(
                                async value =>
                                {
                                    if (deviceSettingValue != null)
                                    {
                                        deviceSettingValue.Value = value;
                                    }
                                    else
                                    {
                                        deviceSettingValue = new DeviceSettingValue()
                                        {
                                            DeviceSetting = deviceSetting,
                                            Value = value
                                        };
                                        device.DeviceSettingValues.Add(deviceSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device setting. {0}", result.Message);
                                },
                                        _icon)
                            {
                                Header = deviceSetting.Name,
                                Description = deviceSetting.Description,
                                Value = _default,
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }

                    case DataType.LIST:
                        {
                            #region ComboboxControl
                            var control = new ComboboxControl(async value =>
                                {
                                    if (deviceSettingValue != null)
                                    {
                                        deviceSettingValue.Value = value.ToString();
                                    }
                                    else
                                    {
                                        deviceSettingValue = new DeviceSettingValue
                                        {
                                            DeviceSetting = deviceSetting,
                                            Value = value.ToString()
                                        };
                                        device.DeviceSettingValues.Add(deviceSettingValue);
                                    }

                                    var result = await Context.TrySaveChangesAsync(_app.Cts.Token);
                                    if (result.HasError)
                                        await Log.ReportErrorFormatAsync(_app.Cts.Token, "Error saving device setting. {0}", result.Message);
                                },
                                _icon,
                                deviceSetting.Options.Select(o => o.Name).ToList())
                            {
                                Header = deviceSetting.Name,
                                Description = deviceSetting.Description,
                                SelectedItem = _default
                            };
                            PropertiesStkPnl.Children.Add(control);

                            break;
                            #endregion
                        }
                }
            }
            #endregion
        }