using Common.Attributes;
using Model;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace DeviceCommand.Device
{
    [BOBCommand]
    public class Backfeed
    {
        public string CurrentDevice { get; set; }
        private E36233A _E36233A { get; set; }
        private IT6724CReverse _IT6724CReverse { get; set; }
        public object CurrentInstance
        {
            get
            {
                if (CurrentDevice == "E36233A")
                    return _E36233A;
                else if (CurrentDevice == "IT6724CReverse")
                    return _IT6724CReverse;
                else
                    throw new InvalidOperationException("还没初始化实例");
            }
        }
        public Backfeed(ICommunicationConfig config)
        {
            if (config is TcpConfig tcpConfig)
            {
                _E36233A = new E36233A(tcpConfig.IPAddress, tcpConfig.Port, tcpConfig.WriteTimeout, tcpConfig.ReadTimeout);
                CurrentDevice = "E36233A";
            }
            else if (config is SerialPortConfig serialPortConfig)
            {
                _IT6724CReverse = new IT6724CReverse(serialPortConfig.COMPort, serialPortConfig.BaudRate, serialPortConfig.DataBit, serialPortConfig.StopBit, serialPortConfig.ParityBit, serialPortConfig.ReadTimeout, serialPortConfig.WriteTimeout);
                CurrentDevice = "IT6724CReverse";
            }
        }

        #region 统一接口 - 设置命令

        // 清除输出保护
        public virtual async Task 清除输出保护(CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.清除输出保护(ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.清除输出保护(ct);
        }

        // 设置电源输出
        public virtual async Task 设置电源输出(bool 开关, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置电源输出(开关, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置电源输出(开关, ct);
        }

        // 设置为远程模式
        public virtual async Task 设置为远程模式(CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置为远程模式(ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置为远程模式(ct);
        }

        // 蜂鸣器测试
        public virtual async Task 蜂鸣器测试(CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.蜂鸣器测试(ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.蜂鸣器测试(ct);
        }

        // 设置电流
        public virtual async Task 设置电流(double 电流, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置电流(电流, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置电流(电流, ct);
        }

        // 设置电流保护OCP电流
        public virtual async Task 设置电流保护OCP电流(double 电流, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置电流保护OCP电流(电流, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置电流保护OCP电流(电流, ct);
        }

        // 设置OCP开关
        public virtual async Task 设置OCP开关(bool 开关, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置OCP开关(开关, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置OCP开关(开关, ct);
        }

        // 设置电压
        public virtual async Task 设置电压(double 电压, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置电压(电压, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置电压(电压, ct);
        }

        // 设置电压保护OVP电压
        public virtual async Task 设置电压保护OVP电压(double 电压, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置电压保护OVP电压(电压, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置电压保护OVP电压(电压, ct);
        }

        // 设置OVP开关
        public virtual async Task 设置OVP开关(bool 开关, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置OVP开关(开关, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置OVP开关(开关, ct);
        }

        // 设置功率保护功率
        public virtual async Task 设置功率保护功率(double 电压, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置功率保护功率(电压, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置功率保护功率(电压, ct);
        }

        // 设置功率保护开关
        public virtual async Task 设置功率保护开关(bool 开关, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置功率保护开关(开关, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置功率保护开关(开关, ct);
        }

        // 设置电流斜率
        public virtual async Task 设置电流斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置电流斜率(上升斜率, 下降斜率, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置电流斜率(上升斜率, 下降斜率, ct);
        }

        // 设置电压斜率
        public virtual async Task 设置电压斜率(double 上升斜率, double 下降斜率, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.设置电压斜率(上升斜率, 下降斜率, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.设置电压斜率(上升斜率, 下降斜率, ct);
        }

        // 发送自定义命令
        public virtual async Task 发送自定义命令(string 指令, CancellationToken ct = default)
        {
            if (CurrentDevice == "E36233A")
                await _E36233A.发送自定义命令(指令, ct);
            else if (CurrentDevice == "IT6724CReverse")
                await _IT6724CReverse.发送自定义命令(指令, ct);
        }
        public virtual async Task<double> 查询实时电压(CancellationToken ct = default)
        {
            double value = 0;

            if (CurrentDevice == "E36233A")
                value = await _E36233A.查询电压(ct);
            else if (CurrentDevice == "IT6724CReverse")
                value = await _IT6724CReverse.查询实时电压(ct);

            return Math.Round(value, 1);
        }

        public virtual async Task<double> 查询实时电流(CancellationToken ct = default)
        {
            double value = 0;

            if (CurrentDevice == "E36233A")
                value = await _E36233A.查询电流(ct);
            else if (CurrentDevice == "IT6724CReverse")
                value = await _IT6724CReverse.查询实时电流(ct);

            return Math.Round(value, 1);
        }

        public virtual async Task<double> 查询实时功率(CancellationToken ct = default)
        {
            double value = 0;

            if (CurrentDevice == "E36233A")
                value = await _E36233A.查询功率(ct);
            else if (CurrentDevice == "IT6724CReverse")
                value = await _IT6724CReverse.查询实时功率(ct);

            return Math.Round(value, 1);
        }



        #endregion
    }
}
