using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FObjBase;
using FLY.Thick.Base.Common;

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class DYNAREA_OBJ_INTERFACE
    {
        public const UInt32 ID = OBJ_INTERFACE_ID.DYNAREA_ID;
        #region Pack
        public class Pack_GetADPos : IPack
        {
            public int thick;
            public int position;
            public int boltIndex;
            public int ad;


            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(thick));
                buf.AddRange(BitConverter.GetBytes(position));
                buf.AddRange(BitConverter.GetBytes(boltIndex));
                buf.AddRange(BitConverter.GetBytes(ad));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int idx = 0;
                thick = BitConverter.ToInt32(value,idx);
                idx += 4;
                position = BitConverter.ToInt32(value, idx);
                idx += 4;
                boltIndex = BitConverter.ToInt32(value, idx);
                idx += 4;
                ad = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }

            #endregion
        }
        public class Pack_Position2 : IPack
        {

            public int position;


            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(position));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                position = BitConverter.ToInt32(value, 0);
                return true;
            }

            #endregion
        }
        public class Pack_GetScanInfo : IPack
        {
            public int[] sampleAD = new int[3];//最新样品AD
            public bool tempState;//动态温修
            public bool datavalid;//数据有效
            public int rolldown;//下辊信号
            public Misc.DIRECTION direction;//扫描方向
            public double width;//膜宽 mm
            public double filmVelocity;//线速度m/min
            public double velocity;//速度 m/min
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                for (int i = 0; i < 3; i++)
                {
                    buf.AddRange(BitConverter.GetBytes(sampleAD[i]));
                }
                buf.AddRange(BitConverter.GetBytes(tempState));
                buf.AddRange(BitConverter.GetBytes(datavalid));
                buf.AddRange(BitConverter.GetBytes(rolldown));
                buf.Add((byte)direction);
                buf.AddRange(BitConverter.GetBytes(width));
                buf.AddRange(BitConverter.GetBytes(filmVelocity));
                buf.AddRange(BitConverter.GetBytes(velocity));
                return (buf.ToArray());
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < (12 + 1  + 1 + 4 + 1+8*3))
                    return false;
                int index = 0;
                for (int i = 0; i < 3; i++)
                {
                    sampleAD[i] = BitConverter.ToInt32(value, index); index += 4;
                }
                tempState = BitConverter.ToBoolean(value, index); index++;
                datavalid = BitConverter.ToBoolean(value, index); index++;
                rolldown = BitConverter.ToInt32(value, index); index += 4;
                direction = (Misc.DIRECTION)value[index];index++;
                width = BitConverter.ToDouble(value, index); index += 8;
                filmVelocity = BitConverter.ToDouble(value, index); index += 8;
                velocity = BitConverter.ToDouble(value, index); index += 8;
                return true;
            }

            #endregion
        }
        public class Pack_GetControllerState : IPack
        {
            public CTRL_STATE controller_state;//运行状态
            public int autoscan_counter;//自动扫描时间累积

            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes((Int32)controller_state));
                buf.AddRange(BitConverter.GetBytes(autoscan_counter));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                if (value.Length < 8)
                    return false;
                int idx = 0;
                controller_state = (CTRL_STATE)BitConverter.ToInt32(value, idx);
                idx += 4;
                autoscan_counter = BitConverter.ToInt32(value, idx);
                idx += 4;
                return true;
            }

            #endregion
        }
        public class Pack_GetIO : IPack
        {
            public UInt16 iStatus;
            public UInt16 oStatus;


            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(iStatus));
                buf.AddRange(BitConverter.GetBytes(oStatus));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                iStatus = BitConverter.ToUInt16(value, 0);
                oStatus = BitConverter.ToUInt16(value, 2);
                return true;
            }

            #endregion
        }
        public class Pack_GetSystem : IPack
        {
            public bool isConnect;//FLYAD 连接状态
            public bool secuteLock;//锁
            public int hrs;//系统剩余小时
            public string version;//版本
            public GAGE_TYPE gagetype;//设备类型
            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(isConnect));
                buf.AddRange(BitConverter.GetBytes(secuteLock));
                buf.AddRange(BitConverter.GetBytes(hrs));
                byte[] version_buf = Converter.StringToBytes(version);
                buf.AddRange(BitConverter.GetBytes(version_buf.Length));
                buf.AddRange(version_buf);
                buf.AddRange(BitConverter.GetBytes((int)gagetype));

                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int cnt = 1 + 1 + 4 + 4 + 4;
                if (value.Length < cnt)
                    return false;

                int index = 0;
                isConnect = BitConverter.ToBoolean(value, index);
                index++;
                secuteLock = BitConverter.ToBoolean(value, index);
                index++;
                hrs = BitConverter.ToInt32(value, index);
                index += 4;
                
                int len = BitConverter.ToInt32(value, index);
                index += 4;
                cnt += len;
                if (value.Length < cnt)
                    return false;

                byte[] version_buf = new byte[len];
                Array.Copy(value, index, version_buf, 0, len);
                index += len;

                version = Converter.BytesToString(version_buf);
                
                gagetype = (GAGE_TYPE)BitConverter.ToInt32(value, index);
                index += 4;
                return true;
            }

            #endregion
        }
        public class Pack_GetProfile : IPack
        {
            public string ProductName;//产品名称
            public int Target;//目标值
            public int Alarm;//报警值
            public int DataBoltNoBegin;//数据开始分区号
            public int DataBoltNoEnd;//数据结束分区号
            public int ScanBoltNoBegin;
            public int ScanBoltNoEnd;
            public double Comp;
            public int Shift;

            #region IPack 成员

            public byte[] ToBytes()
            {

                List<byte> buf = new List<byte>();

                buf.AddRange(BitConverter.GetBytes(Target));
                buf.AddRange(BitConverter.GetBytes(Alarm));
                buf.AddRange(BitConverter.GetBytes(DataBoltNoBegin));
                buf.AddRange(BitConverter.GetBytes(DataBoltNoEnd));
                buf.AddRange(BitConverter.GetBytes(ScanBoltNoBegin));
                buf.AddRange(BitConverter.GetBytes(ScanBoltNoEnd));
                buf.AddRange(BitConverter.GetBytes(Comp));
                buf.AddRange(BitConverter.GetBytes(Shift));


                byte[] name_bytes = Converter.StringToBytes(ProductName);
                buf.AddRange(BitConverter.GetBytes((int)name_bytes.Length));
                buf.AddRange(name_bytes);
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                int index = 0;


                Target = BitConverter.ToInt32(value, index);
                index += 4;
                Alarm = BitConverter.ToInt32(value, index);
                index += 4;

                DataBoltNoBegin = BitConverter.ToInt32(value, index);//数据开始分区号
                index += 4;
                DataBoltNoEnd = BitConverter.ToInt32(value, index);//数据结束分区号
                index += 4;

                ScanBoltNoBegin = BitConverter.ToInt32(value, index);//数据开始分区号
                index += 4;
                ScanBoltNoEnd = BitConverter.ToInt32(value, index);//数据结束分区号
                index += 4;

                Comp = BitConverter.ToDouble(value, index);
                index += 8;
                Shift = BitConverter.ToInt32(value, index);
                index += 4;


                int name_bytes_length = BitConverter.ToInt32(value, index);
                index += 4;
                byte[] name_bytes = new byte[name_bytes_length];
                Array.Copy(value, index, name_bytes, 0, name_bytes_length);
                index += name_bytes_length;

                ProductName = Converter.BytesToString(name_bytes);

                return true;
            }

            #endregion
        }
        public class Pack_GetBoltmap : IPack
        {
            public int NBolts;//总分区数
            public int FirstBoltNo;//开始分区号


            #region IPack 成员

            public byte[] ToBytes()
            {
                List<byte> buf = new List<byte>();
                buf.AddRange(BitConverter.GetBytes(NBolts));
                buf.AddRange(BitConverter.GetBytes(FirstBoltNo));
                return buf.ToArray();
            }

            public bool TryParse(byte[] value)
            {
                NBolts = BitConverter.ToInt32(value, 0);
                FirstBoltNo = BitConverter.ToInt32(value, 4);
                return true;
            }

            #endregion
        }

        #endregion

        #region GetValue
        /// <summary>
        /// Pack_GetADPos
        /// </summary>
        public const UInt16 GET_ADPOS = 0;


        /// <summary>
        /// Pack_GetScanInfo
        /// </summary>
        public const UInt16 GET_SCANINFO = 1;


        /// <summary>
        /// Pack_GetControllerState
        /// </summary>
        public const UInt16 GET_CONTROLLER_STATE = 2;



        /// <summary>
        /// Pack_GetIO
        /// </summary>
        public const UInt16 GET_IO = 4;


        /// <summary>
        /// Pack_GetSystem
        /// </summary>
        public const UInt16 GET_SYSTEM = 5;

        /// <summary>
        /// Pack_GetProfile
        /// </summary>
        public const UInt16 GET_PROFILE = 6;

        /// <summary>
        /// Pack_GetBoltmap
        /// </summary>
        public const UInt16 GET_BOLTMAP = 7;

        /// <summary>
        /// Pack_Position2
        /// </summary>
        public const UInt16 GET_POSITION2 = 8;

        #endregion

        #region PushMsg
        /// <summary>
        /// Pack_GetADPos
        /// </summary>
        public const UInt16 PUSH_ADPOS = 0;
        /// <summary>
        /// Pack_GetScanInfo 
        /// </summary>
        public const UInt16 PUSH_SCANINFO = 1;
        /// <summary>
        /// Pack_GetControllerState
        /// </summary>
        public const UInt16 PUSH_CONTROLLER_STATE = 2;

        /// <summary>
        /// Pack_GetIO
        /// </summary>
        public const UInt16 PUSH_IO = 4;
        /// <summary>
        /// Pack_GetSystem
        /// </summary>
        public const UInt16 PUSH_SYSTEM = 5;

        /// <summary>
        /// Pack_GetProfile
        /// </summary>
        public const UInt16 PUSH_PROFILE = 6;
        /// <summary>
        /// Pack_GetBoltmap
        /// </summary>
        public const UInt16 PUSH_BOLTMAP = 7;

        /// <summary>
        /// Pack_Position2
        /// </summary>
        public const UInt16 PUSH_POSITION2 = 8;
        #endregion


    }
}
