using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Device.Extenions
{
    public static class StringExtenions
    {
        public static string Byte2HexString(this byte[] bytes)
        {
            //return BitConverter.ToString(bytes);
            //return string.Join(" ", bytes.Select(d => d.ToString("X2")));

            return GetBytesString(bytes, 0, bytes.Length);
        }

        private static string GetBytesString(byte[] value, int startIndex, int length)
        {
            int num = length * 3;
            char[] array = new char[num];
            int num2 = startIndex;
            for (int i = 0; i < num; i += 3)
            {
                byte b = value[num2++];
                array[i] = GetHexValue((int)(b / 16));
                array[i + 1] = GetHexValue((int)(b % 16));
                array[i + 2] = ' ';
            }
            return new string(array, 0, array.Length - 1);
        }

        private static char GetHexValue(int i)
        {
            if (i < 10)
            {
                return (char)(i + 48);
            }
            return (char)(i - 10 + 65);
        }

        public static byte XOR_Check(this byte[] buffer, int count, int index = 0)
        {
            byte b = 0;
            for (int i = 0; i < count; i++)
            {
                b ^= buffer[index + i];
            }
            return b;
        }

        public static byte[] CRC16(this byte[] data)//CRC16-Modbus
        {
            int len = data.Length;
            if (len > 0)
            {
                ushort crc = 0xFFFF;

                for (int i = 0; i < len; i++)
                {
                    crc = (ushort)(crc ^ (data[i]));
                    for (int j = 0; j < 8; j++)
                    {
                        crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
                    }
                }
                byte hi = (byte)((crc & 0xFF00) >> 8);  //高位置
                byte lo = (byte)(crc & 0x00FF);         //低位置

                return new byte[] { lo, hi };
            }
            return new byte[] { 0, 0 };
        }

        public static string GetAgvAlarmText(this int alarm)
        {
            switch (alarm)
            {
                case 0:
                    return string.Empty;
                case 1:
                    return "急停报警";
                case 2:
                    return "防撞报警";
                case 3:
                    return "牵引棒";
                case 4:
                    return "障碍物";
                case 5:
                    return "前左驱动报警";
                case 6:
                    return "前后驱动报警";
                case 7:
                    return "脱轨报警";
                case 8:
                    return "低电压报警";
                case 9:
                    return "低电压停机";
                case 10:
                    return "超载报警";
                case 11:
                    return "驱动提升报警";
                case 12:
                    return "路线错误";
                case 13:
                    return "交通管制";
                case 14:
                    return "AGV内部温度异常";
                case 15:
                    return "AGV内部湿度异常";
                case 16:
                    return "烟雾浓度异常";
                default:
                    return $"未知异常(0x{alarm:X2}";
            }
        }

        public static string GetAgvStatusText(this int status)
        {
            switch (status)
            {
                case 0x40:
                    return "运行";
                case 0x43:
                    return "暂停";
                case 0x41:
                    return "停止点";
                case 0x42:
                    return "结束点";
                case 0x44:
                    return "充电中";
                default:
                    return $"未知状态(0x{status:X2})";
            }
        }
    }
}
