using BOB.Singleton;
using DeviceCommand.Device;
using Logger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BOB.ViewModels.Dialogs
{
    public class SQ0030G1DViewModel : BindableBase, IDialogAware
    {

        #region 属性

        private string _Title = "交流电源";
        public string Title
        {
            get { return _Title; }
            set { SetProperty(ref _Title, value); }
        }

        private string _UPhaseVoltage;
        public string UPhaseVoltage
        {
            get { return _UPhaseVoltage; }
            set { SetProperty(ref _UPhaseVoltage, value); }
        }

        private string _VPhaseVoltage;
        public string VPhaseVoltage
        {
            get { return _VPhaseVoltage; }
            set { SetProperty(ref _VPhaseVoltage, value); }
        }

        private string _WPhaseVoltage;
        public string WPhaseVoltage
        {
            get { return _WPhaseVoltage; }
            set { SetProperty(ref _WPhaseVoltage, value); }
        }

        private string _CommonOutputVoltage;
        public string CommonOutputVoltage
        {
            get { return _CommonOutputVoltage; }
            set { SetProperty(ref _CommonOutputVoltage, value); }
        }

        private string _CommonOutputFrequency;
        public string CommonOutputFrequency
        {
            get { return _CommonOutputFrequency; }
            set { SetProperty(ref _CommonOutputFrequency, value); }
        }

        private string _StepOutputVoltage;
        public string StepOutputVoltage
        {
            get { return _StepOutputVoltage; }
            set { SetProperty(ref _StepOutputVoltage, value); }
        }

        private string _StepOutputFrequency;
        public string StepOutputFrequency
        {
            get { return _StepOutputFrequency; }
            set { SetProperty(ref _StepOutputFrequency, value); }
        }

        private string _StepOutputVoltageGroup;
        public string StepOutputVoltageGroup
        {
            get { return _StepOutputVoltageGroup; }
            set { SetProperty(ref _StepOutputVoltageGroup, value); }
        }

        private string _StepOutputFrequencyGroup;
        public string StepOutputFrequencyGroup
        {
            get { return _StepOutputFrequencyGroup; }
            set { SetProperty(ref _StepOutputFrequencyGroup, value); }
        }

        #endregion

        #region 命令

        public ICommand ConnectCommand { get; private set; }
        public ICommand DisconnectCommand { get; private set; }
        public ICommand SetVoltageLevelLowCommand { get; private set; }
        public ICommand SetVoltageLevelHighCommand { get; private set; }
        public ICommand SetThreePhaseVoltageCommand { get; private set; }
        public ICommand SetCommonOutputVoltageCommand { get; private set; }
        public ICommand SetCommonOutputFrequencyCommand { get; private set; }
        public ICommand SetStepOutputVoltageCommand { get; private set; }
        public ICommand SetStepOutputFrequencyCommand { get; private set; }
        public ICommand OpenPowerSupplyCommand { get; private set; }
        public ICommand ClosePowerSupplyCommand { get; private set; }
        public ICommand CloseCommand { get; private set; }

        #endregion

        private IEventAggregator _eventAggregator { get; set; }
        private Devices _devices { get; set; }
        private GlobalVariables _globalVariables { get; set; }
        private SQ0030G1D device { get; set; }
        public SQ0030G1DViewModel(IContainerProvider containerProvider)
        {
            _devices = containerProvider.Resolve<Devices>();
            _globalVariables = containerProvider.Resolve<GlobalVariables>();
            _eventAggregator = containerProvider.Resolve<IEventAggregator>();
            ConnectCommand = new AsyncDelegateCommand(OnConnect);
            DisconnectCommand = new DelegateCommand(OnDisconnect);
            CloseCommand = new DelegateCommand(OnClose);
            SetVoltageLevelLowCommand = new AsyncDelegateCommand(OnSetVoltageLevelLow);
            SetVoltageLevelHighCommand = new AsyncDelegateCommand(OnSetVoltageLevelHigh);
            SetThreePhaseVoltageCommand = new AsyncDelegateCommand(OnSetThreePhaseVoltage);
            SetCommonOutputVoltageCommand = new AsyncDelegateCommand(OnSetCommonOutputVoltage);
            SetCommonOutputFrequencyCommand = new AsyncDelegateCommand(OnSetCommonOutputFrequency);
            SetStepOutputVoltageCommand = new AsyncDelegateCommand(OnSetStepOutputVoltage);
            SetStepOutputFrequencyCommand = new AsyncDelegateCommand(OnSetStepOutputFrequency);
            ClosePowerSupplyCommand = new AsyncDelegateCommand(ClosePowerSupply);
            OpenPowerSupplyCommand = new AsyncDelegateCommand(OpenPowerSupply);
        }

        #region Command Handlers
        private void OnClose()
        {
            RequestClose.Invoke();
        }
        private async Task OnConnect()
        {
            await device.ConnectAsync();
        }

        private void OnDisconnect()
        {
            device.Close();
        }
        private async Task OpenPowerSupply()
        {
            await device.启动电源();
        }

        private async Task ClosePowerSupply()
        {
            await device.关闭电源();    
        }
        private async Task OnSetVoltageLevelLow()
        {
            
        }

        private async Task  OnSetVoltageLevelHigh()
        {
            
        }

        private async Task OnSetThreePhaseVoltage()
        {

        }

        private async Task OnSetCommonOutputVoltage()
        {

        }

        private async Task OnSetCommonOutputFrequency()
        {
           
        }

        private async Task OnSetStepOutputVoltage()
        {
          
        }

        private async Task OnSetStepOutputFrequency()
        {

        }

        #endregion

        #region dialog规范
        public DialogCloseListener RequestClose { get; set; }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            try
            {
                var a = _devices.DeviceDic["SQ0030G1D"] as SQ0030G1D;
                if (a != null)
                {
                    device = a;
                }
                else
                {
                    LoggerHelper.ErrorWithNotify("设备没有初始化无法打开测试界面");
                    RequestClose.Invoke();
                }
            }
            catch (Exception)
            {
                LoggerHelper.ErrorWithNotify("找不到设备无法打开测试界面");
            }

        }

        #endregion
    }
}
