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

namespace GWSocketClient.util
{
    class Utils
    {
        public static int hexToTen(string hex)
        {
            return int.Parse(hex, NumberStyles.HexNumber);
        }


        public static string hexToBin(string hex)
        {
            int res = hexToTen(hex);
            return stringWith0(Convert.ToString(res, 2), 8);
        }

        public static string tenToHex(string ten)
        {
            return tenToHex(Convert.ToInt32(ten));
        }


        public static string tenToHex(int ten)
        {
            return Convert.ToString(ten, 16);
        }


        public static string tenToBin(string ten)
        {
            return tenToBin(Convert.ToInt32(ten));
        }


        public static string tenToBin(int ten)
        {
            return Convert.ToString(ten, 2);
        }


        public static string binToTen(string bin)
        {
            return binToTenInt(bin) + "";
        }

        public static int binToTenInt(string bin)
        {
            return Convert.ToInt32(bin, 2);
        }


        public static string binToHex(string bin)
        {
            int i = binToTenInt(bin);
            return stringWith0(Convert.ToString(i, 16), 2);
        }


        public static string stringWith0(string str, int len)
        {
            int delta = len - str.Length;
            string sb = "";
            for (int i = 0; i < delta; i++)
            {
                sb += "0";
            }
            return sb + str;
        }

        public static string[] REG_STRING =
        {
            "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "a", "b", "c", "d", "e", "f"
        };

        public static string validInput(string reg)
        {
            string trim = reg.Trim();
            if (trim.Length > 0)
            {
                string t = trim.Substring(trim.Length - 1, 1);
                if (REG_STRING.Contains(t))
                {
                    return trim;
                }
                else
                {
                    return trim.Substring(0, trim.Length - 1);
                }
            }
            return trim;
        }

        public static string calcCrcCode(string str)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < str.Length; i += 2)
            {
                list.Add(str.Substring(i, 2));
            }

            int result = 0;
            for (int i = 3; i < list.Count(); i++)
            {
                result ^= hexToTen(list[i]);
            }
            return str + stringWith0(tenToHex(result), 2);
        }


        public static string calcCrcCode2(string str)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < str.Length; i += 2)
            {
                list.Add(str.Substring(i, 2));
            }

            int result = 0;
            for (int i = 3; i < list.Count(); i++)
            {
                result ^= hexToTen(list[i]);
            }
            return stringWith0(tenToHex(result), 2);
        }
    }
}