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

public class OrnamentItem : UIItemBase {
    public Image _itemQuality;
    public Image _ItemIcon;

    public GameObject _Mark;
    public GameObject _ItemGetBtn;
    public GameObject _ItemOnBtn;
    public GameObject _ItemOffBtn;

    public Text _ItemName;
    public Text _UnOwnMark;
    //public GameObject _ItemGetMark_Select;
    //public GameObject _ItemGetMark_Normal;

    private const string _NormalColor = "<color=#4a5f84>{0}</color>";
    private const string _SelectColor = "<color=#735107>{0}</color>";
    private int _ItemDataId;

    [HideInInspector]
    public int ornamentId;
    private Tab_Ornament ornamentTab = null;
    private Tab_CommonItem commonItem = null;
    public override void Show(Hashtable hash)
    {
        base.Show();
        ornamentTab = (Tab_Ornament)hash["InitObj"];
        ornamentId = ornamentTab.ID;
        if (ornamentTab == null)
        {
            Debug.LogError("ornamementTab is null : " + ornamentId);
            return;
        }
        _ItemDataId = ornamentTab.ItemID;

        commonItem = TableManager.GetCommonItemByID(_ItemDataId, 0);
        if (commonItem == null)
        {
            Debug.LogError("CommonItem is null, itemId : " + _ItemDataId);
            return;
        }

        LoadAssetBundle.Instance.SetImageSprite(_itemQuality, GCGame.Utils.GetItemQualityFrame(commonItem));
        LoadAssetBundle.Instance.SetImageSprite(_ItemIcon, commonItem.Icon);
    }

    public override void OnItemClick()
    {
        base.OnItemClick();

        if (OrnamentPanel.Instance)
            OrnamentPanel.Instance.OnOrnamentItemClick(ornamentTab);
    }

    public void ShowMark(bool isShow)
    {
        _Mark.gameObject.SetActive(isShow);
        _ItemName.text = string.Format(isShow ? _SelectColor : _NormalColor, commonItem.Name);
        _UnOwnMark.text = string.Format(isShow ? _SelectColor : _NormalColor, StrDictionary.GetClientDictionaryString("#{42658}"));
    }

    public void RefreshItemState(OrnamentInfo info)
    {
        _ItemGetBtn.gameObject.SetActive(info.IsGeted != 1);
        _ItemOnBtn.gameObject.SetActive(info.Id == ornamentId && info.IsGeted == 1 && info.IsEquiped != 1);
        _ItemOffBtn.gameObject.SetActive(info.Id == ornamentId && info.IsGeted == 1 && info.IsEquiped == 1);
        _UnOwnMark.gameObject.SetActive(info.Id == ornamentId  && info.IsGeted != 1);
    }

    public void ShowGetPathState()
    {
        _ItemGetBtn.gameObject.SetActive(true);
        _ItemOnBtn.gameObject.SetActive(false);
        _ItemOffBtn.gameObject.SetActive(false);
        _UnOwnMark.gameObject.SetActive(true);
    }

    public void OnGetBtn()
    {
        ItemTooltipsLogic.ShowItemTooltip(ornamentTab.ItemID, ItemTooltipsLogic.ShowType.GetPath, _ItemIcon.gameObject.transform.position);
        //ItemGetPathPopRoot.Show(ornamentTab.ItemID, _ItemIcon.gameObject.transform.position);
    }

    public void OnPutOnBtn()
    {
        CG_REQ_PUT_ORNAMENT req = (CG_REQ_PUT_ORNAMENT)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_PUT_ORNAMENT);
        req.Type = ornamentTab.Type;
        req.Id = ornamentTab.ID;
        req.OperationType = 1;
        req.SendPacket();
    }

    public void OnTakeOffBtn()
    {
        CG_REQ_PUT_ORNAMENT req = (CG_REQ_PUT_ORNAMENT)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_PUT_ORNAMENT);
        req.Type = ornamentTab.Type;
        req.Id = ornamentTab.ID;
        req.OperationType = 2;
        req.SendPacket();
    }
}