using System.Collections.Generic;
using UnityEngine;
using GFGGame;
using System;
namespace GFGEditor
{
    /// 
    /// 物品扫描器:
    /// 1.扫描获取途径,并填入物品表的获取途径字段
    /// 2.扫描套装部件个数
    /// 
    public class ItemApproachScanner
    {
        private delegate string GetApproachCall(int itemId);
        public static void startScan()
        {
            ItemCfg[] dataArray = ItemCfgArray.Instance.dataArray;
            GetApproachCall[] actions = new GetApproachCall[] { GetClothingShopApproach, GetLeaguePrayApproach, GetLeagueAnswerApproach, CheckStoreApproach, GetClothingSyntheticApproach, GetSuitGuideApproach, GetSuitSyntheticApproach, GetClothingDecomposeApproach, CheckClothingFosterApproach, CheckDailyTaskApproach, GetZhaiXingApproach, GetStoryLevelApproach };
            Dictionary> suitDic = new Dictionary>();
            int suitPartTotalCount = 0;
            foreach (ItemCfg cfg in dataArray)
            {
                HandleItemAndSuitTable(cfg, suitDic);
                HandleItemApproch(cfg, actions);
            }
            var globalCfg = GlobalCfgArray.globalCfg;
            SQLiteHelper.Instance.OpenConnection();
            try
            {
                //物品
                foreach (ItemCfg cfg in dataArray)
                {
                    var names = new string[] { nameof(cfg.subType), nameof(cfg.itemType), nameof(cfg.approach), nameof(cfg.isHide) };
                    var values = new string[] { "" + cfg.subType, "" + cfg.itemType, "" + cfg.approach, "" + cfg.isHide };
                    SQLiteHelper.Instance.UpdateValues(nameof(ItemCfgArray), names, values, nameof(cfg.id), cfg.id.ToString());
                }
                //套装
                foreach (var a in suitDic)
                {
                    if (a.Key.isHide <= 0)
                    {
                        suitPartTotalCount += a.Value.Count;
                    }
                    var names = new string[] { nameof(a.Key.partsArr).Replace("Arr", "") };
                    var values = new string[] { string.Join(";", a.Value) };
                    SQLiteHelper.Instance.UpdateValues(nameof(SuitCfgArray), names, values, nameof(a.Key.id), a.Key.id.ToString());
                }
                globalCfg.suitPartTotalCount = suitPartTotalCount;
                UpdateGlobalCfg(globalCfg);
            }
            catch (Exception e)
            {
                ET.Log.Error(e.ToString());
            }
            finally
            {
                SQLiteHelper.Instance.CloseConnection();
            }
        }
        private static void UpdateGlobalCfg(GlobalCfg globalCfg)
        {
            var names = new string[] { nameof(globalCfg.suitPartTotalCount) };
            var values = new string[] { globalCfg.suitPartTotalCount.ToString() };
            SQLiteHelper.Instance.UpdateValues(nameof(GlobalCfgArray), names, values, nameof(globalCfg.id), globalCfg.id.ToString());
        }
        private static void HandleItemAndSuitTable(ItemCfg itemCfg, Dictionary> suitDic)
        {
            if (itemCfg.suitId <= 0)
            {
                return;
            }
            var suitCfg = SuitCfgArray.Instance.GetCfg(itemCfg.suitId);
            if (suitCfg == null)
            {
                return;
            }
            if (itemCfg.itemType == ConstItemType.DRESS_UP)
            {
                suitDic.TryGetValue(suitCfg, out var partsList);
                if (partsList == null)
                {
                    partsList = new List();
                    suitDic[suitCfg] = partsList;
                }
                partsList.Add(itemCfg.id);
            }
            itemCfg.isHide = suitCfg.isHide;
        }
        private static void HandleItemApproch(ItemCfg cfg, GetApproachCall[] actions)
        {
            if (cfg.id == 3000001 || cfg.id == 3000002 || cfg.id == 3000003)
            {
                ET.Log.Debug("");
            }
            List approachs = new List();
            foreach (GetApproachCall action in actions)
            {
                string tempApproach = action(cfg.id);
                if (tempApproach != null)
                {
                    approachs.Add(tempApproach);
                }
            }
            if (approachs.Count > 0)
            {
                string approach = string.Join(";", approachs);
                cfg.approach = approach;
                if (approachs.Count > 5)
                {
                    Debug.LogFormat("物品{0}的获取途径为 {1}", cfg.name, approach);
                }
            }
            else
            {
                if (cfg.id < 1000000 || cfg.id >= 2000000)
                {
                    if ((!cfg.name.Contains("原始") && !cfg.name.Contains("默认")) || cfg.id >= 1000000)
                    {
                        //Debug.LogErrorFormat("物品 {0} {1} 没有获取途径", cfg.id, cfg.name);
                    }
                }
            }
        }
        /// 
        /// 尝试获取服装店产出途径
        /// 
        /// 
        /// 
        private static string GetClothingShopApproach(int itemId)
        {
            List dataArray = ShopCfgArray.Instance.GetCfgsBymenu1Andmenu2(ConstStoreTabId.FU_ZHUANG_DIAN, ConstStoreSubId.FU_ZHUANG_DIAN);
            foreach (ShopCfg cfg in dataArray)
            {
                if (cfg.itemId == itemId)
                {
                    return "FU_ZHUANG_DIAN=" + ConstStoreId.CLOTHING_STORE_ID; ;
                }
            }
            List shopCfgGallerys = ShopCfgArray.Instance.GetCfgsBymenu1Andmenu2(ConstStoreTabId.STORE_EXCHANGE, ConstStoreSubId.STORE_EXCHANGE_GALLERY);
            foreach (ShopCfg cfg in shopCfgGallerys)
            {
                if (cfg.itemId == itemId)
                {
                    return "FU_ZHUANG_DIAN=" + ConstStoreId.GALLERY_STORE_ID;
                }
            }
            return null;
        }
        /// 
        /// 尝试获取服装分解产出途径
        /// 
        /// 
        /// 
        private static string GetClothingDecomposeApproach(int itemId)
        {
            DecomposeCfgArray manager = DecomposeCfgArray.Instance;
            DecomposeCfg[] dataArray = manager.dataArray;
            foreach (DecomposeCfg cfg in dataArray)
            {
                for (int i = 0; i < cfg.itemsArr.Length; i++)
                {
                    if (cfg.itemsArr[i][0] == itemId)
                    {
                        return "FU_ZHUANG_DECOMPOSE";
                    }
                }
            }
            return null;
        }
        /// 
        /// 尝试获取摘星(抽奖)产出途径
        /// 
        /// 
        /// 
        private static string GetZhaiXingApproach(int itemId)
        {
            LuckyBoxCfgArray manager = LuckyBoxCfgArray.Instance;
            LuckyBoxCfg[] dataArray = manager.dataArray;
            if (itemId == 10090)
            {
                Debug.Log("");
            }
            foreach (LuckyBoxCfg cfg in dataArray)
            {
                for (int i = 0; i < cfg.bonusArr.Length; i++)
                {
                    if (itemId == cfg.bonusArr[i][0])
                    {
                        return "ZHAI_XING";
                    }
                }
                int dropId = cfg.dropId;
                if (itemId == 10014)
                {
                    if (dropId == 11000008)
                    {
                        ET.Log.Debug("");
                    }
                    ET.Log.Debug("");
                }
                int[] _dropIds = { dropId };
                bool approach = CheckItemInDropOut(itemId, _dropIds);
                if (approach)
                {
                    return "ZHAI_XING";
                }
                // }
            }
            return null;
        }
        /// 
        /// 尝试获取剧情关卡产出途径
        /// 
        /// 
        /// 
        private static string GetStoryLevelApproach(int itemId)
        {
            List approachs = new List();
            StoryLevelCfgArray manager = StoryLevelCfgArray.Instance;
            StoryLevelCfg[] dataArray = manager.dataArray;
            foreach (StoryLevelCfg cfg in dataArray)
            {
                if (itemId == 3000041)
                {
                    ET.Log.Debug("");
                }
                if (itemId == 3002001)
                {
                }
                bool result = CheckItemInBonus(itemId, cfg);
                if (result)
                {
                    string approach = "JU_QING_GUAN_QIA=" + cfg.id;
                    approachs.Add(approach);
                }
            }
            if (approachs.Count > 0)
            {
                return string.Join(";", approachs);
            }
            return null;
        }
        /// 
        /// 尝试获取服装合成产出途径
        /// 
        /// 
        /// 
        private static string GetClothingSyntheticApproach(int itemId)
        {
            var itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
            if (itemCfg == null)
            {
                return null;
            }
            if (itemCfg.itemType != ConstItemType.DRESS_UP)
            {
                return null;
            }
            if (itemCfg.suitId <= 0)
            {
                return null;
            }
            var suitCfg = SuitCfgArray.Instance.GetCfg(itemCfg.suitId);
            if (suitCfg == null)
            {
                return null;
            }
            if (suitCfg.syntheticType <= 0)
            {
                return null;
            }
            return "FU_ZHUANG_HE_CHENG=" + suitCfg.id;
        }
        private static string GetSuitGuideApproach(int itemId)
        {
            SuitCfgArray manager = SuitCfgArray.Instance;
            SuitCfg[] dataArray = manager.dataArray;
            foreach (SuitCfg cfg in dataArray)
            {
                bool result = CheckItemInBonus(itemId, cfg.guideBoxBonusArr);
                if (result)
                {
                    return "TAO_ZHUANG_TU_JIAN";
                }
            }
            return null;
        }
        private static string GetSuitSyntheticApproach(int itemId)
        {
            var itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
            if (itemCfg == null)
            {
                return null;
            }
            if (itemCfg.itemType != ConstItemType.DRESS_UP)
            {
                return null;
            }
            if (itemCfg.suitId <= 0)
            {
                return null;
            }
            var suitCfg = SuitCfgArray.Instance.GetCfg(itemCfg.suitId);
            if (suitCfg == null)
            {
                return null;
            }
            if (suitCfg.syntheticType <= 0)
            {
                return null;
            }
            bool result = CheckItemInBonus(itemId, suitCfg.syntheticBoxBonusArr);
            if (result)
            {
                return "TAO_ZHUANG_HE_CHENG";
            }
            return null;
        }
        private static bool CheckItemInBonus(int itemId, StoryLevelCfg cfg)
        {
            if (cfg.fightID.Length > 0)
            {
                StoryFightCfgArray manager = StoryFightCfgArray.Instance;
                StoryFightCfg storyFightCfg = manager.GetCfg(cfg.fightID);
                List a = new List();
                // a.AddRange(cfg.bonusOnceArr);
                a.AddRange(storyFightCfg.bonusBaseArr);
                int[][] bonus = a.ToArray();
                bool result = CheckItemInBonus(itemId, bonus);
                if (storyFightCfg.id == "501")
                {
                    ET.Log.Debug("");
                }
                if (result)
                {
                    return true;
                }
                result = CheckItemInDropOut(itemId, storyFightCfg.bonusRandomArr);
                return result;
            }
            return false;
        }
        private static bool CheckItemInBonus(int itemId, int[][] bosnu)
        {
            List aList = ItemUtil.CreateItemDataList(bosnu);
            foreach (ItemData itemData in aList)
            {
                if (itemId == itemData.id)
                {
                    return true;
                }
            }
            return false;
        }
        /// 
        /// 检测掉落表是否产出该物品
        /// 
        /// 
        /// 
        /// 
        private static bool CheckItemInDropOut(int itemId, int[] dropIds)
        {
            List result = new List();
            foreach (int dropId in dropIds)
            {
                DropOutCfgArray manager = DropOutCfgArray.Instance;
                List aList = manager.GetCfgsByid(dropId);
                foreach (DropOutCfg dropOutCfg in aList)
                {
                    if (manager.GetCfgsByid(dropOutCfg.item).Count > 0)
                    {
                        int[] _dropIds = { dropOutCfg.item };
                        if (CheckItemInDropOut(itemId, _dropIds))
                        {
                            return true;
                        }
                    }
                    else
                    {
                        if (itemId == dropOutCfg.item)
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        /// 
        /// 检测商城-灵犀卡是否产出该物品
        /// 
        /// 
        /// 
        private static string CheckStoreApproach(int itemId)
        {
            List approachs = new List();
            List goldShopCfgs = ShopCfgArray.Instance.GetCfgsBymenu1Andmenu2(ConstStoreTabId.STORE_MONTH_CARD, ConstStoreSubId.STORE_MONTH_GOLD_CARD);
            for (int i = 0; i < goldShopCfgs.Count; i++)
            {
                if (CheckIsHasItem(itemId, goldShopCfgs[i].itemId))
                {
                    approachs.Add(ConstFunctionId.STORE + "=" + ConstStoreTabId.STORE_MONTH_CARD + "=" + ConstStoreSubId.STORE_MONTH_GOLD_CARD);
                    break;
                }
            }
            List blackShopCfgs = ShopCfgArray.Instance.GetCfgsBymenu1Andmenu2(ConstStoreTabId.STORE_MONTH_CARD, ConstStoreSubId.STORE_MONTH_BLACK_CARD);
            for (int i = 0; i < blackShopCfgs.Count; i++)
            {
                if (CheckIsHasItem(itemId, blackShopCfgs[i].itemId))
                {
                    approachs.Add(ConstFunctionId.STORE + "=" + ConstStoreTabId.STORE_MONTH_CARD + "=" + ConstStoreSubId.STORE_MONTH_BLACK_CARD);
                    break;
                }
            }
            List itemShopCfgs = ShopCfgArray.Instance.GetCfgsBymenu1Andmenu2(ConstStoreTabId.STORE_GIFT_BAG, ConstStoreSubId.STORE_GIFT_BAG_TIME_LIMIT);
            for (int i = 0; i < itemShopCfgs.Count; i++)
            {
                if (CheckIsHasItem(itemId, itemShopCfgs[i].itemId))
                {
                    approachs.Add(ConstFunctionId.STORE + "=" + ConstStoreTabId.STORE_GIFT_BAG + "=" + ConstStoreSubId.STORE_GIFT_BAG_TIME_LIMIT);
                    break;
                }
            }
            List greatestShopCfgs = ShopCfgArray.Instance.GetCfgsBymenu1Andmenu2(ConstStoreTabId.STORE_GIFT_BAG, ConstStoreSubId.STORE_GIFT_BAG_GREATEST);
            for (int i = 0; i < greatestShopCfgs.Count; i++)
            {
                if (CheckIsHasItem(itemId, greatestShopCfgs[i].itemId))
                {
                    approachs.Add(ConstFunctionId.STORE + "=" + ConstStoreTabId.STORE_GIFT_BAG + "=" + ConstStoreSubId.STORE_GIFT_BAG_GREATEST);
                    break;
                }
            }
            List exchangetShopCfgs = ShopCfgArray.Instance.GetCfgsBymenu1Andmenu2(ConstStoreTabId.STORE_EXCHANGE, ConstStoreSubId.STORE_EXCHANGE_LEAGUE);
            for (int i = 0; i < exchangetShopCfgs.Count; i++)
            {
                if (CheckIsHasItem(itemId, exchangetShopCfgs[i].itemId))
                {
                    approachs.Add(ConstFunctionId.STORE + "=" + ConstStoreTabId.STORE_EXCHANGE + "=" + ConstStoreSubId.STORE_EXCHANGE_LEAGUE);
                    break;
                }
            }
            if (approachs.Count > 0)
            {
                return string.Join(";", approachs);
            }
            return null;
        }
        private static bool CheckIsHasItem(int itemId, int checkId)
        {
            ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(checkId);
            if (itemCfg.itemType == ConstItemType.USEABLE)
            {
                for (int i = 0; i < itemCfg.itemsArr.Length; i++)
                {
                    if (itemCfg.itemsArr[i][0] == itemId) return true;
                }
            }
            else if (checkId == itemId)
            {
                return true;
            }
            return false;
        }
        /// 
        /// 检测服装升级是否产出该物品
        /// 
        /// 
        /// 
        private static string CheckClothingFosterApproach(int itemId)
        {
            //培养
            SuitFosterCfgArray manager = SuitFosterCfgArray.Instance;
            SuitFosterCfg[] dataArray = manager.dataArray;
            for (int i = 0; i < dataArray.Length; i++)
            {
                for (int j = 0; j < dataArray[i].rewardsArr.Length; j++)
                {
                    if (dataArray[i].rewardsArr[j][0] == itemId)
                    {
                        return ConstFunctionId.SUIT_FOSTER;
                    }
                }
            }
            //换新
            SuitFosterListCfgArray manager1 = SuitFosterListCfgArray.Instance;
            SuitFosterListCfg[] dataArray1 = manager1.dataArray;
            for (int i = 0; i < dataArray1.Length; i++)
            {
                for (int j = 0; j < dataArray1[i].renewRewardsArr.Length; j++)
                {
                    if (dataArray1[i].renewRewardsArr[j][0] == itemId)
                    {
                        return ConstFunctionId.SUIT_FOSTER;
                    }
                }
            }
            return null;
        }
        /// 
        /// 检测每日任务是否产出该物品
        /// 
        /// 
        /// 
        private static string CheckDailyTaskApproach(int itemId)
        {
            DailyActiveRewardCfgArray manager = DailyActiveRewardCfgArray.Instance;
            DailyActiveRewardCfg[] dataArray = manager.dataArray;
            for (int i = 0; i < dataArray.Length; i++)
            {
                for (int j = 0; j < dataArray[i].rewardsArr.Length; j++)
                {
                    if (dataArray[i].rewardsArr[j][0] == itemId)
                    {
                        return ConstFunctionId.DAILY_TASK;
                    }
                }
            }
            return null;
        }
        /// 
        /// 检测雅集福利是否产出该物品
        /// 
        /// 
        /// 
        private static string GetLeaguePrayApproach(int itemId)
        {
            LeaguePrayCfg[] prayCfgs = LeaguePrayCfgArray.Instance.dataArray;
            for (int i = 0; i < prayCfgs.Length; i++)
            {
                for (int j = 0; j < prayCfgs[i].bonusArr.Length; j++)
                {
                    if (prayCfgs[i].bonusArr[j][0] == itemId)
                    {
                        return ConstFunctionId.LEAGUE_PRAY;
                    }
                }
                for (int j = 0; j < prayCfgs[i].bonusOnceArr.Length; j++)
                {
                    if (prayCfgs[i].bonusOnceArr[j][0] == itemId)
                    {
                        return ConstFunctionId.LEAGUE_PRAY;
                    }
                }
            }
            return null;
        }
        /// 
        /// 检测雅集答题是否产出该物品
        /// 
        /// 
        /// 
        private static string GetLeagueAnswerApproach(int itemId)
        {
            LeagueQuestionCfg[] questionCfgs = LeagueQuestionCfgArray.Instance.dataArray;
            for (int i = 0; i < questionCfgs.Length; i++)
            {
                for (int j = 0; j < questionCfgs[i].bonusArr.Length; j++)
                {
                    if (questionCfgs[i].bonusArr[j][0] == itemId)
                    {
                        return ConstFunctionId.LEAGUE_ANSWER;
                    }
                }
            }
            LeagueQuestionBonusCfg[] questionBonusCfgs = LeagueQuestionBonusCfgArray.Instance.dataArray;
            for (int i = 0; i < questionBonusCfgs.Length; i++)
            {
                for (int j = 0; j < questionBonusCfgs[i].bonusArr.Length; j++)
                {
                    if (questionBonusCfgs[i].bonusArr[j][0] == itemId)
                    {
                        return ConstFunctionId.LEAGUE_ANSWER;
                    }
                }
            }
            return null;
        }
    }
}