using ET;
using FairyGUI;
using System;
using System.Collections;
using System.Collections.Generic;
using UI.CommonGame;
using UnityEngine;
namespace GFGGame
{
    public class ItemUtil
    {
        /// 是否弹购买成功飘字,默认是
        /// 是否打开来源界面。默认否
        /// 是否显示购买提示
        public static void ExchangeItemById(int itemId, int num, bool showTips = true, Action onSuccess = null, bool openSource = false, int maxCount = 9990, bool showTxtBuyTips = false, string prefix = "")
        {
            if (itemId == ConstItemID.GOLD)
            {
                AddGold(num, onSuccess);
            }
            else if (itemId == ConstItemID.POWER)
            {
                AddPower();
            }
            else
            {
                BuyItemConteoller.Show(itemId, num, ConstBuyType.TYPE_ITEM, 0, onSuccess, showTips, openSource, maxCount);
                BuyItemConteoller.showTxtBuyTips = showTxtBuyTips;
            }
        }
        public static void AddPower(string prefix = "", Action onSuccess = null)
        {
            int maxLimit = ItemExchangeCfgArray.Instance.GetCfg(ConstItemID.POWER).maxLimit;
            int lastBuyCount = maxLimit - ItemDataManager.GetItemExchangeTimes(ConstItemID.POWER);
            string showTxt = string.Format("每5分钟回复1点体力\n今日剩余购买{0}/{1}次", lastBuyCount, maxLimit);
            BuyConfirmController.Show(ConstItemID.POWER, 1, () =>
            {
                if (onSuccess != null)
                {
                    onSuccess();
                }
            }, showTxt);
        }
        public static void AddGold(int value = 0, Action onSuccess = null)
        {
            BuyConfirmController.Show(ConstItemID.GOLD, 1, () =>
            {
                if (onSuccess != null)
                {
                    onSuccess();
                }
            });
        }
        public static void AddDiamondPurple()
        {
            ViewManager.Show(ViewName.RECHARGE_STORE_VIEW);
        }
        //红钻(鲛绡)
        public static void AddDiamondRed(int value = 0, Action onSuccess = null)
        {
            ItemExchangeCfg currencyRatioCfg = ItemExchangeCfgArray.Instance.GetCfg(ConstItemID.DIAMOND_RED);// GetCurrencyRatioCfgById(ConstItemID.DIAMOND_RED);
            BuyItemConteoller.Show(currencyRatioCfg.id, value > 0 ? value : 1, ConstBuyType.TYPE_ITEM, 0, onSuccess, true, true);
            BuyItemConteoller.showTxtBuyTips = true;
        }
        public static string GetItemName(int itemId)
        {
            ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
            return itemCfg.name;
        }
        public static string GetSuitName(int suitId)
        {
            SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
            return suitCfg.name;
        }
        public static int GetItemRarity(int itemId)
        {
            ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
            return itemCfg.rarity;
        }
        public static List CreateItemDataList(List items)
        {
            List itemList = new List();
            for (int i = 0; i < items.Count; i++)
            {
                ItemData itemData = createItemData(items[i].ConfigId, items[i].Count);
                itemList.Add(itemData);
            }
            return itemList;
        }
        public static List CreateItemDataList(int[][] bonus, bool isOnceBonus = false)
        {
            List itemList = new List();
            foreach (int[] item in bonus)
            {
                ItemData itemData = createItemData(item);
                itemList.Add(itemData);
                itemData.isOnceBonus = isOnceBonus;
            }
            return itemList;
        }
        public static List CreateItemDataList(int[][] bonus, int num)
        {
            List itemList = new List();
            foreach (int[] item in bonus)
            {
                int count = 0;
                if (item.Length > 1)
                {
                    count = item[1];
                }
                else
                {
                    count = 1;
                }
                ItemData itemData = createItemData(item[0], count * num);
                itemList.Add(itemData);
            }
            return itemList;
        }
        public static List CreateItemDataList(int itemId, int count)
        {
            List itemList = new List();
            ItemData itemData = createItemData(itemId, count);
            itemList.Add(itemData);
            return itemList;
        }
        public static ItemData createItemData(int[] bonus)
        {
            int itemID = bonus[0];
            int itemNum = 1;
            if (bonus.Length > 1)
            {
                itemNum = bonus[1];
            }
            return createItemData(itemID, itemNum);
        }
        public static ItemData createItemData(int itemId, int count)
        {
            ItemData itemData = ItemDataPool.GetItemData(itemId);
            itemData.num = count;
            return itemData;
        }
        public static Boolean CheckItemEnough(int itemId, int num)
        {
            int hasNum = ItemDataManager.GetItemNum(itemId);
            return hasNum >= num;
        }
        public static bool CheckMenuType1(int needItemId, int needSuitId, int typeId)
        {
            DressUpMenuItemCfg1 cfg1 = DressUpMenuItemCfg1Array.Instance.GetCfg(typeId);
            if (needItemId > 0)
            {
                int subType = ItemUtilCS.GetItemSubType(needItemId);
                if (subType == cfg1.type)
                {
                    return true;
                }
                else if (cfg1.subMenusArr.Length > 0)
                {
                    foreach (int id2 in cfg1.subMenusArr)
                    {
                        DressUpMenuItemCfg2 cfg2 = DressUpMenuItemCfg2Array.Instance.GetCfg(id2);
                        if (cfg2.type == subType)
                        {
                            return true;
                        }
                    }
                }
            }
            else if (needSuitId > 0 && cfg1.type == ConstDressUpItemType.TAO_ZHUANG)
            {
                return true;
            }
            return false;
        }
        public static void SortItemIdsByOwned(int[] array)
        {
            Array.Sort(array, (int a, int b) =>
            {
                bool ownedA = ItemDataManager.GetItemNum(a) > 0;
                bool ownedB = ItemDataManager.GetItemNum(b) > 0;
                if (!ownedB && ownedA)
                {
                    return 1;
                }
                else if (ownedB && !ownedA)
                {
                    return -1;
                }
                return 0;
            });
        }
        public static string GetItemResExt(int itemType, int type, bool isIcon = false)
        {
            if (itemType == ConstItemType.DRESS_UP && type == ConstDressUpItemType.BEI_JING && !isIcon)
            {
                return "jpg";
            }
            return "png";
        }
        public static void SetItemNumAndNeedNum(GObject obj, int itemId, int needNum, string color = "#D27869")
        {
            UI_ComCostCurrencyWithHas com = UI_ComCostCurrencyWithHas.Proxy(obj);
            int hasNum = ItemDataManager.GetItemNum(itemId);
            com.m_txtHas.text = StringUtil.GetColorText(NumberUtil.ChangeNumberUnit(hasNum), hasNum < needNum ? color : "#FFFFFF"); hasNum.ToString();
            com.m_txtNeed.text = needNum.ToString();
            ItemCfg cfg1 = ItemCfgArray.Instance.GetCfg(itemId);
            com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg1.res);
        }
        public static void SetItemNeedNum(GObject obj, int itemId, int needNum, string color = "#D27869")
        {
            UI_ComCostCurrency com = UI_ComCostCurrency.Proxy(obj);
            com.m_txtNeed.text = needNum.ToString();
            ItemCfg cfg1 = ItemCfgArray.Instance.GetCfg(itemId);
            com.m_loaIcon.url = ResPathUtil.GetCommonGameResPath(cfg1.res);
        }
    }
}