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

public class RingPanelCtr : MonoBehaviour {

    public static RingPanelCtr Instance;

    private void Awake()
    {
        GetMaxLevel();
        GetLevelNeedExp();
        if (Instance == null)
        {
            Instance = this;
        }
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    private void OnEnable()
    {
        ReqCurRingInfo();
    }

    // 用于红点更新,用于确保红点更新数据完整
    // 正式打开页面会重新执行一遍,待优化
    public void PreInit()
    {
        Awake();
        ReqCurRingInfo();
    }

    #region
    public RingItem ringItem;

    public Text ringName;
    public Text ringLevel;

    public Text curLevelDesc;
    public Text nextLevelDesc;
    public GameObject consumeObj;
    public Slider ringExpSlider;
    public Image consumeItemIcon;
    public Image consumeItemQuality;
    public Text consumeItemName;
    public Text consumeItemOwnValue;
    public UIImgText combatValue;
    public Text sliderValue;
    public GameObject btnRedTip;            // 按钮上的红点提示

    public List<Text> attrList;

    public GameObject _GainBtn;

    public GameObject _LevelUpPanel;
    #endregion


    //当前的进度
    public void ReqCurRingInfo()
    {
        ReqGetMarryRingInfo req = new ReqGetMarryRingInfo();
        req.flag = 1;
        req.SendMsg();
    }

    private int curRingLevel;  //戒指等级
    private MarryRingCtr.RingType curRingType;  //戒指类型
    private Tab_MarryRingBase curRingBase = null;
    private int curRingExp = 0;

    //戒指类型 等级 当前经验
    public void OnReceivePacket(RespGetMarryRingInfo packet)
    {
        curRingLevel = packet.ringlevel;
        curRingType = (MarryRingCtr.RingType)packet.ringType;
        curRingExp = packet.ringExp;
        int ringBaseId = ((int)curRingType + 1) * 1000 + curRingLevel;
        curRingBase = TableManager.GetMarryRingBaseByID(ringBaseId, 0);
        if (curRingBase == null)
        {
            return;
        }

        MaxLevelOpt();
        InitRingItem();  //初始化戒指的相关信息
        InitRingPanelInfo();
        InitSliderAndConsumeItem();
        InitAttrList();
        InitSliderAndConsumeItem();
        UpdataRedTips();
    }

    public void MaxLevelOpt()
    {
        if (maxLevel == 0)
            GetMaxLevel();
        _LevelUpPanel.SetActive(curRingLevel != maxLevel);
    }

    public void InitRingPanelInfo()
    {
        if (curRingBase == null)
        {
            return;
        }

        Tab_CommonItem commonItem = TableManager.GetCommonItemByID(curRingBase.ItemId, 0);
        if (commonItem == null)
        {
            return;
        }

        ringName.text = commonItem.Name;
        ringLevel.text = StrDictionary.GetClientDictionaryString("#{46524}", curRingBase.RingLevel);
    }


    private int consumeItemId = -1;
    public void InitSliderAndConsumeItem()
    {
        if (curRingLevel < maxLevel)
        {
            consumeObj.gameObject.SetActive(true);

            curLevelDesc.text = curRingLevel.ToString();
            nextLevelDesc.text = (curRingLevel + 1).ToString();
            if (curRingBase == null)
            {
                return;
            }
            sliderValue.text = StrDictionary.GetClientDictionaryString("#{3318}", curRingExp - GetCurLevelNeedExp(curRingLevel), curRingBase.NexctLevelNeedExp);
            ringExpSlider.value = (float)(curRingExp - GetCurLevelNeedExp(curRingLevel)) / (float)curRingBase.NexctLevelNeedExp;

            Tab_MarryBase marryBase = TableManager.GetMarryBaseByID(1, 0);
            if (marryBase == null)
            {
                return;
            }
            consumeItemId = marryBase.ConsumeItemId;
            Tab_CommonItem commonItem = TableManager.GetCommonItemByID(marryBase.ConsumeItemId, 0);
            if (commonItem == null)
            {
                return;
            }
            LoadAssetBundle.Instance.SetImageSprite(consumeItemIcon, commonItem.Icon);
            LoadAssetBundle.Instance.SetImageSprite(consumeItemQuality, Utils.GetItemQualityFrame(commonItem.Quality));
            if (commonItem.QualityEffect > 0)
            {
                CommonItemContainerItem.ShowQualityEffect(true, commonItem.QualityEffect, consumeItemIcon.transform);
            }
            else
            {
                CommonItemContainerItem.ShowQualityEffect(false, commonItem.QualityEffect, consumeItemIcon.transform);
            }

            consumeItemName.text = commonItem.Name;

            var backPack = GameManager.gameManager.PlayerDataPool.BackPack;
            if (backPack != null)
            {
                consumeItemOwnValue.text = backPack.GetItemCountByDataId(consumeItemId).ToString();
                _GainBtn.SetActive(backPack.GetItemCountByDataId(consumeItemId) <= 0);
            }
            else
            {
                consumeItemOwnValue.text = "0";
                _GainBtn.SetActive(true);
            }

        }
        else
        {
            consumeObj.gameObject.SetActive(false);
        }
    }

    public void OnGainBtnClick()
    {
        ItemTooltipsLogic.ShowItemTooltip(consumeItemId, ItemTooltipsLogic.ShowType.GetPath, _GainBtn.transform.position);
    }

    private Dictionary<int, int> ringLevelNeedExpDic;
    private int expCount = 0;
    public void GetLevelNeedExp()
    {
        if(ringLevelNeedExpDic != null)
        {
            return;
        }
        expCount = 0;
        ringLevelNeedExpDic = new Dictionary<int, int>();
        ringLevelNeedExpDic.Add(1, 0);
        var tabDic = TableManager.GetMarryRingBase().Values;
        foreach (var info in tabDic)
        {
            if (info.Type == (int)MarryRingCtr.RingType.Marry)
            {
                expCount += info.NexctLevelNeedExp;
                if (ringLevelNeedExpDic.ContainsKey(info.RingLevel + 1))
                {
                    ringLevelNeedExpDic[info.RingLevel + 1] = expCount;
                }
                else
                {
                    ringLevelNeedExpDic.Add(info.RingLevel + 1, expCount);
                }
            }
        }
    }

    public int GetCurLevelNeedExp(int level)
    {
        if (ringLevelNeedExpDic.ContainsKey(level))
        {
            return ringLevelNeedExpDic[level];
        } else
        {
            return 0;
        }
    }

    public void RefreshConsumeRemain()
    {
        var backPack = GameManager.gameManager.PlayerDataPool.BackPack;
        if (backPack != null)
        {
            consumeItemOwnValue.text = backPack.GetItemCountByDataId(consumeItemId).ToString();
        }
        else
        {
            consumeItemOwnValue.text = "0";
        }
        _GainBtn.SetActive(backPack.GetItemCountByDataId(consumeItemId) <= 0);
    }

    public void OnConsumeItemClick()
    {
        if (consumeItemId == -1)
        {
            return;
        }

        ItemTooltipsLogic.ShowItemTooltip(consumeItemId, ItemTooltipsLogic.ShowType.Info, consumeItemIcon.transform.position);
    }

    public void InitAttrList()
    {
        if (curRingBase == null)
        {
            return;
        }
        combatValue.text = curRingBase.CombatValue.ToString();// StrDictionary.GetClientDictionaryString("#{2462}", curRingBase.CombatValue.ToString());
        for (int index = 0; index < attrList.Count; index++)
        {
            if (curRingBase.GetAttrIdbyIndex(index) != -1)
            {
                attrList[index].gameObject.SetActive(true);
                string attrId = "#{" + (curRingBase.GetAttrIdbyIndex(index) + 10000) + "}";
                attrList[index].text = StrDictionary.GetClientDictionaryString("#{46217}", StrDictionary.GetClientDictionaryString(attrId), curRingBase.GetAttrValuebyIndex(index));
            }
            else
            {
                attrList[index].gameObject.SetActive(false);
            }
        }
    }

    public void OnTrainBtnClick()
    {
        var backPack = GameManager.gameManager.PlayerDataPool.BackPack;
        if (backPack != null)
        {
            if (backPack.GetItemCountByDataId(consumeItemId) < 1)
            {
                GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{42696}"));
                return;
            }
        } else
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{42696}"));
            return;
        }

        //没有结婚
        //if(!GameManager.gameManager.PlayerDataPool.IsMarried)
        //{
        //    GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{46514}"));
        //    return;
        //}

        if (curRingLevel >= maxLevel)
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{46519}"));
            return;
        }

        ReqUpgradeMarryRing req = new ReqUpgradeMarryRing();
        req.flag = 1;
        req.SendMsg();
    }

    private int maxLevel = -1;
    public void GetMaxLevel()
    {
        if(maxLevel != -1)
        {
            return;
        }
        var tabDic = TableManager.GetMarryRingBase().Values;
        foreach (var info in tabDic)
        {
            maxLevel = maxLevel > info.RingLevel ? maxLevel : info.RingLevel;
        }
    }

    public void InitRingItem()
    {
        ringItem.InitRingItem();
    }

    public void OnItemClick()
    {
        if (consumeItemId == -1)
        {
            return;
        }

        ItemTooltipsLogic.ShowItemTooltip(consumeItemId, ItemTooltipsLogic.ShowType.Info, consumeItemIcon.transform.position);
    }

    // 红点更新,并触发外层红点检查
    public static bool HasRedPoint()
    {
        //if(!GameManager.gameManager.PlayerDataPool.IsMarried)
        //{
        //    return false;
        //}

        Tab_MarryBase marryBase = TableManager.GetMarryBaseByID(1, 0);
        int temp_CurRingLevel = GameManager.gameManager.PlayerDataPool.MyRingInfoData.GetRingInfo().ringLevel;
        int temp_ConsumeItemId = marryBase.ConsumeItemId;
        int temp_MaxLevel = 0;
        var tabDic = TableManager.GetMarryRingBase().Values;
        foreach (var info in tabDic)
        {
            temp_MaxLevel = temp_MaxLevel > info.RingLevel ? temp_MaxLevel : info.RingLevel;
        }

        var backPack = GameManager.gameManager.PlayerDataPool.BackPack;
        if (backPack != null)
        {
            if (backPack.GetItemCountByDataId(temp_ConsumeItemId) >= 1 && temp_CurRingLevel < temp_MaxLevel)
            {
                return true;
            }
        }

        return false;
    }

    // 红点更新,并触发外层红点检查
    public void UpdataRedTips(Hashtable addparam = null, Hashtable Sendparam = null)
    {
        if (btnRedTip != null)
        {
            if (HasRedPoint())
            {
                btnRedTip.SetActive(true);
                MarryRoot.Instance.UpdateRedTips(MarryRoot.RedTipType.Ring, true);
            }
            else
            {
                btnRedTip.SetActive(false);
                MarryRoot.Instance.UpdateRedTips(MarryRoot.RedTipType.Ring, false);
            }
        }
    }

    //public void UpdataRedTips(Hashtable addparam = null, Hashtable Sendparam = null)
    //{
    //    var backPack = GameManager.gameManager.PlayerDataPool.BackPack;
    //    if (backPack != null && btnRedTip != null)
    //    {
    //        if (backPack.GetItemCountByDataId(consumeItemId) >= 1 && curRingLevel < maxLevel)
    //        {
    //            btnRedTip.SetActive(true);
    //            MarryRoot.Instance.UpdateRedTips(MarryRoot.RedTipType.Ring, true);
    //        }
    //        else
    //        {
    //            btnRedTip.SetActive(false);
    //            MarryRoot.Instance.UpdateRedTips(MarryRoot.RedTipType.Ring, false);
    //        }
    //    }
    //    else if (btnRedTip != null)
    //    {
    //        btnRedTip.SetActive(false);
    //        MarryRoot.Instance.UpdateRedTips(MarryRoot.RedTipType.Ring, false);
    //    }
    //}


}