using System.Collections.Generic;
using Games.LogicObj;
using GCGame;
using GCGame.Table;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Games.Fellow;
using Games.Events;

public class PetGrowWnd : MonoBehaviour
{
    public Transform CurrObj;
    public Transform NextObj;

    public Image CurrIcon;
    public Image CurrQuility;
    public Text CurrLevel;
    public Text CurrQuilityLabel;
    public Text CurrGrowLevel;

    public Image NextIcon;
    public Image NextQuility;
    public Text NextLevel;
    public Text NextQuilityLabel;
    public Text NextGrowLevel;

    public GameObject GetBtnObj;
    public Slider GrowLevelSlider;
    public Text CurrGrow;
    public Text Tip;

    public GameObject BtnTupo;
    public GameObject BtnUp;

    public GameObject MaxObject;
    public GameObject PayObject;
    public Image PayIcon;
    public Image PayQulity;
    public Text PayText;

    public UIContainerBase _AttrsContainerBase;

    public void ClearInfo(bool state)
    {
        LoadAssetBundle.Instance.SetImageSprite(CurrQuility, Utils.GetFellowQuilityIcon(3));
        LoadAssetBundle.Instance.SetImageSprite(NextQuility, Utils.GetFellowQuilityIcon(4));

        CurrQuilityLabel.gameObject.SetActive(state);
        CurrIcon.gameObject.SetActive(state);
        CurrGrowLevel.gameObject.SetActive(state);
        CurrLevel.gameObject.SetActive(state);

        NextQuilityLabel.gameObject.SetActive(state);
        NextIcon.gameObject.SetActive(state);
        NextGrowLevel.gameObject.SetActive(state);
        NextLevel.gameObject.SetActive(state);

        Tip.gameObject.SetActive(state);
        NextObj.gameObject.SetActive(state);
        BtnTupo.SetActive(false);
        MaxObject.SetActive(false);
        GrowLevelSlider.value = 1;
        CurrLevel.gameObject.SetActive(state);
        CurrGrow.gameObject.SetActive(state);
        if(state == false)
            CurrObj.localPosition = new Vector3(30, 140, 0);
        else
            CurrObj.localPosition = new Vector3(-200, 140, 0);

        LoadAssetBundle.Instance.SetImageSprite(PayQulity, GCGame.Utils.GetItemQualityFrame(3));
        PayObject.SetActive(state);
        GetBtnObj.SetActive(false);
        PayText.gameObject.SetActive(state);
        _AttrsContainerBase.InitContentItem(null);

    }

    public void OnSelectPet()
    {
        if (GameManager.gameManager.PlayerDataPool.FellowContainer == null)
            return;

        Debug.Log("petGrowWnd CurrentSelectFellowId:" + PetMainWnd.CurrentSelectFellowId);
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null || pet.Guid == Games.GlobeDefine.GlobeVar.INVALID_GUID)
        {
            ClearInfo(false);
            return;
        }

        ClearInfo(true);
        Tab_FellowBase fellowBase = TableManager.GetFellowBaseByID(pet.DataId, 0);
        if (fellowBase == null)
            return;
        Tab_FellowGrow tab_FellowGrowBase = TableManager.GetFellowGrowByID(fellowBase.GrowQuility, 0);
        if (tab_FellowGrowBase == null)
            return;

        Tab_FellowGrow tab_FellowGrow = TableManager.GetFellowGrowByID(pet.Quality, 0);
        if (tab_FellowGrow == null)
            return;
        Tab_FellowGrow tab_FellowNextGrow = TableManager.GetFellowGrowByID(fellowBase.Quility + 1, 0);

        int Stat = 0;
        //可突破
        if(pet.grow>=tab_FellowGrow.GrowBase && pet.Quality<=fellowBase.GrowQuility && tab_FellowNextGrow!=null)
        {
            BtnTupo.SetActive(true);
            BtnUp.SetActive(false);
            PayObject.SetActive(true);
            MaxObject.SetActive(false);
            Tip.gameObject.SetActive(true);
            Stat = 1;
        }
        else  //最大成长值了
            if(pet.grow >= tab_FellowGrowBase.GrowMax)
        {
            CurrObj.localPosition = new Vector3(30, 140, 0);
            NextObj.gameObject.SetActive(false);
            PayObject.SetActive(false);
            Tip.gameObject.SetActive(false);
            MaxObject.SetActive(true);
            Stat = 2;
        }
        else if(pet.Quality == fellowBase.GrowQuility+1 || tab_FellowNextGrow==null)
        {
            CurrObj.localPosition = new Vector3(30, 140, 0);
            NextObj.gameObject.SetActive(false);
            Tip.gameObject.SetActive(false);
            BtnTupo.SetActive(false);
            BtnUp.SetActive(true);
            PayObject.SetActive(true);
            MaxObject.SetActive(false);
            Stat = 3;
        }
        else
        {
            CurrObj.localPosition = new Vector3(-200, 140, 0);
            NextObj.gameObject.SetActive(true);
            BtnTupo.SetActive(false);
            BtnUp.SetActive(true);
            PayObject.SetActive(true);
            MaxObject.SetActive(false);
            Tip.gameObject.SetActive(true);
        }

        LoadAssetBundle.Instance.SetImageSprite(CurrIcon, pet.GetIcon());
        LoadAssetBundle.Instance.SetImageSprite(CurrQuility, Utils.GetFellowQuilityIcon(pet.Quality));
        CurrQuilityLabel.text = StrDictionary.GetClientDictionaryString("#{20173}", StrDictionary.GetClientDictionaryString(Utils.GetFellowQuilityStr((FELLOWQUALITY)pet.Quality)));
        if(Stat == 2)
        {
            CurrGrowLevel.text = StrDictionary.GetClientDictionaryString("#{20174}", tab_FellowGrowBase.GrowMax);
        }
        else if(Stat == 3)
        {
            if(tab_FellowNextGrow == null)
            {
                CurrGrowLevel.text = StrDictionary.GetClientDictionaryString("#{20174}", fellowBase.Growth + "-" + tab_FellowGrowBase.GrowMax);
            }
            else
            {
                CurrGrowLevel.text = StrDictionary.GetClientDictionaryString("#{20174}", tab_FellowGrowBase.GrowBase + "-" + tab_FellowGrowBase.GrowMax);
            }
        }
        else
        {
            CurrGrowLevel.text = StrDictionary.GetClientDictionaryString("#{20174}", fellowBase.Growth + "-" + tab_FellowGrowBase.GrowBase);
        }

        CurrLevel.text ="Lv."+ pet.Level.ToString();
        if (pet.Quality + 1 >= (int)FELLOWQUALITY.ORANGE)
        {
            CommonItemContainerItem.ShowQualityEffect(true, 8001, NextIcon.transform);
        }
        else
        {
            CommonItemContainerItem.ShowQualityEffect(false, 8001, NextIcon.transform);
        }
        if (pet.Quality >= (int)FELLOWQUALITY.ORANGE)
        {
            CommonItemContainerItem.ShowQualityEffect(true, 8001, CurrIcon.transform);
        }
        else
        {
            CommonItemContainerItem.ShowQualityEffect(false, 8001, CurrIcon.transform);
        }

        if (Stat == 0)
        {
            LoadAssetBundle.Instance.SetImageSprite(NextIcon, pet.GetIcon());
            LoadAssetBundle.Instance.SetImageSprite(NextQuility, Utils.GetFellowQuilityIcon(pet.Quality + 1));
            NextQuilityLabel.text = StrDictionary.GetClientDictionaryString("#{20173}", StrDictionary.GetClientDictionaryString(Utils.GetFellowQuilityStr((FELLOWQUALITY)(pet.Quality + 1))));
            NextGrowLevel.text = StrDictionary.GetClientDictionaryString("#{20174}", tab_FellowGrow.GrowBase + "-" + tab_FellowGrow.GrowMax);
            NextLevel.text = "Lv." + pet.Level.ToString();
        }
        
        if(tab_FellowNextGrow==null)
        {
            Tip.gameObject.SetActive(Stat!=2);
            Tip.text = StrDictionary.GetClientDictionaryString("#{20192}", tab_FellowGrow.GrowMax);
        }
        else
        {
            string colorName = StrDictionary.GetClientDictionaryString(Utils.GetFellowQuilityStr((FELLOWQUALITY)(pet.Quality + 1)));
            Tip.text = StrDictionary.GetClientDictionaryString("#{20175}", tab_FellowGrow.GrowBase, colorName);
        }
       
        if (Stat == 1 || Stat == 2)
        {
            Tip.text = StrDictionary.GetClientDictionaryString("#{20177}");
            GrowLevelSlider.value = 1;
        }
        else
        {
            if(tab_FellowNextGrow==null)
            {
                GrowLevelSlider.value = (pet.grow - fellowBase.Growth) / (tab_FellowGrow.GrowMax - fellowBase.Growth);
            }
            else
            {
                if (pet.grow < tab_FellowGrowBase.GrowBase)
                {
                    GrowLevelSlider.value = (pet.grow - fellowBase.Growth) / (tab_FellowGrowBase.GrowBase - fellowBase.Growth);
                }
                else
                {
                    GrowLevelSlider.value = (pet.grow - tab_FellowGrowBase.GrowBase) / (tab_FellowGrowBase.GrowMax - tab_FellowGrowBase.GrowBase);
                }
            }
        }
        CurrGrow.text = StrDictionary.GetClientDictionaryString("#{20176}", pet.grow);

        if(BtnTupo.activeSelf)
        {
            CostPay(tab_FellowGrowBase.ConsumeSubTypeBreak, tab_FellowGrowBase.ConsumeNumBreak);
        }
        else
        {
            CostPay(tab_FellowGrowBase.ConsumeSubType, tab_FellowGrowBase.ConsumeNum);
        }

        List<string> attrs = new List<string>();
        attrs.Add(string.Format("{0}-{1}-{2}", PropID.GetAttrName(PropID.PropertyID.MAXHP), pet.blood, pet.growthMaxHp - pet.blood));
        attrs.Add(string.Format("{0}-{1}-{2}", PropID.GetAttrName(PropID.PropertyID.ATTACKALLRATE), pet.attack, pet.growthAttack - pet.attack));
        attrs.Add(string.Format("{0}-{1}-{2}", PropID.GetAttrName(PropID.PropertyID.PYSDEF), pet.physical, pet.growthPysDef - pet.physical));
        attrs.Add(string.Format("{0}-{1}-{2}", PropID.GetAttrName(PropID.PropertyID.MAGDEF), pet.magic, pet.growthMagDef - pet.magic));
        attrs.Add(string.Format("{0}-{1}-{2}", PropID.GetAttrName(PropID.PropertyID.HIT), pet.hit, pet.growthHit - pet.hit));
        attrs.Add(string.Format("{0}-{1}-{2}", PropID.GetAttrName(PropID.PropertyID.DODGE), pet.avoid, pet.growthDodge - pet.avoid));
        _AttrsContainerBase.InitContentItem(attrs);

    }

    bool PayEnough = false;
    public void CostPay(int CostItemId,int CostItemNum)
    {
        Tab_CommonItem item = TableManager.GetCommonItemByID(CostItemId, 0);
        if (item == null)
        {
            return;
        }
        LoadAssetBundle.Instance.SetImageSprite(PayQulity, GCGame.Utils.GetItemQualityFrame(item.Quality));
        LoadAssetBundle.Instance.SetImageSprite(PayIcon, item.Icon);
        int hasCount = GameManager.gameManager.PlayerDataPool.BackPack.GetItemCountByDataId(CostItemId);
        if (hasCount < CostItemNum && CostItemId > 0)
        {
            PayText.text = string.Format(item.Name + ":<color=#ff0000ff>{0}</color>/{1}", hasCount, CostItemNum);
            GetBtnObj.SetActive(true);
            PayEnough = false;
            Button btn = GetBtnObj.GetComponent<Button>();
            if (btn != null)
            {
                if (btn.onClick.GetPersistentEventCount() <= 0)
                {
                    btn.onClick.RemoveAllListeners();
                    btn.onClick.AddListener(delegate ()
                    {
                        ItemTooltipsLogic.ShowItemTooltip(CostItemId, ItemTooltipsLogic.ShowType.GetPath, btn.transform.position);
                    });
                }
            }
        }
        else
        {
            PayEnough = true;
            PayText.text = string.Format(item.Name + ":<color=#00ff00ff>{0}</color>/{1}", hasCount, CostItemNum);
            GetBtnObj.SetActive(false);
        }
    }

    public void Descript_Click()
    {
        MessageHelpLogic.ShowHelpMessage(66);
    }

    public void UPGrow_Click()
    {
        if (GameManager.gameManager.PlayerDataPool.FellowContainer == null)
            return;
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null || pet.Guid == Games.GlobeDefine.GlobeVar.INVALID_GUID)
        {
            return;
        }
        Tab_FellowGrow tab_FellowGrow = TableManager.GetFellowGrowByID(pet.Quality, 0);
        if (tab_FellowGrow == null)
            return;
        if(tab_FellowGrow.GrowMax <= pet.grow)
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("{#20178}"));
            return;
        }
        if (PayEnough==false)
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("{#21002}"));
            return;
        }

        CG_FELLOW_GROWTH_UPGRADE cmd = (CG_FELLOW_GROWTH_UPGRADE)PacketDistributed.CreatePacket(MessageID.PACKET_CG_FELLOW_GROWTH_UPGRADE);
        cmd.Guid = (long)pet.Guid;
        cmd.SendPacket();
    }

    public void UpQuality_Click()
    {
        if (GameManager.gameManager.PlayerDataPool.FellowContainer == null)
            return;
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null || pet.Guid == Games.GlobeDefine.GlobeVar.INVALID_GUID)
        {
            return;
        }
        if (PayEnough == false)
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("{#21002}"));
            return;
        }
        CG_FELLOW_GROWTH_BREAK cmd = (CG_FELLOW_GROWTH_BREAK)PacketDistributed.CreatePacket(MessageID.PACKET_CG_FELLOW_GROWTH_BREAK);
        cmd.Guid = (long)pet.Guid;
        cmd.SendPacket();
    }
}