using System.Runtime.InteropServices;
using System.Text;

namespace EasyTemplate.Service
{
    public class Tool
    {
        public const int NozzleState_Offline = 0;
        public const int NozzleState_Idle = 1;
        public const int NozzleState_Filling = 2;

        public const int WarningState_Normal = 0;
        public const int WarningState_Prewarning = 1;
        public const int WarningState_Warning = 2;


        // 计算单个字节的CRC
        public static ushort calccrc(byte crcbuf, ushort crc)
        {
            byte i;
            crc = (ushort)(crc ^ crcbuf);
            for (i = 0; i < 8; i++)
            {
                byte chk;
                chk = (byte)(crc & 1);
                crc = (ushort)(crc >> 1);
                crc = (ushort)(crc & 0x7fff);
                if (chk == 1)
                    crc = (ushort)(crc ^ 0xa001); /* CRC polynom=0xa001 */
                crc = (ushort)(crc & 0xffff);
            }
            return crc;
        }

        // 带自定义多项式的CRC计算
        public static ushort calccrc(byte crcbuf, ushort crc, ushort polynom)
        {
            byte i;
            crc = (ushort)(crc ^ crcbuf);
            for (i = 0; i < 8; i++)
            {
                byte chk;
                chk = (byte)(crc & 1);
                crc = (ushort)(crc >> 1);
                crc = (ushort)(crc & 0x7fff);
                if (chk == 1)
                    crc = (ushort)(crc ^ polynom);
                crc = (ushort)(crc & 0xffff);
            }
            return crc;
        }

        // 标准CRC校验
        public static ushort chkcrc(byte[] buf, ushort len)
        {
            ushort i;
            ushort crc;
            crc = 0x0000; /* Initial crc value=0x0000 */
            for (i = 0; i < len; i++)
            {
                crc = calccrc(buf[i], crc);
            }
            return crc;
        }

        // 带多项式的CRC校验
        public static ushort chkcrc(byte[] buf, ushort len, ushort polynom)
        {
            ushort i;
            ushort crc;
            crc = 0x0000;
            for (i = 0; i < len; i++)
            {
                crc = calccrc(buf[i], crc, polynom);
            }
            return crc;
        }

        // Modbus CRC校验
        public static ushort chkcrc_modbus(byte[] buf, ushort len, ushort polynom)
        {
            ushort i;
            ushort crc;
            crc = 0xffff;
            for (i = 0; i < len; i++)
            {
                crc = calccrc(buf[i], crc, polynom);
            }
            return crc;
        }

        public static void crc_calc_sinopec(byte[] ptr_buffer, ushort length)
        {
            ushort crc_result;

            /* calculate CRC */
            crc_result = chkcrc(ptr_buffer, length);
            /* ... and store it */
            ptr_buffer[length] = (byte)(crc_result >> 8);
            ptr_buffer[length + 1] = (byte)crc_result;
        }

        public static string BytesToBcd(byte[] Bytes)
        {
            if (Bytes == null || Bytes.Length == 0)
                return string.Empty;

            StringBuilder result = new StringBuilder();

            foreach (byte b in Bytes)
            {
                // 提取高4位和低4位
                byte highNibble = (byte)((b >> 4) & 0x0F);
                byte lowNibble = (byte)(b & 0x0F);

                // 转换为字符
                result.Append((char)('0' + highNibble));
                result.Append((char)('0' + lowNibble));
            }

            return result.ToString();
        }
        public static T ByteToStructure<T>(Byte[] dataBuffer)
        {
            object structure = null;
            int size = Marshal.SizeOf(typeof(T));
            IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
                structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
            }
            catch(Exception e)
            {

            }
            finally
            {
                Marshal.FreeHGlobal(allocIntPtr);
            }
            return (T)structure;
        }

        public static byte[] StructToBytes(object structObj)
        {
            //得到结构体的大小
            int size = Marshal.SizeOf(structObj);
            //创建byte数组
            byte[] bytes = new byte[size];
            //分配结构体大小的内存空间
            IntPtr structPtr = Marshal.AllocHGlobal(size);
            //将结构体拷到分配好的内存空间
            Marshal.StructureToPtr(structObj, structPtr, false);
            //从内存空间拷到byte数组
            Marshal.Copy(structPtr, bytes, 0, size);
            //释放内存空间
            Marshal.FreeHGlobal(structPtr);
            //返回byte数组
            return bytes;
        }


        /// <summary>
        /// 将字节数组按大端序转换为整数
        /// </summary>
        /// <param name="bytes">字节数组</param>
        /// <returns>转换后的整数</returns>
        public static int BytesToInt(byte[] bytes)
        {
            if (bytes == null)
                throw new ArgumentNullException(nameof(bytes));

            if (bytes.Length == 0)
                return 0;

            if (bytes.Length > 4)
                throw new ArgumentException("字节数组长度不能超过4个字节");

            int result = 0;
            for (int i = 0; i < bytes.Length; i++)
            {
                result = (result << 8) | bytes[i];
            }
            return result;
        }




       

    }
}
