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

public class ChatHistoryLogicFriend : ChatHistoryLogic
{
    public GameObject _ChatTimePrefab;
    public GameObject _ChatInfoSys;

    public void OnDisable()
    {
        PlayerPreferenceData.SetFriendChatHistory(GameManager.gameManager.PlayerDataPool.ChatHistory.FriendChatList);
    }

    #region chat history

    public override void OnReceiveChat()
    {
        int HistoryCount = GameManager.gameManager.PlayerDataPool.ChatHistory.ChatHistoryList.Count;
        ChatHistoryItem LastHistory = GameManager.gameManager.PlayerDataPool.ChatHistory.ChatHistoryList[HistoryCount - 1];
        if ((LastHistory.EChannel == GC_CHAT.CHATTYPE.CHAT_TYPE_FRIEND || LastHistory.EChannel == GC_CHAT.CHATTYPE.CHAT_TYPE_TELL))
        {
            bool isNeedInsertTime = false;
            if (GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid != LastHistory.SenderGuid)
            {
                if (LastHistory.SenderGuid == 0)
                {
                    if (string.Equals("1", LastHistory.SenderName))
                    {
                        LastHistory.SenderIcon = Utils.GetSysIconName(Utils.SysIconType.Sys);
                    }
                    else if (string.Equals("2", LastHistory.SenderName))
                    {
                        LastHistory.SenderIcon = Utils.GetSysIconName(Utils.SysIconType.Guild);
                    }
                    isNeedInsertTime = GameManager.gameManager.PlayerDataPool.ChatHistory.AddFriendChat(LastHistory.SenderGuid, LastHistory.SenderName,
                        LastHistory.SenderIcon, LastHistory.SenderLevel, LastHistory.SenderGuid, LastHistory.SenderVIPLevel, LastHistory.SenderPrivilegeVIP, LastHistory);
                }
                else
                {
                    isNeedInsertTime = GameManager.gameManager.PlayerDataPool.ChatHistory.AddFriendChat(LastHistory.SenderGuid, LastHistory.SenderName, 
                        LastHistory.SenderIcon, LastHistory.SenderLevel,
                        LastHistory.SenderGuid, LastHistory.SenderVIPLevel, LastHistory.SenderPrivilegeVIP, LastHistory);
                }
            }
            else
            {
                isNeedInsertTime = GameManager.gameManager.PlayerDataPool.ChatHistory.AddFriendChat(LastHistory.ReceiverGuid, LastHistory.ReceiverName, "",
                    0, LastHistory.SenderGuid, -1, -1, LastHistory);
            }

            if (_FriendGuid == LastHistory.SenderGuid
                || GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid == LastHistory.SenderGuid)
            {   
                if(isNeedInsertTime)
                    InsertTime(GCGame.Utils.GetServerDateTime().ToString());
                {
                    HandleChatHistory(LastHistory);
                }
            }
        }
    }

    protected override void HandleChatHistory(ChatHistoryItem history, bool showLast = true)
    {
        if (history.SenderGuid == GlobeVar.SYSFRIEND_GUID || history.IsSystem)
        {
            var chatLogInfo = GetIdleChatLogItem(_ChatInfoSys);
            chatLogInfo.SetChatLog(history);

            if (showLast)
            {
                chatLogInfo.RectTransform.SetAsLastSibling();
                _ShowingChatLog.Add(chatLogInfo);
            }
            else
            {
                chatLogInfo.RectTransform.SetAsFirstSibling();
                _ShowingChatLog.Insert(0, chatLogInfo);
            }
        }
        else
        {
            base.HandleChatHistory(history, showLast);
        }
    }

    public override void ShowHistory(UInt64 friendGuid)
    {
        _FriendGuid = friendGuid;
        _SelectedCannel = (int)GC_CHAT.CHATTYPE.CHAT_TYPE_FRIEND;

        ClearHistory();
        ClearUnReanNew();

        FriendChat friendInfo = GameManager.gameManager.PlayerDataPool.ChatHistory.GetFriendChat(friendGuid);
        if (friendInfo == null || friendInfo.ChatList==null)
            return;

        int maxCount = friendInfo.ChatList.Count;
        maxCount = Math.Min(_SHOW_COUNT_IN_SINGLE_WIN, maxCount);
        int startIdx = friendInfo.ChatList.Count - maxCount;
        for (int j = startIdx; j < startIdx + maxCount; ++j)
        {
            
            var chatInfo = friendInfo.ChatList[j];
            ShowChatItem(friendInfo, chatInfo);
        }

        if (_ShowingChatLog.Count > 0)
        {
            _ShowingHistoryIdxMin = startIdx;
            _ShowingHistoryIdxMax = startIdx + maxCount - 1;
        }
    }

    public override void UpdateChannelHistoryRange(int idxStart, bool isUp)
    {
        FriendChat friendInfo = GameManager.gameManager.PlayerDataPool.ChatHistory.GetFriendChat(_FriendGuid);
        if (friendInfo == null || friendInfo.ChatList == null)
            return;

        int showedCnt = 0;
        if (isUp)
        {
            for (int j = idxStart; j >= 0; --j)
            {
                var chatItem = friendInfo.ChatList[j];

                ShowChatItem(friendInfo, chatItem,false);
                _ShowingHistoryIdxMin = j;
                
                ++showedCnt;
                if (showedCnt == _SHOW_COUNT_IN_SINGLE_WIN * 0.5f)
                {
                    break;
                }
            }
        }
    }

    public override void UpdateChannelHistory()
    {
        ClearHistory();
        ClearUnReanNew();
        ShowHistory(_FriendGuid);
    }

    private void ShowChatItem(FriendChat friendInfo, FriendChatRecord chatInfo, bool isLast = true)
    {
        if (chatInfo._TempChatHistory == null)
        {
            chatInfo._TempChatHistory = new ChatHistoryItem();

            chatInfo._TempChatHistory.EChannel = GC_CHAT.CHATTYPE.CHAT_TYPE_FRIEND;
            chatInfo._TempChatHistory.ChatInfo = chatInfo.ChatInfo;
            chatInfo._TempChatHistory._ReadTip = chatInfo._ReadTip > 0;
        }
        else
        {
            chatInfo._ReadTip = chatInfo._TempChatHistory._ReadTip ? 1 : 0;
        }

        ChatHistoryItem chatHistory = chatInfo._TempChatHistory;
        switch (chatInfo.RecordType)
        {
            case FriendChatRecord.RECORD_TYPE.Time:
                InsertTime(chatHistory.ChatInfo);
                return;
            case FriendChatRecord.RECORD_TYPE.OtherChat:
                chatHistory.SenderGuid = friendInfo.ChatGuid;
                chatHistory.SenderName = friendInfo.ChatName;
                chatHistory.SenderIcon = friendInfo.ChatIcon;
                chatHistory.SenderLevel = friendInfo.ChatLevel;
                chatHistory.SenderVIPLevel = friendInfo.VIPLevel;
                chatHistory.SenderPrivilegeVIP = friendInfo.PrivilegeVIP;
                chatHistory.HeadBGType = chatInfo._HeadType;
                chatHistory.ChatPopType = chatInfo._ChatPopType;
                break;
            case FriendChatRecord.RECORD_TYPE.MyChat:
                chatHistory.SenderGuid = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid;
                chatHistory.SenderName = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.RoleName;
                chatHistory.SenderIcon = Utils.GetProfessionSpriteName(GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Profession);
                chatHistory.SenderLevel = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Level;
                chatHistory.SenderVIPLevel = VipData.GetVipLv();
                chatHistory.SenderPrivilegeVIP = GameManager.gameManager.PlayerDataPool.GetMaxPrivilegeVip();
                chatHistory.HeadBGType = GameManager.gameManager.PlayerDataPool.PlayerOrnamentStateInfo.OrnamentHeadBGID;
                chatHistory.ChatPopType = GameManager.gameManager.PlayerDataPool.PlayerOrnamentStateInfo.OrnamentChatBGID;
                break;
            case FriendChatRecord.RECORD_TYPE.SysChat:
                chatHistory.SenderGuid = friendInfo.ChatGuid;
                chatHistory.SenderName = friendInfo.ChatName;
                chatHistory.SenderIcon = Utils.GetSysIconName(Utils.SysIconType.Sys);
                chatHistory.SenderLevel = friendInfo.ChatLevel;
                break;
            case FriendChatRecord.RECORD_TYPE.GuideChat:
                chatHistory.SenderGuid = friendInfo.ChatGuid;
                chatHistory.SenderName = friendInfo.ChatName;
                chatHistory.SenderIcon = Utils.GetSysIconName(Utils.SysIconType.Guild);
                chatHistory.SenderLevel = friendInfo.ChatLevel;
                chatHistory.HeadBGType = chatInfo._HeadType;
                chatHistory.ChatPopType = chatInfo._ChatPopType;
                break;
        }
        
        try
        {
            HandleChatHistory(chatInfo._TempChatHistory, isLast);
        }
        catch (Exception e)
        {
            LogModule.ErrorLog("chat history error:" + e.ToString());
        }
    }

    private void InsertTime(string timeStr)
    {
        var chatLogInfo = GetIdleChatLogItem(_ChatTimePrefab);
        var chatTimeLog = chatLogInfo as ChatInfoLogItemTime;
        chatTimeLog.SetTime(timeStr);
    }


    #endregion

}