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

namespace FVS
{
    public static class BaseUtils
    {
        public static string Color2Hex(Color color)
        {
            return "#" + ColorUtility.ToHtmlStringRGB(color);
        }
        public static Color CssColor2UnityColor(string color)
        {
            Color c = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            if (color[0] == '#')
            {
                ColorUtility.TryParseHtmlString(color, out c);
            }
            else if (color[0] == 'R' || color[0] == 'r')
            {
                MatchCollection mc = Regex.Matches(color, @"[0-9]+\.*[0-9]*");
                float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;

                if (mc.Count == 3)
                {
                    r = float.Parse(mc[0].Value) / 255.0f;
                    g = float.Parse(mc[1].Value) / 255.0f;
                    b = float.Parse(mc[2].Value) / 255.0f;
                }
                else
                {
                    r = float.Parse(mc[0].Value) / 255.0f;
                    g = float.Parse(mc[1].Value) / 255.0f;
                    b = float.Parse(mc[2].Value) / 255.0f;
                    a = float.Parse(mc[3].Value);
                }
                c = new Color(r, g, b, a);
            }
            return c;
        }

        public static void WriteVector3(JsonWriter jw, Vector3 vector)
        {
            jw.WriteArrayStart();
            jw.Write(vector.x);
            jw.Write(vector.y);
            jw.Write(vector.z);
            jw.WriteArrayEnd();
        }

        public static Vector3 ParseVector3(float[] arr)
        {
            return new Vector3(arr[0], arr[1], arr[2]);
        }

        public static string JsonStringFormat(string sourceJson)
        {
            sourceJson += " ";
            int itap = 0;
            string newjson = "";
            string gap = "  ";

            for (int i = 0; i < sourceJson.Length - 1; i++)
            {
                if (sourceJson[i] == '{' || sourceJson[i] == '[')
                {
                    itap++;
                    newjson += sourceJson[i] + "\n";
                    for (int a = 0; a < itap; a++) { newjson += gap; }
                }
                else if ((sourceJson[i] == '}' || sourceJson[i] == ']'))
                {
                    itap--;
                    newjson += "\n";
                    for (int a = 0; a < itap; a++) { newjson += gap; }
                    newjson += sourceJson[i] + "" + ((sourceJson[i + 1] == ',') ? "," : "") + "\n";
                    if (sourceJson[i + 1] == ',') i++;
                    for (int a = 0; a < itap; a++) { newjson += gap; }
                }
                else if (sourceJson[i] != '}' && sourceJson[i] != ']' && sourceJson[i + 1] == ',')
                {
                    newjson += sourceJson[i] + "" + sourceJson[i + 1] + "\n";
                    i++;
                    for (int a = 0; a < itap; a++) { newjson += gap; }
                }
                else
                {
                    newjson += sourceJson[i];
                }
            }
            return newjson;
        }
    }
    
}

