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

public class RedPacket
{
    #region 

    private static RedPacket _Instance;
    public static RedPacket Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = new RedPacket();
            }
            return _Instance;
        }
    }

    private RedPacket() { }

    #endregion

    public void PickRedPacket(int packetID, int packetType)
    {
        CG_REQ_RED_PACKET_DETAIL_INFO packet = (CG_REQ_RED_PACKET_DETAIL_INFO)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_RED_PACKET_DETAIL_INFO);
        packet.Id = packetID;
        packet.Type = packetType;
        packet.SendPacket();

        LogModule.DebugLog("pick red pack:" + packetID + "," + packetType);

        if (RedPacketRecvRoot.Instance())
        {
            RedPacketRecvRoot.Instance().OnBtnRefresh();
        }
    }

    public void RecvRedPacket(GC_GIVE_RED_PACKET_RET packet)
    {
        if (ActiveBtns.Instance())
        {
            ActiveBtns.Instance().ShowRedPacket(packet);
        }
        var history = GetRedPacketChatHistory(packet);
        GameManager.gameManager.PlayerDataPool.ChatHistory.AddHistory(history);
        if (ChatFrameLogic.Instance() != null)
        {
            ChatFrameLogic.Instance().OnReceiveChat(null);
        }
        if (ChatInfoLogic.Instance() != null)
        {
            ChatInfoLogic.Instance().OnReceiveChat();
            ChatInfoLogic.Instance().ShowRedPacketEffect();
        }
        else
        {
            ChatInfoLogic.ShowRedPacket(true);
        }

        LogModule.DebugLog("RecvRedPacket");
        HornPanelCtr.PopHornMessage(history);

        if (RedPacketRecvRoot.Instance())
        {
            RedPacketRecvRoot.Instance().OnBtnRefresh();
        }

        if (packet.HasRedPacketRain == (int)GC_GIVE_RED_PACKET_RET.ERedPacketRain.ETypeHasRedPacketRain)
        {
            GlobalEffectMgr.PlayRedPacketRain();
        }
    }

    private ChatHistoryItem GetRedPacketChatHistory(GC_GIVE_RED_PACKET_RET packet)
    {

        // 记录聊天历史
        ChatHistoryItem history = new ChatHistoryItem();
        history.CleanUp();
        history.SenderGuid = packet.SenderGuid;
        history.SenderName = packet.SenderName;
        history.SenderIcon = Utils.GetProfessionSpriteName(packet.SenderProfession);
        history.SenderPro = packet.SenderProfession;
        history.SenderVIPLevel = packet.VipLevel;
        history.SenderPrivilegeVIP = packet.PrivilegeVIP;
        history.SenderLevel = packet.SenderLevel;
        if(packet.HasHeadBGType)
            history.HeadBGType = packet.HeadBGType;
        if(packet.HasChatPopType)
            history.ChatPopType = packet.ChatPopType;

        if (packet.Type != (int)GC_GIVE_RED_PACKET_RET.ERedPacketType.ETypeWorld && packet.Type != (int)GC_GIVE_RED_PACKET_RET.ERedPacketType.ETypeGuild)
        {
            char firstChar = packet.RedPacketStr[0];
            if (firstChar != '#')
            {
                history.ChatInfo = packet.RedPacketStr;
            }
            else
            {
                history.ChatInfo = StrDictionary.GetServerDictionaryFormatString(packet.RedPacketStr);
            }
        }
        else
        {
            history.ChatInfo = Utils.StrFilter_Chat(packet.RedPacketStr);
        }

        history.ChatInfo = history.ChatInfo.Replace(" ", " ");
        history.EChannel = packet.Type == (int)GC_GIVE_RED_PACKET_RET.ERedPacketType.ETypeGuild ? GC_CHAT.CHATTYPE.CHAT_TYPE_GUILD : GC_CHAT.CHATTYPE.CHAT_TYPE_WORLD;
        history.SenderVIPLevel = packet.VipLevel;
        history.SetRecMsgTime();
        history._RedPacketID = packet.RedPacketId;
        history._RedPacketType = packet.Type;
        if (packet.Type == (int)GC_GIVE_RED_PACKET_RET.ERedPacketType.ETypeSystem)
        {
            history.SenderName = StrDictionary.GetClientDictionaryString("#{4336}");
        }

        return history;

    }
}