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

namespace TZONE.GPSSocketServer.Common
{
    /// <summary>
    /// Tools Class
    /// </summary>
    public class Utils
    {

        /// <summary>
        /// Picture packets
        /// </summary>
        public static string[] PicTitle =
        {
            "FFD8FFDB008400140E0F120F0D14121012171514181E32211E1C1C1E3D2C2E243249404C4B47404645505A736250556D5645466488656D777B8182814E608D978C7D96737E817C011517171E1A1E3B21213B7C5346537C7C7C7C7C7C7C7C7C7C7C7C7C7C",
            "7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7C7CFFC000110800F0014003012100021101031101FFDD00040014FFC401A20000010501010101010100000000000000000102030405060708090A0B100002010303",
            "020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A",
            "838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FA010003010101010101010101000000000000010203040506070809",
            "0A0B1100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A63646566676869",
            "6A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00E3C8C1C1E0",
        };

        /// <summary>
        ///  return time
        /// </summary>
        /// <param name="data">date(ddMMyy)</param>
        /// <param name="time">time(HHmmss)</param>
        public static DateTime stringTodatetime(string data, string time)
        {
            data = data.Insert(4, "20");
            string _d = data + time.Substring(0, 6);
            return DateTime.ParseExact(_d, "ddMMyyyyHHmmss", null);
        }
        public static decimal GetLat(string lat, string ns)
        {
            decimal l = System.Convert.ToDecimal(lat.Substring(0, 2)) + System.Convert.ToDecimal(lat.Substring(2)) / 60;
            if (ns.ToUpper() == "S")
            {
                return l * (-1);
            }
            else
            {
                return l;
            }
        }
        public static decimal GetLng(string lng, string we)
        {
            decimal l = System.Convert.ToDecimal(lng.Substring(0, 3)) + System.Convert.ToDecimal(lng.Substring(3)) / 60;
            if (we == "E")
            {
                return l;
            }
            else
            {
                return l * (-1);
            }
        }
        public static string GetIO(string io, int a, int b)
        {
            try
            {
                return io.Substring(a, b);

            }
            catch
            {
                return "-1";
            }
        }
        public static decimal GetDecimal(string value)
        {
            try
            {
                return Convert.ToDecimal(value);
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Encoding escape character
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string Escape(string msg)
        {
            if (string.IsNullOrEmpty(msg))
                return "";
            msg = msg.Replace("\'", "\\'");
            msg = msg.Replace("\"", "\\\"");
            msg = msg.Replace("\\", "\\\\");
            msg = msg.Replace("\0", "\\0");
            msg = msg.Replace("\a", "\\a");
            msg = msg.Replace("\t", "\\t");
            msg = msg.Replace("\r", "\\r");
            msg = msg.Replace("\n", "\\n");
            msg = msg.Replace("\f", "\\f");
            msg = msg.Replace("\b", "\\b");
            msg = msg.Replace("\v", "\\v");
            return msg;
        }

        /// <summary>
        /// Hexadecimal string genetically byte array
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] HexToByte(string hexString)
        {
            int count = hexString.Length;
            int len = count / 2;
            byte[] HTB = new byte[len];
            int num = 0;
            for (int i = 0; i < count; i += 2)
            {
                HTB[num] = System.Convert.ToByte(hexString.Substring(i, 2), 16);
                num++;
            }
            return HTB;
        }

        public static decimal GetDTLat(string lat)
        {
            string ns =lat.Substring(lat.Length - 1);
            decimal l = System.Convert.ToDecimal(lat.Substring(0, 2)) + System.Convert.ToDecimal(lat.Substring(2,lat.Length-3)) /10000/ 60;
            if (ns.ToUpper() == "S")
            {
                return l * (-1);
            }
            else
            {
                return l;
            }
        }
        public static decimal GetDTLng(string lng)
        {
            string we = lng.Substring(lng.Length - 1);
            decimal l = System.Convert.ToDecimal(lng.Substring(0, 3)) + System.Convert.ToDecimal(lng.Substring(3, lng.Length - 4)) / 10000 / 60;
            if (we == "E")
            {
                return l;
            }
            else
            {
                return l * (-1);
            }
        }

    }
}
