using UnityEngine;
using System;
using System.Text;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using System.Collections.Generic;
/**
 * @email 540632763@qq.com
 *
 * @author Oscar
 */
namespace com.badam.support
{
    public class WeiYuSupport
    {
        static char[] symbol = { '<', '[', '{' };

        static readonly string[] tags = { "</color>", "[/color]", "{/color}", "</size>", "[/size]", "{/size}", "</b>", "[/b]", "{/b}", "</i>", "[/i]", "{/i}" };

        static readonly string[] patternList = new string[] {
        @"([<\[{](?<Tag>(?:color|b|i|size))[^>}\]]*?[>\]}])((?:(?<Nested>[<\[{]\k<Tag>[^>}\]]*[>\]}])|[<\[{]/\k<Tag>[>\]}](?<-Nested>)|.*?)*)([<\[{]/\k<Tag>[>\]}])" };

        public static string ToString(string str )
        {

            return CheckAndReset(str,0);
        }

        static string CheckAndReset(string str, int id)
        {
            string res = "";
            if (id< patternList.Length)
            {
                string pattern = patternList[id];
                Regex regex = new Regex(pattern);
                if (regex.IsMatch(str))
                {
                    string[] rs = Regex.Split(str, pattern);
                    List<string> mylist = new List<string>();
                    int len = rs.Length;
                    for (int i = len - 1; i >= 0; i--)
                    {
                        if (!String.IsNullOrEmpty(rs[i]))
                        {
                            if (ContainsTag(rs[i]) )
                            {//翻转标签
                                mylist[mylist.Count - 1] = rs[i - 2];
                                mylist.Add(CheckAndReset( rs[i - 1] ,id) );
                                mylist.Add(rs[i]);
                                i -= 2;
                            }
                            else
                            {
                                mylist.Add( CheckAndReset( rs[i], id ) );
                            }

                        }
                    }
                    res = string.Join("", mylist.ToArray());
                }
                else {
                    res = CheckAndReset(str,id+1);
                }
            }
            else
            {
                res = ToWeiYu(str);
            }

            return res;
        }
        public static string ResetTags(string str)
        {
            string res = "";

            string pattern = patternList[0];
            Regex regex = new Regex(pattern);
            if (regex.IsMatch(str))
            {
                string[] rs = Regex.Split(str, pattern);
                List<string> mylist = new List<string>();
                int len = rs.Length;
                for (int i = 0; i<len;i++)
                {
                    if (!String.IsNullOrEmpty(rs[i]))
                    {
                        if (ContainsTag(rs[i]))
                        {
                            mylist[mylist.Count - 2] = rs[i].Reverse();
                            mylist.Add(rs[i-2].Reverse());
                            i += 1;
                        }
                        else
                        {
                            mylist.Add(ResetTags(rs[i]));
                        }

                    }
                }
                res = string.Join("", mylist.ToArray());
            }
            else {
                res = str;
            }
            return res;
        }

        static bool ContainsTag(string s)
        {
            for(int i=0;i< tags.Length; i++)
            {
                if(s == tags[i])
                {
                    return true;
                }
            }

            return false;
        }

        static string ToWeiYu(string v)
        {
            char[] sources = v.ToCharArray();
            string s = UyBaseAndEx.toEx(v);
            char[] arr = s.ToCharArray();

            Array.Reverse(arr);
            string res = new string(arr);

            res = Reverse(res, @"[a-zA-Z_0-9~=+$%.￥…—// -]+");

            res = res.Replace("(", "#####");
            res = res.Replace("（", "#####");
            res = res.Replace(")", "(");
            res = res.Replace("）", "(");
            res = res.Replace("#####", ")");

            res = res.Replace("[", "#####");
            res = res.Replace("【", "#####");
            res = res.Replace("]", "[");
            res = res.Replace("】", "[");
            res = res.Replace("#####", "]");
            /*
            res = res.Replace("{", "#####");
            res = res.Replace("}", "{");
            res = res.Replace("#####", "}");*/

            res = res.Replace("<", "#####");
            res = res.Replace(">", "<");
            res = res.Replace("#####", ">");

            return res;

        }

        static string Reverse(string res, string pattern)
        {
            //将英文字母和数字反序回来
            Regex regExp = new Regex(pattern);
            if (regExp.IsMatch(res))
            {
                res = Regex.Replace(res, pattern, new MatchEvaluator(Reverse));
            }
            return res;
        }

        static string Reverse(Match m)
        {
            string s = m.Value;
            string res = "";
            //UnityEngine.Debug.LogError(s);
            char[] arr = s.ToCharArray();
            Array.Reverse(arr);
            res = new string(arr);
            return res;
        }

    }
}