using BOB.Singleton;
using DeviceCommand.Device;
using Logger;
using NetTaste;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BOB.ViewModels.Dialogs
{
    public class WS_68030_380TViewModel : BindableBase, IDialogAware
    {
        #region 属性

        private string _title = "交流负载";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private string _currentPowerDisplay;
        public string CurrentPowerDisplay
        {
            get { return _currentPowerDisplay; }
            set { SetProperty(ref _currentPowerDisplay, value); }
        }

        #endregion

        #region 命令

        public ICommand ConnectCommand { get; private set; }
        public ICommand DisconnectCommand { get; private set; }
        public ICommand LoadCommand { get; private set; }
        public ICommand UnloadCommand { get; private set; }
        public ICommand ReadPowerCommand { get; private set; }
        public ICommand SetPowerCommand { get; private set; }
        public ICommand CloseCommand { get; private set; }

        #endregion

        #region 事件聚合器和设备操作

        private IEventAggregator _eventAggregator { get; set; }
        private Devices _devices { get; set; }
        private GlobalVariables _globalVariables { get; set; }
        private WS_68030_380T device { get; set; }

        public WS_68030_380TViewModel(IContainerProvider containerProvider)
        {
            _devices = containerProvider.Resolve<Devices>();
            _globalVariables = containerProvider.Resolve<GlobalVariables>();
            _eventAggregator = containerProvider.Resolve<IEventAggregator>();

            // 初始化命令
            ConnectCommand = new AsyncDelegateCommand(OnConnect);
            DisconnectCommand = new DelegateCommand(OnDisconnect);
            LoadCommand = new AsyncDelegateCommand(OnLoad);
            UnloadCommand = new AsyncDelegateCommand(OnUnload);
            ReadPowerCommand = new AsyncDelegateCommand(OnReadPower);
            SetPowerCommand = new AsyncDelegateCommand(OnSetPower);
            CloseCommand = new DelegateCommand(OnClose);
        }

        #endregion

        #region 命令处理方法

        private async Task OnConnect()
        {
            await device.ConnectAsync();
        }

        private void OnDisconnect()
        {
            device.Close();
        }

        private async Task OnLoad()
        {
            await device.加载();
        }

        private async Task OnUnload()
        {
            await device.卸载();
        }

        private async Task OnReadPower()
        {
            var result = await device.读取功率_KW();
        }

        private async Task OnSetPower()
        {
            if (float.TryParse(CurrentPowerDisplay, out float a))
            {
                await device.设置功率_KW(a);
            }
        }

        #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["WS_68030_380T"] as WS_68030_380T;
                if (a != null)
                {
                    device = a;
                }
                else
                {
                    LoggerHelper.ErrorWithNotify("设备没有初始化无法打开测试界面");
                    RequestClose.Invoke();
                }
            }
            catch (Exception)
            {
                LoggerHelper.ErrorWithNotify("找不到设备无法打开测试界面");
            }

        }
        private void OnClose()
        {
            RequestClose.Invoke();
        }

        #endregion
    }
}
