using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Games.Item;
using GCGame.Table;
using GCGame;
using Games.Fellow;

public class FellowSkillListItem : UIItemSelect
{
    public Image Quility;
    public Image Icon;
    public Text Level;
    public GameObject Levelback;
    public GameObject LevelMask;
    public GameObject EnoughHave;
    public GameObject Study;

    public GameObject Child;
    public Text Type;

    int fellodSkillId = -1;
    int commonItemId = -1;
    public override void Show(Hashtable hash)
    {
        fellodSkillId = (int)hash["InitObj"];

        if(fellodSkillId == -1)
        {
            Child.SetActive(false);
            Type.text = StrDictionary.GetClientDictionaryString("#{20159}") + " : ";
            Type.gameObject.SetActive(true);
            return;
        }
        if(fellodSkillId == -2)
        {
            Child.SetActive(false);
            Type.text = StrDictionary.GetClientDictionaryString("#{20158}") + " : ";
            Type.gameObject.SetActive(true);
            return;
        }
        if(fellodSkillId == -3)
        {
            Child.SetActive(false);
            Type.gameObject.SetActive(false);
            return;
        }
        Child.SetActive(true);
        Type.gameObject.SetActive(false);
        InitInfo();
        base.Show();
    }

    public void InitInfo()
    {
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
        {
            gameObject.SetActive(false);
            GameObject.Destroy(gameObject);
            return;
        }
        Tab_CommonItem commonItem = null;
        int NeedConst = 0;
        pet.ConstPay(fellodSkillId, out commonItem, out NeedConst);
        int id = pet.IsSkillBookUsed(fellodSkillId);
        int realSkill = (id == 0 ? fellodSkillId : id);
        Tab_FellowSkill skill = TableManager.GetFellowSkillByID(realSkill, 0);
        if (skill == null)
        {
            return;
        }
        if (commonItem == null)
        {
            gameObject.SetActive(false);
            GameObject.Destroy(gameObject);
            return;
        }
        LoadAssetBundle.Instance.SetImageSprite(Icon, skill.SkillIcon);
        LoadAssetBundle.Instance.SetImageSprite(Quility, GCGame.Utils.GetItemQualityFrame(commonItem.Quality));
        commonItemId = commonItem.Id;
        int hasCount = GameManager.gameManager.PlayerDataPool.BackPack.GetItemCountByDataId(commonItem.Id);
        Level.text = (id == 0 ? "" : "Lv." + skill.Level.ToString());
        Levelback.gameObject.SetActive(id != 0);
        EnoughHave.SetActive(hasCount > 0 && hasCount >= NeedConst && pet.IsSkillMax(realSkill) == false);
        LevelMask.SetActive(id == 0);
        Study.gameObject.SetActive(skill.Type != 1 && pet.IsHaveSkillId(fellodSkillId));
    }

    public override void Refresh()
    {
        Tab_FellowSkill skill = TableManager.GetFellowSkillByID(fellodSkillId, 0);
        if (skill == null)
        {
            return;
        }
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
        {
            gameObject.SetActive(false);
            GameObject.Destroy(gameObject);
            return;
        }
        InitInfo();
    }

    public override void Refresh(Hashtable hash)
    {
        if (hash == null)
            return;
        Fellow pet = GameManager.gameManager.PlayerDataPool.FellowContainer.GetFellowByGuid(PetMainWnd.CurrentSelectFellowId);
        if (pet == null)
        {
            gameObject.SetActive(false);
            GameObject.Destroy(gameObject);
            return;
        }
        if (hash.ContainsKey("new") == false)
            return;
        if (hash.ContainsKey("old") == false)
            return;
        int oldID = (int)hash["old"];
        int newID = (int)hash["new"];
        int realId = pet.IsSkillBookUsed(fellodSkillId);
        if (realId == 0)
            realId = fellodSkillId;
        
        Tab_FellowLearnSkillCost skillOld = TableManager.GetFellowLearnSkillCostByID(oldID, 0);
        Tab_FellowLearnSkillCost skillNew = TableManager.GetFellowLearnSkillCostByID(newID, 0);
        InitInfo();
        if (pet.GY_skillid == newID || pet.MW_skillid == newID)
        {
            if (oldID == realId && skillOld != null && skillNew != null)
            {
                GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{20116}", pet.Name, skillNew.SkillName, skillOld.SkillName));
            }
            if (skillNew == null && newID == realId && skillNew != null)
            {
                GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{20117}", pet.Name, skillNew.SkillName));
            }
        }
        InitInfo();
        return;
    }
}