using BOB.Singleton;
using BOB.ViewModels.UserControls;
using Common.PubEvent;
using DeviceCommand.Device;
using Logger;
using OxyPlot;
using OxyPlot.Annotations;
using OxyPlot.Axes;
using OxyPlot.Legends;
using OxyPlot.Series;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BOB.ViewModels.Dialogs
{
    public class EAEL9080ViewModel : BindableBase, IDialogAware
    {
      
        #region 属性
        private string _Title = "低压直流负载";
        private string _设备标识字符串;
        private string _负载模式;
        private  LineSeries _电压_lineSeries = new LineSeries
        {
            Title = "电压(V)",
            StrokeThickness = 2,
        };
        private  LineSeries _电流_lineSeries = new LineSeries
        {
            Title = "电流(A)",
            StrokeThickness = 2,
        };
        private  LineSeries _功率_lineSeries = new LineSeries
        {
            Title = "功率(W)",
            StrokeThickness = 2,
        };
        private NumericalDisplayViewModel _电压MV=new();
        private NumericalDisplayViewModel _电流MV = new();
        private NumericalDisplayViewModel _功率MV=new();
        private PlotModel _曲线Model = new();

        public PlotModel 曲线Model
        {
            get { return _曲线Model; }
            set { SetProperty(ref _曲线Model, value); }
        }
        public string Title
        {
            get { return _Title; }
            set { SetProperty(ref _Title, value); }
        }

        public string 设备标识字符串
        {
            get { return _设备标识字符串; }
            set { SetProperty(ref _设备标识字符串, value); }
        }

        public string 负载模式
        {
            get { return _负载模式; }
            set { SetProperty(ref _负载模式, value); }
        }

        public LineSeries 电流_lineSeries
        {
            get { return _电流_lineSeries; }
            set { SetProperty(ref _电流_lineSeries, value); }
        }

        public LineSeries 电压_lineSeries
        {
            get { return _电压_lineSeries; }
            set { SetProperty(ref _电压_lineSeries, value); }
        }
        public LineSeries 功率_lineSeries
        {
            get { return _功率_lineSeries; }
            set { SetProperty(ref _功率_lineSeries, value); }
        }
        public NumericalDisplayViewModel 电压MV
        {
            get { return _电压MV; }
            set { SetProperty(ref _电压MV, value); }
        }
        public NumericalDisplayViewModel 电流MV
        {
            get { return _电流MV; }
            set { SetProperty(ref _电流MV, value); }
        }
        public NumericalDisplayViewModel 功率MV
        {
            get { return _功率MV; }
            set { SetProperty(ref _功率MV, value); }
        }
        public DateTimeAxis TimeAxis { get; private set; }
        public LinearAxis ValueAxis { get; private set; }
        #endregion

        #region 命令
        public ICommand 连接命令 { get; private set; }
        public ICommand 断开命令 { get; private set; }
        public ICommand 设置为远程模式命令 { get; private set; }
        public ICommand 设置负载工作模式命令 { get; private set; }
        public ICommand 电源输出开命令 { get; private set; }
        public ICommand 电源输出关命令 { get; private set; }
        public ICommand 关闭命令 { get; private set; }
        public ICommand 查询设备信息命令 { get; private set; }
        public ICommand ResetViewCommand { get; private set; }

        #endregion

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

        public EAEL9080ViewModel(IContainerProvider containerProvider)
        {
            _devices = containerProvider.Resolve<Devices>();
            _globalVariables = containerProvider.Resolve<GlobalVariables>();
            _eventAggregator = containerProvider.Resolve<IEventAggregator>();
            连接命令 = new AsyncDelegateCommand(OnConnect);
            断开命令 = new DelegateCommand(OnDisconnect);
            设置为远程模式命令 = new AsyncDelegateCommand(OnSetRemoteMode);
            设置负载工作模式命令 = new AsyncDelegateCommand(OnSetLoadMode);
            电源输出开命令 = new AsyncDelegateCommand(OnPowerOn);
            电源输出关命令 = new AsyncDelegateCommand(OnPowerOff);
            关闭命令 = new DelegateCommand(OnOff);
            查询设备信息命令 = new AsyncDelegateCommand(OnQueryDeviceInfo);
            ResetViewCommand = new DelegateCommand(ResetView);
            _eventAggregator.GetEvent<CurveDataEvent>().Subscribe(UpdateCurve);
            InitCurve();
        }
        private void ResetView()
        {
            曲线Model.ResetAllAxes();
            曲线Model.InvalidatePlot(true);
        }
        private void InitCurve()
        {
            TimeAxis = new DateTimeAxis
            {
                Position = AxisPosition.Bottom,
                StringFormat = "HH:mm:ss",
                IntervalType = DateTimeIntervalType.Seconds,
            };
            ValueAxis = new LinearAxis
            {
                Position = AxisPosition.Left,
            };
            曲线Model.Axes.Add(TimeAxis);
            曲线Model.Axes.Add(ValueAxis);
            曲线Model.Series.Add(电压_lineSeries);
            曲线Model.Series.Add(电流_lineSeries);
            曲线Model.Series.Add(功率_lineSeries);
            曲线Model.Legends.Add(new Legend()
            {
                LegendPosition = LegendPosition.TopRight,
                LegendOrientation = LegendOrientation.Vertical,
                LegendPlacement = LegendPlacement.Inside
            });

            曲线Model.InvalidatePlot(true);
        }

        private void UpdateCurve((string device, Dictionary<string,double> data) tuple)
        {
            var (device, data) = tuple;
            if (device != "EAEL9080") return;
            电流MV.MonitorValue = data["实时电流"];
            电压MV.MonitorValue = data["实时电压"];
            功率MV.MonitorValue = data["实时功率"];

            var now = DateTimeAxis.ToDouble(DateTime.Now);
            电流_lineSeries.Points.Add(new DataPoint(now, data["实时电流"]));
            电压_lineSeries.Points.Add(new DataPoint(now, data["实时电压"]));
            功率_lineSeries.Points.Add(new DataPoint(now, data["实时功率"]));
            曲线Model.InvalidatePlot(true);
        }
        private async Task OnQueryDeviceInfo()
        {
            设备标识字符串 = await device.查询设备信息();
        }

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

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

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


        private async Task OnSetRemoteMode()
        {
            await device.设置远程控制(true);
        }

        private async Task OnSetLoadMode()
        {
            
        }

        private async Task OnPowerOn()
        {
            await device.打开输出();
        }

        private async Task OnPowerOff()
        {
            await device.关闭输出();
        }

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