using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ProductionControl.Utils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;

namespace ProductionControl.Device
{
    internal class SmallAxis
    {
        public Action<int, string> log; 
        public Action<bool> stateChange;
        /// <summary>
        /// 最大倍数总的脉冲数
        /// </summary>
        public int MaxPPU { get; private set; }
        /// <summary>
        /// 是否打开设备成功
        /// </summary>
        public bool IsInit { get; private set; } = false;
        private SSerialPort sSerialPort;

        public SmallAxis(string comName,int baud)
        {
            sSerialPort = new SSerialPort(comName, baud);
        }
        public void start()
        { 
            //最大
            //byte[] getMaxPPU = new byte[] { 0x58, 0x4D, 0x0D };
            //sSerialPort.send(getMaxPPU, 0, getMaxPPU.Length);
            //Thread.Sleep(20);
            //byte[] recv = sSerialPort.getMessage();//9 byte
            //if (recv == null) recv = new byte[0]; 
            //if (recv.Length > 0)
            //{
            //    byte[] maxPos = subBuff(recv, 2, 5);//5 byte
            //    string str = "";
            //    for (int i = 0; i < maxPos.Length; i++)
            //       str += maxPos[i].ToString("X") + " ";

            //    //MaxPos= maxPos
            //}

            //
            IsInit = true;
            //timer.Elapsed += Timer_Elapsed;
            //timer.Interval = 100;
            //timer.Enabled = true;
        }
        public void stop()
        {
            if (!IsInit) return;

            IsInit = false;
            sSerialPort.close();
        }
        public void home()
        {
            byte[] goHome = new byte[] { 0x58, 0x48, 0x0D };
            sSerialPort.send(goHome, 0, goHome.Length);
        }
        public void gotoPos(int pos)
        {
            int x = pos;// 100;
            string hex = x.ToString("X").PadLeft(6, ' ');
            byte[] gotoX = new byte[9];
            gotoX[0] = 0x58;
            gotoX[1] = 0x47;
            for (int i = 0; i < hex.Length; i++)
                gotoX[i + 2] = (byte)(hex[i]);
            gotoX[8] = 0x0D;
            sSerialPort.send(gotoX, 0, gotoX.Length);
        }

        public bool isFree()
        {
            byte[] getState = new byte[] { 0x58, 0x5A, 0x0D };
            sSerialPort.send(getState, 0, getState.Length);
            Thread.Sleep(20);
            byte[] recv = sSerialPort.getMessage();
            if (recv == null) recv = new byte[0];
            if (recv.Length > 0)
                return recv[0] == 0x55;
            return false;
        }
      
        public int getCurrPPU()
        {
            byte[] getPPU = new byte[] { 0x58, 0x4E, 0x0D };
            sSerialPort.send(getPPU, 0, getPPU.Length);
            Thread.Sleep(20);
            byte[] recv = sSerialPort.getMessage();//9 byte
            if (recv == null) recv = new byte[0]; 
            if (recv.Length > 0)
            {
                byte[] maxPos = subBuff(recv, 2, 5);//5 byte
                string str = "";
                for (int i = 0; i < maxPos.Length; i++)
                    str += maxPos[i].ToString("X") + " ";
                return 0;
            }
            return -1;
        }
        private byte[] subBuff(byte[] buff, int start, int length)
        {
            byte[] res = new byte[length];
            for (int i = start; i < buff.Length && i < start + length; i++)
                res[i - start] = buff[i];
            return res;
        }
    }
}
