using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Ursa.Controls;
using 常用工具集.Base;

namespace 常用工具集.ViewModel._05其他
{
    public class 地标写入ViewModel : ViewModelBase
    {
        public bool Enabled1 { get; set; } = true;
        public bool Enabled2 { get; set; } = false;
        //串口号下拉列表数据
        public List<string> SerialList { get; set; }
        public int SerialIndex { get; set; } = 0;
        public string Data { get; set; } = "";

        public DelegateCommand OpenCmd { get; set; }
        public DelegateCommand CloseCmd { get; set; }
        public DelegateCommand ReadCmd { get; set; }
        public DelegateCommand WriteCmd { get; set; }
        public DelegateCommand ClearCmd { get; set; }

        private SerialPort sp1;
        public 地标写入ViewModel()
        {
            this.sp1 = new SerialPort();
            //串口号
            string[] portNames = SerialPort.GetPortNames();
            if (portNames != null && portNames.Length > 0)
            {
                SerialList = portNames.ToList();
            }
            OpenCmd = new DelegateCommand(OpenCmdFunc);
            CloseCmd = new DelegateCommand(CloseCmdFunc);
            ReadCmd = new DelegateCommand(ReadCmdFunc);
            WriteCmd = new DelegateCommand(WriteCmdFunc);
            ClearCmd = new DelegateCommand(ClearCmdFunc);
        }

        private void OpenCmdFunc(object obj)
        {
            try
            {
                this.sp1.PortName = SerialList[SerialIndex];
                this.sp1.BaudRate = 19200;
                this.sp1.DataBits = 8;
                this.sp1.StopBits = StopBits.One;
                this.sp1.Parity = Parity.None;
                if (this.sp1.IsOpen)
                {
                    this.sp1.Close();
                }
                this.sp1.Open();
                Enabled1 = false;
                Enabled2 = true;
            }
            catch (Exception ex)
            {
                MessageBox.ShowAsync(ex.Message);
            }
        }

        private void CloseCmdFunc(object obj)
        {
            if (this.sp1.IsOpen)
            {
                this.sp1.Close();
            }
            Enabled1 = true;
            Enabled2 = false;
        }

        private void ReadCmdFunc(object obj)
        {

            List<byte> list = this.sendData(this.getReadData());
            if ((list != null) && (list.Count >= 15))
            {
                int num = 0;
                num = (((((((list[9] << 8) | list[10]) << 8) | list[11]) << 8) | list[12]) << 8) | list[13];
                Data = Convert.ToString(num);
                this.sendData(this.getBeatData());
            }
        }

        private void WriteCmdFunc(object obj)
        {
            if (this.sp1.IsOpen)
            {
                string str = Data;
                int num = 0;
                bool flag = int.TryParse(str, out num);
                if (!flag)
                {
                    MessageBox.ShowAsync("请输入数字");
                    return;
                }
                if ((num < 0) || (num >= 999999999))
                {
                    MessageBox.ShowAsync("数字只能在0~999999999之间");
                    return;
                }
                byte a = (byte)((num & 0xff00000000L) >> 32);
                byte b = (byte)((num & 0xff000000L) >> 24);
                byte c = (byte)((num & 0xff0000) >> 16);
                byte d = (byte)((num & 0xff00) >> 8);
                byte num8 = (byte)(num & 0xff);
                this.sendData(this.getWriteData(a, b, c, d, num8));
                this.sendData(this.getBeatData());
            }
        }

        private void ClearCmdFunc(object obj)
        {
            Data = "";
        }


        private List<byte> getBeatData()
        {
            return new List<byte> { 0xAA, 0xBB, 0x05, 0x00, 0x00, 0x00, 0x06, 0x01, 0x07 };
        }

        private List<byte> getReadData()
        {
            return new List<byte> { 0xAA, 0xBB, 0x05, 0x00, 0x00, 0x00, 0x10, 0x20, 0x30 };
        }


        private List<byte> getWriteData(byte a, byte b, byte c, byte d, byte e)
        {
            List<byte> list = new List<byte>();
            list.Add(0xAA);
            list.Add(0xBB);
            list.Add(0x0A);
            list.Add(0x00);
            list.Add(0x00);
            list.Add(0x00);
            list.Add(0x09);
            list.Add(0x03);
            list.Add(a);
            list.Add(b);
            list.Add(c);
            list.Add(d);
            list.Add(e);
            byte item = list[6];
            for (int i = 7; i < 13; i++)
            {
                item ^= list[i];
            }
            list.Add(item);
            return list;
        }


        public List<byte> sendData(List<byte> sendData)
        {
            if (!this.sp1.IsOpen)
            {
                return null;
            }
            this.sp1.Write(sendData.ToArray(), 0, sendData.Count);
            while (this.sp1.BytesToRead == 0)
            {
                Thread.Sleep(1);
            }
            Thread.Sleep(50);
            byte[] buffer = new byte[this.sp1.BytesToRead];
            this.sp1.Read(buffer, 0, buffer.Length);
            return buffer.ToList<byte>();
        }

        private void Sp1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (this.sp1.IsOpen)
            {
                DateTime now = DateTime.Now;
                try
                {
                    byte[] buffer = new byte[this.sp1.BytesToRead];
                    this.sp1.Read(buffer, 0, buffer.Length);
                }
                catch (Exception exception)
                {
                    MessageBox.ShowAsync(exception.Message, "出错提示!!!!!");
                }
            }
            else
            {
                MessageBox.ShowAsync("请打开某个串口", "错误提示");
            }
        }
    }
}
