using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GCGame.Table;

// 福利大厅 - 资源找回
//8128	资源找回	{0}总共可追回资源:
//8129	资源找回 可追回1次
//8130	资源找回	{0}VIP追回
//8131	资源找回 银票追回
//8132	资源找回 一键追回
//8133	资源找回 完美追回
//8134	资源找回 是否使用 {0}追回70%资源
//8135	资源找回 是否使用 {0}追回100%资源
//8136	资源找回 开启 {0}之后,可以使用完美追回,追回100%的资源!是否前往激活{1}
public class WelfareGetBackAwardCtr : WelfarePageBaseCS {

    private static WelfareGetBackAwardCtr instance;
    public static WelfareGetBackAwardCtr Instance
    {
        get { return instance; }
    }

    public Button normalGetBtn;
    public Text normalDesc;
    public SimpleItem normalItem;

    public Button specialGetBtn;
    public Text specialDesc;
    public SimpleItem specialItem;

    public ItemContainerTipCtr messageBox;
    public UIContainerBase container;

    //public Text noResTip;               // 无资源是中间文字提示
    public Text tips;

    private List<ResourceRecoveryInfo> data;
    private List<int> hasGetIndexs;

    private int normalMoneyType = -1;
    private int normalTotalMoney = -1;
    private int specialMoneyType;
    private int specialTotalMoney;

    private void Awake()
    {
        if(instance == null)
        {
            instance = this;
            normalGetBtn.onClick.AddListener(OnNormalGetClick);
            specialGetBtn.onClick.AddListener(OnSpecialGetClick);
        }

        normalDesc.text = StrDictionary.GetClientDictionaryString("#{8132}");
        specialDesc.text = StrDictionary.GetClientDictionaryString("#{8133}");
        hasGetIndexs = new List<int>();

        // 普通追回按钮,没有会变的可能,所以仅进行一次赋值
        normalDesc.text = StrDictionary.GetClientDictionaryString("#{8132}");
        specialDesc.text = StrDictionary.GetClientDictionaryString("#{8133}");
        tips.text = StrDictionary.GetClientDictionaryString("#{60031}");
    }

    private void OnDestroy()
    {
        instance = null;
    }

    public override void OnPacketRec(NodePageInfoRet packet) { /* do nothing*/ }

    public void OnPacketRec(RetResourceRecoveryInfo packet)
    {
        data = packet.info;
        hasGetIndexs.Clear();
        for(int i = 0; i < data.Count; ++i)
        {
            if(data[i].RewardState == 2)
            {
                hasGetIndexs.Add(i);
            }
        }

        container.InitContentItem(data);
        UpdateBtnAndMoney();
    }

    private void UpdateBtnAndMoney()
    {
        // 所有奖励都拿了
        if (hasGetIndexs.Count == data.Count)
        {
            //noResTip.gameObject.SetActive(true);
            //noResTip.text = StrDictionary.GetClientDictionaryString("#{60032}");

            normalItem.gameObject.SetActive(false);
            specialItem.gameObject.SetActive(false);
        }
        else
        {
            normalTotalMoney = 0;
            specialTotalMoney = 0;
            normalMoneyType = data[0].ConsumeType[0].MoneyType;
            specialMoneyType = data[0].ConsumeType[1].MoneyType;

            normalItem.gameObject.SetActive(true);
            specialItem.gameObject.SetActive(true);
            //noResTip.gameObject.SetActive(false);
            for (int i = 0; i < data.Count; ++i)
            {
                if (!hasGetIndexs.Contains(i))
                {
                    normalTotalMoney += data[i].ConsumeType[0].MoneyNum;
                    specialTotalMoney += data[i].ConsumeType[1].MoneyNum;
                }
            }

            normalItem.ShowMoney(normalMoneyType, normalTotalMoney);
            specialItem.ShowMoney(specialMoneyType, specialTotalMoney);
        }
    }

    private void OnNormalGetClick()
    {
        // 没有可领项
        if (hasGetIndexs.Count == data.Count)
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{60033}"));
            return;
        }

        long curMoney = GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_COIN_BIND);
        long replaceMoney = GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_COIN);
        if (curMoney < normalTotalMoney)
        {
            MessageBoxLogic.OpenOKCancelBox(StrDictionary.GetClientDictionaryString("#{60034}", normalTotalMoney - curMoney), null,
                () =>
                {
                    if (JudgeMoneyLogic.IsMoneyEnough((MONEYTYPE)normalMoneyType, normalTotalMoney))
                    {
                        string title = StrDictionary.GetClientDictionaryString("#{8132}");
                        string moneyType = GCGame.Utils.GetMoneyName(normalMoneyType);
                        string desc = StrDictionary.GetClientDictionaryString("#{8134}", normalTotalMoney + moneyType);
                        messageBox.ShowTip(title, desc, GetTotalItem(), NormalGet, null);
                    }
                });
        }
        else
        {
            string title = StrDictionary.GetClientDictionaryString("#{8132}");
            string moneyType = GCGame.Utils.GetMoneyName(normalMoneyType);
            string desc = StrDictionary.GetClientDictionaryString("#{8134}", normalTotalMoney + moneyType);
            messageBox.ShowTip(title, desc, GetTotalItem(), NormalGet, null);
        }
    }

    private void OnSpecialGetClick()
    {
        // 没有可领项
        if (hasGetIndexs.Count == data.Count)
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{60033}"));
            return;
        }

        //GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr
        //if()
        //Tab_PrivilegeVip tab = TableManager.GetPrivilegeVipByID(data[0].PrivilegeVipType, 0);
        //if (tab != null && !GameManager.gameManager.PlayerDataPool.PrivilegeTypeStateDic[tab.VipType])
        //{
        //    string tip = StrDictionary.GetClientDictionaryString("#{8136}", tab.Name, tab.Name);
        //    MessageBoxLogic.OpenOKCancelBox(tip, null,
        //        () =>
        //        {
        //            YuanBaoShopLogic.OpenVipPage();
        //        });
        //}
        if(!JudgeMoneyLogic.IsMoneyEnough((MONEYTYPE)specialMoneyType, specialTotalMoney))
        {
            return;
        }
        else
        {
            string title = StrDictionary.GetClientDictionaryString("#{8133}");
            string moneyType = GCGame.Utils.GetMoneyName(specialMoneyType);
            string desc = StrDictionary.GetClientDictionaryString("#{8135}", specialTotalMoney + moneyType);
            messageBox.ShowTip(title, desc, GetTotalItem(), SpecialGet, null);
        }
    }

    private void NormalGet()
    {
        ReqResourceRecoveryReward req = new ReqResourceRecoveryReward();
        req.ResourceType = data[0].ResourceType;
        req.SubType = data[0].SubType;
        req.RecoveryType = 1;
        req.IsTotal = 1;

        req.SendMsg();
    }

    private void SpecialGet()
    {
        ReqResourceRecoveryReward req = new ReqResourceRecoveryReward();
        req.ResourceType = data[0].ResourceType;
        req.SubType = data[0].SubType;
        req.RecoveryType = 2;
        req.IsTotal = 1;

        req.SendMsg();
    }

    private IEnumerable<WelfareRew> GetTotalItem()
    {
        if (data == null)
        {
            return null;
        }

        Dictionary<int, WelfareRew> itemDic = new Dictionary<int, WelfareRew>();
        for (int i = 0; i < data.Count; ++i)
        {
            if (!hasGetIndexs.Contains(i))
            {
                for (int j = 0; j < data[i].RewardItem.Count; ++j)
                {
                    if (itemDic.ContainsKey(data[i].RewardItem[j]._ItemId))
                    {
                        itemDic[data[i].RewardItem[j]._ItemId]._ItemNum += data[i].RewardItem[j]._ItemNum;
                    }
                    else
                    {
                        WelfareRew newItem = new WelfareRew();
                        newItem._ItemId = data[i].RewardItem[j]._ItemId;
                        newItem._ItemNum = data[i].RewardItem[j]._ItemNum;
                        itemDic[data[i].RewardItem[j]._ItemId] = newItem;
                    }
                }
            }
        }

        return itemDic.Values;
    }

    // 服务器返回已经寻回资源的活动
    public void OnResultRec(RetResourceRecoveryReward packet)
    {
        container.ForeachActiveItem<WelfareResGetBackTag>(
            (WelfareResGetBackTag item) =>
            {
                if (item.IsMatch(packet.ResourceType, packet.SubType))
                {
                    item.SetState(true);
                }
            });

        for (int i = 0; i < data.Count; ++i)
        {
            if (data[i].ResourceType == packet.ResourceType && data[i].SubType == packet.SubType && !hasGetIndexs.Contains(i))
            {
                hasGetIndexs.Add(i);
            }
        }

        UpdateBtnAndMoney();
    }
}