using System;
using System.Collections.Generic;
using System.Linq;
using 常用工具集.Base;
using MES.Utility.Network;
using MES.Utility.Core;
namespace 常用工具集.ViewModel._01PLC通信调试
{
    public class Modbus调试1ViewModel : ViewModelBase
    {
        private ModbusHelper modbus;
        public bool Enabled1 { get; set; } = true;
        public bool Enabled2 { get; set; } = false;


        public string IpAddress { get; set; } = "192.168.1.100";
        public int Port { get; set; } = 502;

        public ushort ReadAddress { get; set; } = 100;
        public ushort WriteAddress { get; set; } = 100;
        public ushort ReadCount { get; set; } = 5;
        public string ReadResult1 { get; set; } = "";
        public string ReadResult2 { get; set; } = "";
        public string ReadResult3 { get; set; } = "";
        public string ReadResult4 { get; set; } = "";
        public string WriteValue1 { get; set; } = "";
        public string WriteValue2 { get; set; } = "";
        public string WriteValue3 { get; set; } = "";
        public string WriteValue4 { get; set; } = "";


        public DelegateCommand ConnectCmd { get; set; }
        public DelegateCommand DisconnectCmd { get; set; }


        public DelegateCommand ReadCmd { get; set; } //1线圈 2状态. 3保持.4输入
        public DelegateCommand WriteCmd { get; set; } //1单个线圈 2单个保持. 3多个线圈 4多个保持


        public Modbus调试1ViewModel()
        {
            ConnectCmd = new DelegateCommand(ConnectCmdFunc);
            DisconnectCmd = new DelegateCommand(DisconnectCmdFunc);
            ReadCmd = new DelegateCommand(ReadCmdFunc);
            WriteCmd = new DelegateCommand(WriteCmdFunc);
        }

        private void ConnectCmdFunc(object obj)
        {
            try
            {
                modbus = new ModbusHelper(IpAddress, Port);
                Enabled1 = false;
                Enabled2 = true;
            }
            catch (Exception ex)
            {
                modbus = null;
                GlobalValues.Error("连接失败");
            }
        }

        private void DisconnectCmdFunc(object obj)
        {
            modbus.Dispose();
            modbus = null;
            ReadResult1 = ""; ReadResult2 = ""; ReadResult3 = ""; ReadResult4 = "";
            WriteValue1 = ""; WriteValue2 = ""; WriteValue3 = ""; WriteValue4 = "";
            Enabled1 = true;
            Enabled2 = false;
        }

        private void ReadCmdFunc(object obj)
        {
            int cmd = Convert.ToInt32(obj.ToString());
            bool flag = false;
            switch (cmd)
            {
                case 1:
                    flag = modbus.ReadCoils(ReadAddress, ReadCount, out bool[] values);
                    if (!flag)
                    {
                        GlobalValues.Error("读取失败");
                        DisconnectCmdFunc(null);
                        return;
                    }
                    ReadResult1 = values.Select(it => it ? "1" : "0").ToList().GetStrArray(" ");
                    return;
                case 2:
                    flag = modbus.ReadInputs(ReadAddress, ReadCount, out bool[] values2);
                    if (!flag)
                    {
                        GlobalValues.Error("读取失败");
                        DisconnectCmdFunc(null);
                        return;
                    }
                    ReadResult2 = values2.Select(it => it ? "1" : "0").ToList().GetStrArray(" ");
                    return;
                case 3:
                    flag = modbus.ReadHoldingRegisters(ReadAddress, ReadCount, out ushort[] values3);
                    if (!flag)
                    {
                        GlobalValues.Error("读取失败");
                        DisconnectCmdFunc(null);
                        return;
                    }
                    ReadResult3 = values3.Select(it => Convert.ToString(it)).ToList().GetStrArray(" ");
                    return;
                case 4:
                    flag = modbus.ReadInputRegisters(ReadAddress, ReadCount, out ushort[] values4);
                    if (!flag)
                    {
                        GlobalValues.Error("读取失败");
                        DisconnectCmdFunc(null);
                        return;
                    }
                    ReadResult4 = values4.Select(it => Convert.ToString(it)).ToList().GetStrArray(" "); ;
                    return;

            }
        }

        private void WriteCmdFunc(object obj)
        {
            try
            {
                int cmd = Convert.ToInt32(obj.ToString());
                bool flag = false;
                switch (cmd)
                {
                    case 1:
                        flag = modbus.WriteCoil(WriteAddress, WriteValue1 == "1" ? true : false);
                        if (!flag)
                        {
                            GlobalValues.Error("写入失败");
                            DisconnectCmdFunc(null);
                        }
                        else
                        {
                            GlobalValues.Success("写入成功");
                        }
                        return;
                    case 2:
                        flag = modbus.WriteRegister(WriteAddress, Convert.ToUInt16(WriteValue2));
                        if (!flag)
                        {
                            GlobalValues.Error("写入失败");
                            DisconnectCmdFunc(null);
                        }
                        else
                        {
                            GlobalValues.Success("写入成功");
                        }
                        return;
                    case 3:
                        List<bool> data1 = WriteValue3.Split(' ').Where(it => !it.IsNullOrEmpty()).Select(it => it == "1" ? true : false).ToList();
                        flag = modbus.WriteCoils(WriteAddress, data1.ToArray());
                        if (!flag)
                        {
                            GlobalValues.Error("写入失败");
                            DisconnectCmdFunc(null);
                        }
                        else
                        {
                            GlobalValues.Success("写入成功");
                        }
                        return;
                    case 4:
                        List<ushort> data2 = WriteValue4.Split(' ').Where(it => !it.IsNullOrEmpty()).Select(it => Convert.ToUInt16(it)).ToList();
                        flag = modbus.WriteRegisters(ReadAddress, data2.ToArray());
                        if (!flag)
                        {
                            GlobalValues.Error("写入失败");
                            DisconnectCmdFunc(null);
                        }
                        else
                        {
                            GlobalValues.Success("写入成功");
                        }
                        return;

                }
            }
            catch (Exception ex)
            {
                GlobalValues.Error($"写入失败：{ex.Message}");
            }
        }
    }
}
