using UnityEngine;
using System.Collections;
using GCGame.Table;
using Module.Log;

public class JudgeMoneyLogic : MonoBehaviour
{
    public static JudgeMoneyLogic Instance;
    void Awake()
    {
        Instance = this;
    }

    void OnDestroy()
    {
        Instance = null;
    }

    /// <summary>
    /// 判断当前需要消耗类型的货币是否充足(参数1. 当前所需货币类型  2.当前所需类型货币的数量) (不足操作 :1.首充  2.VIP 3.累计充值, 4.正常购买灵玉)
    /// </summary>
    /// <param name="type">货币类型</param>
    /// <param name="count">货币需求数量</param>
    /// <returns></returns>
    public static bool IsMoneyEnough(MONEYTYPE type, long count)
    {
        if((int)type > (int)MONEYTYPE.MONEYTYPE_COIN_BIND || (int)type < (int)MONEYTYPE.MONEYTYPE_COIN)
        {
            if (type == MONEYTYPE.MONEYTYPE_USERCHALLENGE)
            {
                if (count >= 0 && GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(type) < count)
                {
                    GUIData.AddNotifyData("#{1409}");
                    return false;
                }
            }

            if (type == MONEYTYPE.MONEYTYPE_SNATCHSCORE)
            {
                if (count >= 0 && GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(type) < count)
                {
                    GUIData.AddNotifyData("#{1410}");
                    return false;
                }
            }
            return true;
        }
        LogModule.DebugLog("Money cnt:" + GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(type) + "," + count);

        MONEYTYPE _LessMoneyType = type;

        //MONEYTYPE_COIN_BIND  银票不足可以用银两代替,反之不行
        if (type == MONEYTYPE.MONEYTYPE_COIN_BIND)
        {
            if (count >= 0 && 
                GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_COIN) +
                 GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_COIN_BIND) >= count)
            {
                return true;
            }else
            {
                GUIData.AddNotifyData("#{6107}");
                return false;
            }
        }
        else if (type == MONEYTYPE.MONEYTYPE_COIN)
        {
            if (count >= 0 &&
                GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_COIN) +
                 GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_COIN_BIND) >= count)
            {
                return true;
            }
            _LessMoneyType = MONEYTYPE.MONEYTYPE_COIN;
        }
        else
        {
            if (count >= 0 && GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(type) >= count)
            {
                return true;
            }
        }


        if ((int)type < (int)MONEYTYPE.MONEYTYPE_COIN || (int)type > (int)MONEYTYPE.MONEYTYPE_COIN_BIND)
        {
            return false;
        }

        if (type == MONEYTYPE.MONEYTYPE_YUANBAO)
        {
            MessageBoxLogic.OpenOKCancelBox(StrDictionary.GetClientDictionaryString("#{42600}"), "", delegate ()
            {
                OpenRechargePanel();
            },
                delegate ()
                {
                    UIManager.CloseUI(UIInfo.MessageBox);
                });
        }
        else
        {
            ShowSwitchMoneyPanel(_LessMoneyType);
        }

        return false;
    }
    public static void ShowSwitchMoneyPanel(MONEYTYPE type, bool ignoreTip)
    {
        switch(type)
        {
            case MONEYTYPE.MONEYTYPE_YUANBAO:  //充值
                OpenRechargePanel();
                CloseSwitchWindow();
                break;
            default:  //兑换

                if (type == MONEYTYPE.MONEYTYPE_COIN_BIND)
                    return;

                UIManager.ShowUI(UIInfo.SwitchMoneyPanl, delegate (bool bSucess, object param)
                {
                    if (bSucess)
                    {
                        SwitchMoneyPanlCtr.Instance.InitAimMoneyType(type);
                    }
                });
                break; 
        }
    }

    //
    public static bool IsMoneyEnoughWhenSwitch(int val)
    {
        var rebateDic = TableManager.GetCurrencyExchange();
        Tab_CurrencyExchange info;
        if (rebateDic.TryGetValue((int)MONEYTYPE.MONEYTYPE_YUANBAO, out info))
        {
            var lingyuToYuanbaoRebate = info.GetMoneybyIndex((int)MONEYTYPE.MONEYTYPE_YUANBAO_BIND);
            if (GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_YUANBAO_BIND) / lingyuToYuanbaoRebate >= val
                || GameManager.gameManager.PlayerDataPool.Money.GetMoneyByType(MONEYTYPE.MONEYTYPE_YUANBAO) >= val)
                return true;
        }
        return false;
    }

    public static void OpenRechargePanel()
    {
        if (YuanBaoShopLogic.Instance() && YuanBaoShopLogic.Instance().gameObject.activeInHierarchy)
        {
            YuanBaoShopLogic.Instance()._TagPanel.ShowPage(3);  //充值界面

            //特权VIP界面需要返回充值界面
            if (VipInfo.Instance)
            {
                VipInfo.Instance.ShowYBPanel();
            }
        }
        else
        {
            UIManager.ShowUI(UIInfo.YuanBaoShop, delegate (bool bSucess, object param)
            {
                if (bSucess)
                {
                    YuanBaoShopLogic.Instance()._TagPanel.ShowPage(3);  //充值界面
                                                                        //特权VIP界面需要返回充值界面
                    if (VipInfo.Instance)
                    {
                        VipInfo.Instance.ShowYBPanel();
                    }
                }
            });
        }

        //关闭交换界面
        if (SwitchMoneyPanlCtr.Instance && SwitchMoneyPanlCtr.Instance.gameObject.activeInHierarchy)
        {
            UIManager.CloseUI(UIInfo.SwitchMoneyPanl);
        }
    }

    public static void ShowSwitchMoneyPanel(MONEYTYPE type)
    {
        string lessMoneyName = "";
        switch (type)
        {
            case MONEYTYPE.MONEYTYPE_YUANBAO_BIND:
                lessMoneyName = StrDictionary.GetClientDictionaryString("#{42601}");;
                break;
            case MONEYTYPE.MONEYTYPE_COIN:
                lessMoneyName = StrDictionary.GetClientDictionaryString("#{42602}");
                break;
            case MONEYTYPE.MONEYTYPE_COIN_BIND:
                lessMoneyName = StrDictionary.GetClientDictionaryString("#{42603}");
                break;
        }
        MessageBoxLogic.OpenOKCancelBox(StrDictionary.GetClientDictionaryString("#{42604}", lessMoneyName), "", delegate ()
        {
            UIManager.ShowUI(UIInfo.SwitchMoneyPanl, delegate (bool bSucess, object param)
            {
                if (bSucess)
                {
                    SwitchMoneyPanlCtr.Instance.InitAimMoneyType(type);
                }
            });
        }, delegate ()
        {
            UIManager.CloseUI(UIInfo.SwitchMoneyPanl);
        });

    }
    
    public static void CloseSwitchWindow()
    {
        UIManager.CloseUI(UIInfo.SwitchMoneyPanl);
    }
}