using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using Games.Item;
using GCGame.Table;
using Games.ChatHistory;
using GCGame;
using Games.GlobeDefine;
using Module.Log;

public class CommunityLeaveMsgInfo
{
    public int MsgIDX;
    public ulong GUID;
    public int Profession;
    public string HeadIconName;
    public string Name;
    public int Level;
    public int Time;
    public string MsgText;

    public ulong TargetGUID;
    public string TargetName;
}

public class CommunityLeaveMsgItem : CommunityPlayerBaseItem
{
    public Text _Time;
    public ChatContex _MsgText;
    public Button _BtnDel;

    private CommunityLeaveMsgInfo _MsgInfo;

    public override void Show(Hashtable hash)
    {
        base.Show(hash);

        CommunityLeaveMsgInfo msgInfo = (CommunityLeaveMsgInfo)hash["InitObj"];
        ShowMsgInfo(msgInfo);
    }

    public virtual void ShowMsgInfo(CommunityLeaveMsgInfo msgInfo)
    {
        _MsgInfo = msgInfo;

        if (_Icon.isActiveAndEnabled)
        {
            if (!string.IsNullOrEmpty(msgInfo.HeadIconName))
            {
                _Icon.SetNetImage(Community.Instance.GetHeadIconDownUrl(msgInfo.HeadIconName), msgInfo.GUID);
            }
            else
            {
                _Icon.NetImageUrl = "";
                _Icon._ImageChecking.gameObject.SetActive(false);
                LoadAssetBundle.Instance.SetImageSprite(_Icon._Image, Utils.GetProfessionSpriteName(msgInfo.Profession));
            }
        }
        _GUID = _MsgInfo.GUID;
        _Name.text = _MsgInfo.Name;
        _Level.text = _MsgInfo.Level.ToString();
        _Time.text = CommunityBlogItem.GetBlogTimeStr(_MsgInfo.Time);

        string showLinkStr = "";
        if (!string.IsNullOrEmpty(msgInfo.TargetName))
        {
            string retRoleLink = StrDictionary.GetClientDictionaryString("#{39003}", msgInfo.TargetName, msgInfo.TargetGUID);
            showLinkStr += StrDictionary.GetClientDictionaryString("#{39006}", retRoleLink);
            showLinkStr += ":";
        }

        //msgInfo.MsgText = "#{39029}*" + _MsgInfo.GUID + "*" + _MsgInfo.Name + "123";
        if (msgInfo.MsgText.StartsWith("#{39029}")) //特殊处理在其他人空间留言的情况
        {
            showLinkStr += StrDictionary.GetServerDictionaryFormatString(msgInfo.MsgText);
        }
        else
        {
            showLinkStr += msgInfo.MsgText;
        }
        _MsgText.ShowLinkStr(showLinkStr);

        if (CommunityLogic.CommunityPlayerInfo.GUID == GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid
            || _MsgInfo.GUID == GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid)
        {
            _BtnDel.gameObject.SetActive(true);
        }
        else
        {
            _BtnDel.gameObject.SetActive(false);
        }
    }

    public void OnDeleteMsg()
    {
        MessageBoxLogic.OpenOKCancelBox(39007, -1, OnDeleteMsgOK);
        //LogModule.DebugLog("Delete Message:" + _MsgInfo.GUID);
    }

    private void OnDeleteMsgOK()
    {
        LogModule.DebugLog("Delete Message:" + _MsgInfo.GUID);

        CG_REQ_DEL_LEAVE_WORD_FROM_SPACE packet = (CG_REQ_DEL_LEAVE_WORD_FROM_SPACE)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_DEL_LEAVE_WORD_FROM_SPACE);
        packet.Roleguid = CommunityLogic.CommunityPlayerInfo.GUID;
        packet.Leavewordindex = _MsgInfo.MsgIDX;
        packet.SendPacket();
    }
}