using GCGame.Table;
using UnityEngine;
using UnityEngine.UI;

public class IntegralShopCtr : MonoBehaviour
{
    public enum IntegralType
    {
        HUOLI = 1, //活力
        MENPAI,
        ZHANCHANG,
        JINGJI
    }


    public static IntegralShopCtr Instance;


    //当前选择的数量
    private int CurSelectedCout = 1; //默认的是1
    private int CurSelectedItemId = -1;
    private int ItemCost = -1;
    private int ShopType = -1;

    private void Awake()
    {
        Instance = this;
    }

    // Use this for initialization
    private void Start()
    {
    }

    // Update is called once per frame
    private void Update()
    {
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    public void InitMyIntegralShopItem(int type) //Type跟Id相对应
    {
        ShopType = type;
        var roleIntegralShop = TableManager.GetRoleIntegralShopByID(type);
        if (roleIntegralShop != null)
        {
            //初始化item
            var roleIntegral = TableManager.GetRoleIntegralByID(type);
            if (roleIntegral != null)
            {
                ShopName.text = roleIntegral.IntegralName; //设置商店名字
                for (var index = 0; index < roleIntegralShop.getItemCount(); index++)
                {
                    var commonItem = TableManager.GetCommonItemByID(roleIntegralShop.GetItembyIndex(index));
                    if (commonItem == null) break;

                    //创建item
                    var integralItemPrefab = Instantiate(IntrgralShopItemPreResource);

                    //设置比例
                    integralItemPrefab.transform.parent = PrefabParent;

                    integralItemPrefab.transform.localPosition = Vector3.zero;
                    integralItemPrefab.transform.localScale = Vector3.one;
                    integralItemPrefab.transform.localRotation = Quaternion.Euler(Vector3.zero);

                    //第一个设置成打开状态
                    if (index == 0)
                    {
                        integralItemPrefab.GetComponent<Toggle>().isOn = true;
                        InitMyDescPanel(commonItem.Id);

                        CurSelectedItemId = commonItem.Id;
                        CurSelectedCout = 1;
                        SubBtnMask.gameObject.SetActive(true); //1不能再减
                        CountMyTotalCost();
                    }

                    integralItemPrefab.GetComponent<Toggle>().group = MyToggleGroup;
                    integralItemPrefab.GetComponent<IntegralShopItem>().InitMyItem(commonItem.Icon, commonItem.Name,
                        roleIntegralShop.GetCostbyIndex(index), type, commonItem.Id);
                }
            }
        }
    }

    public void InitItemType(int classId)
    {
        switch (classId)
        {
            case 1:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{1046}");
                break;
            case 2:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{86701}");
                break;
            case 3:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{86702}");
                break;
            case 4:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{86703}");
                break;
            case 5:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{1697}");
                break;
            case 6:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{86705}");
                break;
            case 7:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{1879}");
                break;
            case 8:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{1880}");
                break;
            case 9:
                DescItemType.text = StrDictionary.GetClientDictionaryString("#{86708}");
                break;
        }
    }

    //初始化当前选择Item的描述Panel
    public void InitMyDescPanel(int itemId)
    {
        if (itemId == CurSelectedItemId) return;


        ItemCost = GetItemCost(itemId);
        var commonItem = TableManager.GetCommonItemByID(itemId);
        if (commonItem != null)
        {
            LoadAssetBundle.Instance.SetImageSprite(DescItemIcon, commonItem.Icon);
            if (commonItem.QualityEffect > 0)
                CommonItemContainerItem.ShowQualityEffect(true, commonItem.QualityEffect, DescItemIcon.transform);
            else
                CommonItemContainerItem.ShowQualityEffect(false, commonItem.QualityEffect, DescItemIcon.transform);

            DescItemname.text = commonItem.Name;
            DescItemType.text = "";
            DescItemDesc.text = StrDictionary.GetClientString_WithNameSex(commonItem.Tips);
        }

        CurSelectedCout = 1;
        CountText.text = "1";
        CurSelectedItemId = itemId;

        InitItemType(commonItem.ClassID);

        //初始化数量为1  屏蔽减少按钮
        if (!SubBtnMask.gameObject.activeInHierarchy)
            SubBtnMask.gameObject.SetActive(true);


        CountMyTotalCost();
    }

    //获取当前选择的Item的Cost
    public int GetItemCost(int itemId)
    {
        var Shop = TableManager.GetRoleIntegralShopByID(ShopType);
        if (Shop != null)
            for (var index = 0; index < Shop.getItemCount(); index++)
                if (itemId == Shop.GetItembyIndex(index))
                    return Shop.GetCostbyIndex(index);

        return -1;
    }

    //显示需要的花费
    public void CountMyTotalCost()
    {
        TotalCostText.text = (ItemCost * CurSelectedCout).ToString();
    }

    //判断增加或者减少按钮是否需要遮罩
    public void AdjustMyMaskImage()
    {
        if (CurSelectedCout == 1)
            if (!SubBtnMask.gameObject.activeInHierarchy)
                SubBtnMask.gameObject.SetActive(true);

        if (CurSelectedCout > 1 && CurSelectedCout < 99)
        {
            if (SubBtnMask.gameObject.activeInHierarchy)
                SubBtnMask.gameObject.SetActive(false);
            if (AddBtnMask.gameObject.activeInHierarchy)
                AddBtnMask.gameObject.SetActive(false);
        }

        if (CurSelectedCout == 99)
        {
            if (!AddBtnMask.gameObject.activeInHierarchy)
                AddBtnMask.gameObject.SetActive(true);
        }
    }

    public void OnSubBtnClick()
    {
        CurSelectedCout--;
        CountText.text = CurSelectedCout.ToString();
        CountMyTotalCost();
        AdjustMyMaskImage();
    }

    public void OnAddBtnClick()
    {
        CurSelectedCout++;
        CountText.text = CurSelectedCout.ToString();
        CountMyTotalCost();
        AdjustMyMaskImage();
    }

    public void OnBuyButtonClick()
    {
        //购买协议
    }

    public void OnCloseItemClick()
    {
        //UIManager.CloseUI(UIInfo.IntegralShop);
    }

    #region

    public Text ShopName;
    public Text DescItemname;
    public Text DescItemType;
    public Text DescItemDesc;
    public Image DescItemIcon;

    public Button AddBtn;
    public Image AddBtnMask;
    public Button SubBtn;
    public Image SubBtnMask;

    public Text CountText;
    public Text TotalCostText;
    public Text OwnMoneyCost;

    public GameObject IntrgralShopItemPreResource;
    public Transform PrefabParent;

    public ToggleGroup MyToggleGroup;

    #endregion
}