using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 常用工具集.Base;
using McProtocol;
using McProtocol.Mitsubishi;

namespace 常用工具集.ViewModel._01PLC通信调试
{
    public class 三菱MC协议ViewModel : ViewModelBase
    {
        public List<string> MCList { get; set; } = new List<string>() { "MC1E", "MC3E", "MC4E" };
        public int MCIndex { get; set; } = 1;

        public bool Enabled1 { get; set; } = true;
        public bool Enabled2 { get; set; } = false;
        public string IpAddress { get; set; } = "127.0.0.1";
        public int Port { get; set; } = 5001;
        public string Address { get; set; } = "D100";
        public string Address2 { get; set; } = "D10";
        public int StringLength { get; set; } = 50;
        public string BoolValue { get; set; } = "true";
        public string Int16Value { get; set; } = "";
        public string Int32Value { get; set; } = "";
        public string RealValue { get; set; } = "";
        public string StringValue { get; set; } = "";
        public int BytesCount { get; set; } = 50;
        public string ReadedValue { get; set; } = "";
        public string Message { get; set; } = "";

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

        public DelegateCommand ReadCmd { get; set; }
        public DelegateCommand WriteCmd { get; set; }
        private McHelper plc;
        public 三菱MC协议ViewModel()
        {
            ConnectCmd = new DelegateCommand(ConnectCmdFunc);
            DisconnectCmd = new DelegateCommand(DisconnectCmdFunc);
            ReadCmd = new DelegateCommand(ReadCmdFunc);
            WriteCmd = new DelegateCommand(WriteCmdFunc);
        }


        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="obj"></param> 
        private void ConnectCmdFunc(object obj)
        {
            //  //  MC1E = 4,            MC3E = 11,        MC4E = 15
            if (MCIndex == 0)
            {
                this.plc = new McHelper(IpAddress, Port, McFrame.MC1E);
            }
            else if (MCIndex == 1)
            {
                this.plc = new McHelper(IpAddress, Port, McFrame.MC3E);
            }
            else if (MCIndex == 2)
            {
                this.plc = new McHelper(IpAddress, Port, McFrame.MC4E);
            }
            else
            {
                this.plc = new McHelper(IpAddress, Port);
            }


            bool flag = plc.Connect();
            if (flag)
            {
                Enabled2 = true;
                Enabled1 = false;
            }
            else
            {
                GlobalValues.Error("PLC连接失败");
            }
        }
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="obj"></param> 
        private void DisconnectCmdFunc(object obj)
        {
            try { plc.Dispose(); } catch { }
            plc = null;
            Enabled1 = true;
            Enabled2 = false;
        }

        /// <summary>
        /// 读取操作
        /// </summary>
        /// <param name="obj"></param>
        private void ReadCmdFunc(object obj)
        {
            ReadedValue = string.Empty;
            Message = string.Empty;
            string cmd = obj.ToString();
            if (cmd == "BOOL")
            {
                //bool value;
                //bool flag = plc.ReadBool(Address, out value);
                //if (!flag)
                //{
                //    ReadedValue = string.Empty;
                //    Message = "读取失败";
                //    return;
                //}
                //ReadedValue = value.ToString();
                Message = "该功能暂未实现";
            }
            else if (cmd == "INT16")
            {
                short value;
                bool flag = plc.ReadInt16(Address, out value);
                if (!flag)
                {
                    ReadedValue = string.Empty;
                    Message = "读取失败";
                    return;
                }
                ReadedValue = value.ToString();
                Message = "读取成功";
            }
            else if (cmd == "INT32")
            {
                int value;
                bool flag = plc.ReadInt32(Address, out value);
                if (!flag)
                {
                    ReadedValue = string.Empty;
                    Message = "读取失败";
                    return;
                }
                ReadedValue = value.ToString();
                Message = "读取成功";
            }
            else if (cmd == "REAL")
            {
                float value;
                bool flag = plc.ReadFloat(Address, out value);
                if (!flag)
                {
                    ReadedValue = string.Empty;
                    Message = "读取失败";
                    return;
                }
                ReadedValue = value.ToString();
                Message = "读取成功";
            }
            else if (cmd == "STRING")
            {
                string value;
                bool flag = plc.ReadString(Address, StringLength, out value);
                if (!flag)
                {
                    ReadedValue = string.Empty;
                    Message = "读取失败";
                    return;
                }
                ReadedValue = value.ToString();
                Message = "读取成功";
            }
            else if (cmd == "Bytes")
            {
                //ReadedValue = string.Empty;
                //Message = string.Empty;
                //byte[] value;
                //bool flag = plc.ReadBytes(Address2, BytesCount, out value);
                //if (!flag)
                //{
                //    ReadedValue = string.Empty;
                //    Message = "读取失败";
                //    return;
                //}
                //ReadedValue = ToHexStrFromByte(value);
                Message = "该功能暂未实现";
            }
        }

        private void WriteCmdFunc(object obj)
        {
            ReadedValue = string.Empty;
            Message = string.Empty;
            string cmd = obj.ToString();
            if (cmd == "BOOL")
            {
                Message = "该功能有问题，已弃用";
                return;
            }
            else if (cmd == "INT16")
            {
                short value;
                bool flag = short.TryParse(Int16Value, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = plc.WriteInt16(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "INT32")
            {
                int value;
                bool flag = int.TryParse(Int32Value, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = plc.WriteInt32(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "REAL")
            {
                float value;
                bool flag = float.TryParse(RealValue, out value);
                if (!flag)
                {
                    Message = "请输入正确的数据";
                    return;
                }
                flag = plc.WriteFloat(Address, value);
                Message = flag ? "写入成功" : "写入失败";
            }
            else if (cmd == "STRING")
            {
                bool flag = plc.WriteString(Address, StringLength, StringValue);
                Message = flag ? "写入成功" : "写入失败";
            }
        }

    
    }
}
