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 PetXiuWuWnd : MonoBehaviour
{
    public Text bone;    //根骨资质
    public Text Strength; //精力资质
    public Text power;   //力量资质
    public Text brains;  //智力资质
    public Text agile;   //敏捷资质
    public Text boneOne;    //根骨
    public Text StrengthOne; //精力
    public Text powerOne;   //力量资质
    public Text brainsOne;  //智力资质
    public Text agileOne;   //敏捷资质



    public Text currentXW;  //当前修为
    public Text currentWX;  //当前悟性

    public Text XWCousumeNum;  //修为消耗的道具拥有和需要数
    public Text XWCousumeName; //修为消耗的道具名称
    public Image XWCousumeIcon; //修为消耗的道具Icon
    public Image XWCousumeQuility;

    public Text WuXingCousumeNum;   //悟性消耗的道具拥有和需要的数
    public Text WuXingCousumeNumName;  //悟性消耗的道具名称
    public Image WuXingCousumeNumIcon; //悟性消耗的道具Icon
    public Image WuXingCousumeNumQuility;

    public GameObject XWGetBtn;
    public GameObject XWUpBtn;

    public GameObject WXGetBtn;
    public GameObject WXUpBtn;

    public InitAptitudeStart petStart;
    private int CultivationItemID;
    private int SavvyItemID;

    public void ClearInfo(bool state)
    {
        bone.gameObject.SetActive(state);
        Strength.gameObject.SetActive(state);
        power.gameObject.SetActive(state);
        brains.gameObject.SetActive(state);
        agile.gameObject.SetActive(state);
        currentXW.gameObject.SetActive(state);
        currentWX.gameObject.SetActive(state);
        XWCousumeNum.gameObject.SetActive(state);
        XWCousumeName.gameObject.SetActive(state);
        XWCousumeIcon.gameObject.SetActive(state);
        WuXingCousumeNum.gameObject.SetActive(state);
        WuXingCousumeNumName.gameObject.SetActive(state);
        WuXingCousumeNumIcon.gameObject.SetActive(state);
        petStart.ClearInfo(state);
    }

    public void Awake()
    {
        Hashtable hash = new Hashtable();
        hash["name"] = "PetXiuWu";
        MessageEventCallBack fun = Fresh;
        hash["callFun"] = fun;
        EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.FRESHPETINFO, hash);

        Hashtable calbackMoveparam3 = new Hashtable();
        calbackMoveparam3["name"] = "PetXiuWuFresh";
        MessageEventCallBack fun3 = PayFresh;
        calbackMoveparam3.Add("callFun", fun3);
        EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.FRESHSAMEUSETIP, calbackMoveparam3);

        Hashtable calbackMoveparam2 = new Hashtable();
        calbackMoveparam2["name"] = "MainXiuWu";
        MessageEventCallBack fun2 = UpdateFelllowAttr;
        calbackMoveparam2.Add("callFun", fun2);
        Games.Events.EventDispatcher.Instance.AddMessageEvent(Games.Events.EventId.UpdatePetInfo, calbackMoveparam2);
    }

    public void PayFresh(Hashtable addparam, Hashtable sendparam)
    {
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
            return;
        CountCost(pet);
    }

    private void SetCurrentData(Fellow pet)
    {
        currentXW.text = string.Format("{0}/{1}", pet.cultivation, GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Level);
        currentWX.text = string.Format("{0}/{1}", pet.sawy * 1.0 / 100.0f, 10);
    }

    public void UpdateFelllowAttr(Hashtable addParam,Hashtable sendParam)
    {
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
            return;
        SetCurrentData(pet);
        InitAtitude(pet);
        petStart.InitStart(pet.AptitudeTotle);
    }

    public void Fresh(Hashtable addparam, Hashtable Sendparam)
    {
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
            return;
        SetCurrentData(pet);
        InitAtitude(pet);
        petStart.InitStart(pet.AptitudeTotle);
    }

    public void OnDestroy()
    {
        EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.FRESHPETINFO, "PetXiuWu");
        EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.FRESHSAMEUSETIP, "PetXiuWuFresh");
        EventDispatcher.Instance.RemoveMessage(Games.Events.EventId.UpdatePetInfo, "MainXiuWu");
    }

    void CountCost(Fellow pet)
    {
        int CultivationDataID = pet.cultivation;
        Tab_PromoteCultivation Cultivation = TableManager.GetPromoteCultivationByID(pet.cultivation, 0);
        if (Cultivation != null)
        {
            Tab_CommonItem item = TableManager.GetCommonItemByID(Cultivation.CostItemId, 0);
            if (item != null)
            {
                LoadAssetBundle.Instance.SetImageSprite(XWCousumeQuility, Utils.GetItemQualityFrame(item.Quality));
                LoadAssetBundle.Instance.SetImageSprite(XWCousumeIcon, item.Icon);
                if (item.QualityEffect > 0)
                {
                    CommonItemContainerItem.ShowQualityEffect(true, item.QualityEffect, XWCousumeIcon.transform);
                }
                else
                {
                    CommonItemContainerItem.ShowQualityEffect(false, item.QualityEffect, XWCousumeIcon.transform);
                }
            }
            XWCousumeName.text = item.Name;
            int hasCount = GameManager.gameManager.PlayerDataPool.BackPack.GetItemCountByDataId(Cultivation.CostItemId);
            if (hasCount < Cultivation.CostNum)
            {
                XWCousumeNum.text = string.Format("<color=#82439cff>{0}</color>/{1}", hasCount, Cultivation.CostNum);
            }
            else
            {
                XWCousumeNum.text = string.Format("<color=#82439cff>{0}</color>/{1}", hasCount, Cultivation.CostNum);
            }

            XWGetBtn.SetActive((hasCount < Cultivation.CostNum));
            XWUpBtn.SetActive((hasCount >= Cultivation.CostNum));
            CultivationItemID = Cultivation.CostItemId;
        }

        int sawyID = pet.sawy / 10;
        Tab_PromoteSavvy Savvy = TableManager.GetPromoteSavvyByID(sawyID, 0);
        if (Savvy != null)
        {
            Tab_CommonItem item = TableManager.GetCommonItemByID(Savvy.CostItemId, 0);
            if (item != null)
            {
                LoadAssetBundle.Instance.SetImageSprite(WuXingCousumeNumQuility, Utils.GetItemQualityFrame(item.Quality));
                LoadAssetBundle.Instance.SetImageSprite(WuXingCousumeNumIcon, item.Icon);
                if (item.QualityEffect > 0)
                {
                    CommonItemContainerItem.ShowQualityEffect(true, item.QualityEffect, XWCousumeIcon.transform);
                }
                else
                {
                    CommonItemContainerItem.ShowQualityEffect(false, item.QualityEffect, XWCousumeIcon.transform);
                }
            }
            WuXingCousumeNumName.text = item.Name;
            int hasCount = GameManager.gameManager.PlayerDataPool.BackPack.GetItemCountByDataId(Savvy.CostItemId);
            if (hasCount < Savvy.CostNum)
            {
                WuXingCousumeNum.text = string.Format("<color=#82439cff>{0}</color>/{1}", hasCount, Savvy.CostNum);
            }
            else
            {
                WuXingCousumeNum.text = string.Format("<color=#82439cff>{0}</color>/{1}", hasCount, Savvy.CostNum);
            }

            WXGetBtn.SetActive((hasCount < Savvy.CostNum));
            WXUpBtn.SetActive((hasCount >= Savvy.CostNum));
            SavvyItemID = Savvy.CostItemId;
        }
    }

    public void OnSelectPet()
    {
        if (GameManager.gameManager.PlayerDataPool.FellowContainer == null)
            return;
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
            return;

        InitAtitude(pet);

        SetCurrentData(pet);

        CountCost(pet);
        petStart.InitStart(pet.AptitudeTotle);
    }


    public void InitAtitude(Fellow pet)
    {
        Tab_PromoteCultivation atitudeData = TableManager.GetPromoteCultivationByID(pet.cultivation, 0);
        if (atitudeData == null)
            return;
        int sawyID = pet.sawy / 10;
        Tab_PromoteSavvy Savvy = TableManager.GetPromoteSavvyByID(sawyID, 0);
        if (Savvy == null)
            return;
        float addRate1 = Savvy.AddAptitude * 1.0f / 100.0f;
        float addRate = atitudeData.AddAptitudePer * 1.0f / 100.0f;
        bone.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", pet.Boneatitude, addRate1 + addRate);
        Strength.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", pet.Forceatitude, addRate1 + addRate);
        power.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", pet.Poweratitude, addRate1 + addRate);
        brains.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", pet.Smartatitude, addRate1 + addRate);
        agile.text = string.Format("{0}<color=#721fadff>(+{1}%)</color>", pet.Agileatitude, addRate1 + addRate);

        boneOne.text = string.Format("{0}<color=#721fadff>(+{1})</color>", pet.bone, GetPropVal(atitudeData, (int)PropID.PropertyID.CONSTITU,-1));
        StrengthOne.text = string.Format("{0}<color=#721fadff>(+{1})</color>", pet.force, GetPropVal(atitudeData, (int)PropID.PropertyID.MORFIB, -1));
        powerOne.text = string.Format("{0}<color=#721fadff>(+{1})</color>", pet.power, GetPropVal(atitudeData, (int)PropID.PropertyID.STRENGTH, -1));
        brainsOne.text = string.Format("{0}<color=#721fadff>(+{1})</color>", pet.smart, GetPropVal(atitudeData, (int)PropID.PropertyID.INTELLENG, -1));
        agileOne.text = string.Format("{0}<color=#721fadff>(+{1})</color>", pet.agile, GetPropVal(atitudeData, (int)PropID.PropertyID.AGILE, -1));
    }

    int  GetPropVal(Tab_PromoteCultivation atitudeData,int Prop,int SubProp)
    {
        for(int i=0;i<atitudeData.getAddPropTypeCount();i++)
        {
            if(Prop  == atitudeData.GetAddPropTypebyIndex(i) && SubProp == atitudeData.GetAddPropSubTypebyIndex(i))
            {
                return atitudeData.GetAddPropValbyIndex(i);
            }
        }
        return 0;
    }

    public void XWCostGet()
    {
        ItemTooltipsLogic.ShowItemTooltip(CultivationItemID, ItemTooltipsLogic.ShowType.GetPath, transform.position);
    }

    public void WXCostGet()
    {
        ItemTooltipsLogic.ShowItemTooltip(SavvyItemID, ItemTooltipsLogic.ShowType.GetPath, transform.position);
    }

    public void Tip()
    {
        MessageHelpLogic.ShowHelpMessage(17);
    }

    public void OnClick_UpXiuWei()
    {
        GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Xiewei.ToString();
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
            return;

        int XWLevel = SystemParam.GetSystemParam_INT(36);
        if (pet.Level < XWLevel)
        {
            GUIData.AddNotifyData(GCGame.Table.StrDictionary.GetClientDictionaryString("#{20140}", 60));
            return;
        }

        if (pet.cultivation >= GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Level)
        {
            GUIData.AddNotifyData(GCGame.Table.StrDictionary.GetClientDictionaryString("#{21009}"));
            return;
        }
        
        if (pet.cultivation >= TableManager.GetPromoteCultivation().Count)
        {
            GUIData.AddNotifyData(GCGame.Table.StrDictionary.GetClientDictionaryString("#{21008}"));
            return;
        }

        CG_PROMOTE_FELLOW send = (CG_PROMOTE_FELLOW)PacketDistributed.CreatePacket(MessageID.PACKET_CG_PROMOTE_FELLOW);
        send.FellowGuid = PetMainWnd.CurrentSelectFellowId;
        send.PromoteType = 2;
        send.SendPacket();
    }

    public void OnClick_UpWuXin()
    {
        var savy = TableManager.GetPromoteSavvy();
        if (savy == null)
            return;
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
            return;
        int XWLevel = SystemParam.GetSystemParam_INT(36);
        if (pet.Level < XWLevel)
        {
            GUIData.AddNotifyData(GCGame.Table.StrDictionary.GetClientDictionaryString("#{20139}", 80));
            return;
        }
        if (pet.sawy / 10 >= savy.Count)
        {
            GUIData.AddNotifyData(GCGame.Table.StrDictionary.GetClientDictionaryString("#{21007}"));
            return;
        }
        CG_PROMOTE_FELLOW send = (CG_PROMOTE_FELLOW)PacketDistributed.CreatePacket(MessageID.PACKET_CG_PROMOTE_FELLOW);
        send.FellowGuid = PetMainWnd.CurrentSelectFellowId;
        send.PromoteType = 1;
        send.SendPacket();
    }
}