using LitJson;
using System;
using System.Text;
using System.Text.RegularExpressions;
using UnityEngine;

namespace WS
{
    ///<summary>扩展</summary>
    public static class Extend
	{
		public static string ToHex(this byte b)
		{
			return b.ToString("X2");
		}

		public static string ToHex(this byte[] bytes)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (byte b in bytes)
			{
				stringBuilder.Append(b.ToString("X2"));
			}
			return stringBuilder.ToString();
		}

		public static string ToHex(this byte[] bytes, string format)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (byte b in bytes)
			{
				stringBuilder.Append(b.ToString(format));
			}
			return stringBuilder.ToString();
		}

		public static string ToHex(this byte[] bytes, int offset, int count)
		{
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = offset; i < offset + count; ++i)
			{
				stringBuilder.Append(bytes[i].ToString("X2"));
			}
			return stringBuilder.ToString();
		}

		public static string ToStr(this byte[] bytes)
		{
			return Encoding.Default.GetString(bytes);
		}

		public static string ToStr(this byte[] bytes, int index, int count)
		{
			return Encoding.Default.GetString(bytes, index, count);
		}

		public static string Utf8ToStr(this byte[] bytes)
		{
			return Encoding.UTF8.GetString(bytes);
		}

		public static string Utf8ToStr(this byte[] bytes, int index, int count)
		{
			return Encoding.UTF8.GetString(bytes, index, count);
		}

		public static void WriteTo(this byte[] bytes, int offset, uint num)
		{
			bytes[offset] = (byte)(num & 0xff);
			bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
			bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
			bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
		}

		public static void WriteTo(this byte[] bytes, int offset, int num)
		{
			bytes[offset] = (byte)(num & 0xff);
			bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
			bytes[offset + 2] = (byte)((num & 0xff0000) >> 16);
			bytes[offset + 3] = (byte)((num & 0xff000000) >> 24);
		}

		public static void WriteTo(this byte[] bytes, int offset, byte num)
		{
			bytes[offset] = num;
		}

		public static void WriteTo(this byte[] bytes, int offset, short num)
		{
			bytes[offset] = (byte)(num & 0xff);
			bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
		}

		public static void WriteTo(this byte[] bytes, int offset, ushort num)
		{
			bytes[offset] = (byte)(num & 0xff);
			bytes[offset + 1] = (byte)((num & 0xff00) >> 8);
		}

		/// <summary>转换为16进制</summary>
		/// <param name="lowerCase">是否小写</param>
		public static string ToHex(this byte[] bytes, bool lowerCase = true)
		{
			if (bytes == null)
				return null;

			var result = new StringBuilder();
			var format = lowerCase ? "x2" : "X2";
			for (var i = 0; i < bytes.Length; i++)
			{
				result.Append(bytes[i].ToString(format));
			}

			return result.ToString();
		}

		/// <summary>16进制转字节数组</summary>
		public static byte[] HexToBytes(this string s)
		{
			if (string.IsNullOrEmpty(s))
				return null;
			var bytes = new byte[s.Length / 2];

			for (int x = 0; x < s.Length / 2; x++)
			{
				int i = (Convert.ToInt32(s.Substring(x * 2, 2), 16));
				bytes[x] = (byte)i;
			}

			return bytes;
		}

		public static byte[] ToByteArray(this string str)
		{
			byte[] byteArray = Encoding.Default.GetBytes(str);
			return byteArray;
		}

		public static byte[] ToUtf8(this string str)
		{
			byte[] byteArray = Encoding.UTF8.GetBytes(str);
			return byteArray;
		}

		/// <summary>转换为Base64</summary>
		public static string ToBase64(this byte[] bytes)
		{
			if (bytes == null)
				return null;

			return Convert.ToBase64String(bytes);
		}

		private readonly static StringBuilder sb = new StringBuilder();

		/// <summary>设置输入框限制字符</summary>
		/// <param name="arg0">输入字符</param>
		/// <param name="regex">正则表达式</param>
		/// <param name="length">限制长度</param>
		public static string SetRegexText(string arg0, string regex, int length)
		{
			sb.Clear();
			if (arg0.Length >= length)
			{
				arg0 = arg0.Substring(0, arg0.Length - 1);
			}
			Regex reg = new Regex(regex);
			if (!reg.IsMatch(arg0))
			{
				char[] chararr = arg0.ToCharArray();
				for (int i = 0; i < chararr.Length; i++)
				{
					string str = chararr[i].ToString();
					if (reg.IsMatch(str))
					{
						sb.Append(str);
					}
				}
				arg0 = sb.ToString();
			}
			return arg0;
		}

		public static string ToJson(this object obj)
		{
			return JsonMapper.ToJson(obj);
		}

		public static bool BelongsToHierarchy(this Transform subject, Transform hierarchy)
		{
			if (subject == null)
				return false;
			else if (subject == hierarchy)
				return true;
			else
				return BelongsToHierarchy(subject.transform.parent, hierarchy);
		}
	}
}