using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
namespace GuruClient
{
    public enum StartMode
    {
        //继续
        Continue,
        //重玩
        Replay,
        //查看
        View
    }
    public enum LevelType
    {
        None,
        MainLine,
        Daily,
        Story,
        UA,
        Activity
    }
    [Serializable]
    public class ALevel
    {
        //关卡ID
        public int level;
        //关卡类型:0-主线关卡  1-每日挑战关卡  2-活动关卡
        public LevelType type;
        public string psdID;
        public string gameGeneration;
        //关卡ab对应的hash
        public string gameHash;
        public string thumbnailBadGeneration;
        public string thumbnailFinishGeneration;
        
        //Daily
        public string dateTime;
        public bool pass;
        
        //Activity
        public int unlockStar;
        public int unlockCoin;
        //1,渐现,2缩放
        public int animType = 2;
        //标签
        public string tags;
        public string GameABName()
        {
            return $"330_assets_bundles_levels_{psdID}.ab?generation={gameGeneration}";
        }
        public string ThumbanilBadABName()
        {
            return $"331_assets_bundles_textures_thumbnail_{psdID}_bad_png.ab?generation={thumbnailBadGeneration}";
        }
        public string ThumbanilFinishABName()
        {
            return $"331_assets_bundles_textures_thumbnail_{psdID}_finish_png.ab?generation={thumbnailFinishGeneration}";
        }
        public string GetLevelName()
        {
            if (type == LevelType.MainLine)
            {
                return $"MainLine_PSDID_{psdID}";
            }
            if (type == LevelType.Daily)
            {
                return $"Daily_PSDID_{psdID}";
            }
            if (type == LevelType.Story)
            {
                return $"Story_PSDID_{psdID}";
            }
            if (type == LevelType.Activity)
            {
                return $"Activity_PSDID_{psdID}";
            }
            
            return psdID;
        }
        public string GetTypeName()
        {
            if (type == LevelType.MainLine)
            {
                return $"MainLine";
            }
            if (type == LevelType.Daily)
            {
                return $"Daily";
            }
            if (type == LevelType.Story)
            {
                return $"Story";
            }
            if (type == LevelType.Activity)
            {
                return $"Activity";
            }
            return "";
        }
    }
    [Serializable]
    public class APatch
    {
        public string name;
        public Vector2 pos1;
        public float[] pos;
        public int stage;
        public int sub_stage;
        public string tag;
        public bool isValid()
        {
            return !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(tag);
        }
        public void SetName(string nameIn)
        {
            this.name = nameIn;
        }
        /// 
        /// 是否是拼图块
        /// 
        /// 
        public bool IsPatch()
        {
            return tag == "patch";
        }
        public bool IsLine()
        {
            return tag == "line";
        }
        public bool IsAuto()
        {
            return tag == "auto";
        }
        public bool IsFinish()
        {
            return tag == "finish";
        }
    }
    
    [Serializable]
    public class PatchData
    {
        public string name;
        public Vector2 pos;
        public int stage;
        public int sub_stage;
        public string parent;
        public string tag;
        //用于info
        public float mapWidth;
        public float mapHeight;
        
        /// 
        /// 是否是拼图块
        /// 
        /// 
        public bool IsPatch()
        {
            return tag == "patch";
        }
        public bool IsSubPatch()
        {
            return tag == "spatch";
        }
        
        public bool IsAuto()
        {
            return tag == "auto";
        }
        public bool IsFinish()
        {
            return tag == "finish";
        }
        public bool IsBase()
        {
            return tag == "base";
        }
        public bool IsBad()
        {
            return tag == "bad";
        }
        public bool IsSubBad()
        {
            return tag == "sbad";
        }
        
        public bool IsEntry()
        {
            return tag == "entry";
        }
        public bool IsInfo()
        {
            return name == "info";
        }
        public bool IsRole()
        {
            return tag == "role";
        }
    }
    public class ConfigManager
    {
        private static ConfigManager _instance;
        public static ConfigManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ConfigManager();
                }
                return _instance;
            }
        }
        #region 关卡池
        //总关卡池
        private List _levelPoolList = new List();
        //总关卡池字典
        private Dictionary _levelPoolDict = new Dictionary();
        //每日挑战关卡池
        private List _dailyLevelPoolList = new List();
        //每日挑战关卡池字典
        private Dictionary _dailyLevelPoolDict = new Dictionary();
        #endregion
        
        #region 主线本地已存储关卡列表
        //主线关卡列表
        private List _mainlineDBList = new List();
        //主线关卡字典
        private Dictionary _mainlineDBDict = new Dictionary();
        #endregion
        
        #region Daily本地已存储关卡列表
        //每日挑战列表
        private List _dailyLevelsDBList = new List();
        //每日挑战字典
        private Dictionary _dailyLevelsDBDict = new Dictionary();
        //每日挑战日期和关卡对照字典
        private Dictionary _dailyDT2Level = new Dictionary();
        #endregion
        
        
        private string version = "";
        public bool challengeConfigLoaded = false;
        
        public void Initialize()
        {
            
        }
        
        /// 
        /// 通过PSDID获取关卡数据
        /// 
        public ALevel GetLevel(string psdID, LevelType levelType, string dateTime = "", bool readOnly = false)
        {
            ALevel ret = new ALevel();
            ret.psdID = "395";
            ret.level = 1;
            ret.type = LevelType.MainLine;
            ret.tags = "";
            Debug.Log($"获取 GetLevel 失败,未找到 psd_id=" + psdID);
            return ret;
        }
        /// 
        /// 获取关卡池第一个关卡
        /// 
        /// 
        public ALevel GetLevelPoolFirst()
        {
            ALevel aLevel = null;
            if (_levelPoolList.Count > 0)
            {
                aLevel = _levelPoolList[0];
            }
            return aLevel;
        }
        
        /// 
        /// 获取Daily关卡池第一个关卡
        /// 
        /// 
        public ALevel GetDailyLevelPoolFirst()
        {
            ALevel aLevel = null;
            if (_dailyLevelPoolList.Count > 0)
            {
                aLevel = _dailyLevelPoolList[0];
            }
            if (aLevel == null)
            {
                aLevel = GetLevelPoolFirst();
            }
            return aLevel;
        }
        public ALevel GetDailyLevelByDT(string dt)
        {
            if (_dailyDT2Level.TryGetValue(dt, out var value))
            {
                return value;
            }
            return null;
        }
        public Dictionary ParsePatches(string txt)
        {
            Dictionary patchesDB = new Dictionary();
            Debug.Log(txt);
            Dictionary patchesDB1 = Utility.Json.Deserialize>(txt);
            foreach (KeyValuePair aItem in patchesDB1)
            {
                aItem.Value.SetName(aItem.Key);
            }
            // for (int i = 0; i < patchesDB1.Count(); i++)
            // {
            //     // patchesDB1.ElementAt(i).Value.name = patchesDB1.ElementAt(i).Key;
            //     KeyValuePair kv = patchesDB1.ElementAt(i);
            //     kv.Value.SetName(kv.Key);
            // }
            // foreach (var item1 in js)
            // {
            //     string key1 = item1.Key;
            //     JObject value1 = (JObject)js[key1];
            //     APatch pe = new APatch();
            //     pe.name = key1;
            //     foreach (var item2 in value1)
            //     {
            //         string key2 = item2.Key;
            //         var value2 = value1[key2];
            //
            //         if ((key2).Equals("pos"))
            //         {
            //             double x = (double)value2[0];
            //             double y = (double)value2[1];
            //
            //             pe.pos.x = (float)x;
            //             pe.pos.y = (float)y; 
            //         }else if((key2).Equals("tag"))
            //         {
            //             pe.tag = (string)value2;
            //         }else if((key2).Equals("stage"))
            //         {
            //             pe.stage = int.Parse((string)value2);
            //         }else if((key2).Equals("sub_stage"))
            //         {
            //             pe.sub_stage = int.Parse(((string)value2));
            //         }
            //     }
            //     patchesDB.Add((string)key1, pe);
            // }
            return patchesDB1;
        }
        /// 
        /// 通过关卡名称获取PSD ID
        /// 
        /// 关卡名称
        /// 
        public Tuple GetPSDIDByLevelName(string name)
        {
            string psdID = "";
            LevelType levelType = LevelType.None;
            string[] arr = name.Split('_');
            if (arr.Length == 3)
            {
                psdID = arr[2];
                if (arr[0] == "MainLine")
                {
                    levelType = LevelType.MainLine;
                }
                else if (arr[0] == "Daily")
                {
                    levelType = LevelType.Daily;
                }
                else if (arr[0] == "Story")
                {
                    levelType = LevelType.Story;
                }
                else if (arr[0] == "Activity")
                {
                    levelType = LevelType.Activity;
                }
                else
                {
                    levelType = LevelType.None;
                }
            }
            return new Tuple(psdID, levelType);;
        }
        public void OnDestroy()
        {
            
        }
        public string GetTypeNameByType(LevelType type)
        {
            if (type == LevelType.MainLine)
            {
                return $"MainLine";
            }
            if (type == LevelType.Daily)
            {
                return $"Daily";
            }
            if (type == LevelType.Story)
            {
                return $"Story";
            }
            if (type == LevelType.Activity)
            {
                return $"Activity";
            }
            return "";
        }
    }
}