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

public class CommunityBlogInfo
{
    public ulong GUID;
    public string Contex = "";
    public int Profession;
    public List<string> ContexImageUrls;
    public string HeadIconUrl = "";
    public int Level;
    public string Name = "";
    public ulong RoleGuid;
    public int Time;
    public int ForwardingCnt;
    public int LikeCnt;
    public int CommentCnt;
    public bool IsLikeBlog;
    public List<LikedRoleInfo> LikeRoles;
    public List<BlogComment> Comments;
    public CommunityBlogInfo ForwardingBlog;
}

public class CommunityBlogItem : CommunityBlogContexItem
{
    #region 

    void Update()
    {
        
        CommentHeightUpdate();
    }

    #endregion

    public Text _TargetPlayerTime;
    public CommunityBlogContexItem _ForwardingContex;
    public GameObject _BlogOptPanel;
    public GameObject _CommentOptPanel;
    public Text _Time;
    public Text _ForwardingCnt;
    public Text _LikeCnt;
    public Text _CommentCnt;
    public Button _BtnLike;
    public Button _BtnDisLike;
    public CommunityBlogCommentItem _LikePlayerNames;
    public UIContainerBase _CommentContainer;
    public GameObject _BtnDelete;
    public Text _CommentRet;
    public LayoutElement _ContentLayout;

    private BlogComment _RetBlogComment;
    public CommunityBlogInfo _BlogInfo;
    private bool _IsShowDeleteBtn = false;

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

        var blogInfo = (CommunityBlogInfo)hash["InitObj"];
        bool isShowPlayerInfo = true;
        if (hash.ContainsKey("IsShowPlayerInfo"))
        {
            isShowPlayerInfo = (bool)hash["IsShowPlayerInfo"];
        }

        _IsFixedCommentHeight = false;
        if (hash.ContainsKey("IsFixedCommentHeight"))
        {
            _IsFixedCommentHeight = (bool)hash["IsFixedCommentHeight"];
        }

        _IsShowDeleteBtn = false;
        if (hash.ContainsKey("IsShowDeleteBtn"))
        {
            _IsShowDeleteBtn = (bool)hash["IsShowDeleteBtn"];
        }

        if (_ContentLayout != null)
        {
            _ContentLayout.enabled = false;
        }
        _ContexText._ContexText.verticalOverflow = VerticalWrapMode.Overflow;

        _GUID = blogInfo.RoleGuid;

        ShowBlogInfo(blogInfo, isShowPlayerInfo);
        OnBtnShowOpt();
    }

    public override void ShowBlogInfo(CommunityBlogInfo blogInfo, bool isShowPlayerInfo)
    {
        base.ShowBlogInfo(blogInfo, isShowPlayerInfo);
        _BlogInfo = blogInfo;

        if (blogInfo.ForwardingBlog != null)
        {
            _ForwardingContex.gameObject.SetActive(true);
            _ForwardingContex.ShowBlogInfo(blogInfo.ForwardingBlog, true);
        }
        else
        {
            _ForwardingContex.gameObject.SetActive(false);
        }

        var timeStr = GetBlogTimeStr(blogInfo.Time);
        if (!isShowPlayerInfo)
        {
            _TargetPlayerTime.gameObject.SetActive(true);
            _Time.gameObject.SetActive(false);
            _TargetPlayerTime.text = timeStr;
        }
        else
        {
            _TargetPlayerTime.gameObject.SetActive(false);
            _Time.gameObject.SetActive(true);
            _Time.text = timeStr;
        }

        if (_IsShowDeleteBtn)
        {
            _BtnDelete.SetActive(true);
        }
        else
        {
            _BtnDelete.SetActive(false);
        }

        _ForwardingCnt.text = blogInfo.ForwardingCnt.ToString();
        _LikeCnt.text = blogInfo.LikeCnt.ToString();
        _CommentCnt.text = blogInfo.CommentCnt.ToString();

        blogInfo.IsLikeBlog = false;
        foreach (var likeInfo in blogInfo.LikeRoles)
        {
            if (likeInfo.Roleguid == GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid)
            {
                blogInfo.IsLikeBlog = true;
            }
        }

        InitLikeNames();
        InitLikeBtn();

        if (blogInfo.Comments.Count == 0)
        {
            _CommentContainer._ContainerObj.gameObject.SetActive(false);
        }
        else
        {
            _CommentContainer._ContainerObj.gameObject.SetActive(true);
            ShowComments();
        }

        if (blogInfo.Comments.Count == 0 && blogInfo.LikeRoles.Count == 0)
        {
            _CommentContainer.gameObject.SetActive(false);
        }
        else
        {
            _CommentContainer.gameObject.SetActive(true);
        }

        _GUID = blogInfo.RoleGuid;
    }

    private void InitLikeNames()
    {
        if (_BlogInfo.LikeRoles.Count == 0)
        {
            _LikePlayerNames.gameObject.SetActive(false);
        }
        else
        {
            _LikePlayerNames.gameObject.SetActive(true);
            string likeNameStr = "";
            for (int i = 0; i < _BlogInfo.LikeRoles.Count; ++i)
            {
                likeNameStr += StrDictionary.GetClientDictionaryString("#{39003}", _BlogInfo.LikeRoles[i].Rolename, _BlogInfo.LikeRoles[i].Roleguid);
            }
            _LikePlayerNames._MsgInfo.ShowLinkStr(likeNameStr);
        }
    }

    private void InitLikeBtn()
    {
        if (_BlogInfo.IsLikeBlog)
        {
            _BtnLike.gameObject.SetActive(false);
            _BtnDisLike.gameObject.SetActive(true);
        }
        else
        {
            _BtnLike.gameObject.SetActive(true);
            _BtnDisLike.gameObject.SetActive(false);
        }

        if (_BlogInfo.Comments.Count == 0 && _BlogInfo.LikeRoles.Count == 0)
        {
            _CommentContainer.gameObject.SetActive(false);
        }
        else
        {
            _CommentContainer.gameObject.SetActive(true);
        }

        _LikeCnt.text = _BlogInfo.LikeCnt.ToString();
    }

    public static string GetBlogTimeStr(int serverTime)
    {
        GuiTextDebug.debug("Blog ServerTime:" + serverTime);
        string timeStr = "";
        var dateTime = Utils.GetServerDateTime(serverTime);
        var nowTime = Utils.GetServerDateTime();
        if (dateTime.Day == nowTime.Day && dateTime.Hour == nowTime.Hour)
        {
            timeStr = StrDictionary.GetClientDictionaryString("#{39014}", nowTime.Minute - dateTime.Minute);
        }
        else if (dateTime.Day == nowTime.Day)
        {
            timeStr = StrDictionary.GetClientDictionaryString("#{39001}", nowTime.Hour - dateTime.Hour);
        }
        else
        {
            timeStr = StrDictionary.GetClientDictionaryString("#{39002}", dateTime.Month, dateTime.Day);
        }
        return timeStr;
    }

    public void OnBtnDelete()
    {
        MessageBoxLogic.OpenOKCancelBox(39008, -1, OnDeleteOk);
    }

    private void OnDeleteOk()
    {
        CG_REQ_DELETE_BLOG packet = (CG_REQ_DELETE_BLOG)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_DELETE_BLOG);
        packet.Blogguid = _BlogInfo.GUID;
        packet.SendPacket();
    }

    public void OnFwording()
    {
        var targetSprite = _Icon;
        if (!_Icon.isActiveAndEnabled)
        {
            targetSprite = CommunityLogic.Instance()._Icon;
        }
        if (_BlogInfo.ForwardingBlog != null)
        {
            targetSprite = _ForwardingContex._Icon;
        }
        if (targetSprite == null)
        {
            targetSprite = CommunityLogic.Instance()._Icon;
        }
        CommunityLogic.Instance().ShowFwording(_BlogInfo, targetSprite);
    }

    public void OnBtnLike()
    {
        Community.Instance.SetLikeBlog(this);
    }

    public void AddLike()
    {
        ++_BlogInfo.LikeCnt;
        _BlogInfo.LikeRoles.Insert(0, new LikedRoleInfo() { Roleguid = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid, Rolename = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.RoleName });
        _BlogInfo.IsLikeBlog = true;

        InitLikeNames();
        InitLikeBtn();
    }

    public void OnBtnDisLike()
    {
        Community.Instance.SetDisLikeBlog(this);
    }

    public void RemoveLike()
    {
        --_BlogInfo.LikeCnt;
        var myGuid = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid;
        var myLikeInfo = _BlogInfo.LikeRoles.Find((likeRole) =>
        {
            if (likeRole.Roleguid == myGuid)
                return true;
            return false;
        });
        if (myLikeInfo != null)
        {
            _BlogInfo.LikeRoles.Remove(myLikeInfo);
        }
        _BlogInfo.IsLikeBlog = false;

        InitLikeNames();
        InitLikeBtn();
    }

    public void OnBtnShowComment()
    {
        _RetBlogComment = null;
        _BlogOptPanel.SetActive(false);
        _CommentOptPanel.SetActive(true);

        _CommentRet.text = StrDictionary.GetClientDictionaryString("#{39006}", _BlogInfo.Name);
    }

    public void OnBtnShowOpt()
    {
        _RetBlogComment = null;
        _BlogOptPanel.SetActive(true);
        _CommentOptPanel.SetActive(false);
    }

    public void OnCommentClick(object commentObj)
    {
        BlogComment blogComment = commentObj as BlogComment;
        if (blogComment == null)
            return;

        _BlogOptPanel.SetActive(false);
        _CommentOptPanel.SetActive(true);
        _RetBlogComment = blogComment;
        _CommentRet.text = StrDictionary.GetClientDictionaryString("#{39006}", _RetBlogComment.Rolename);
    }

    public void OnCommentSend(string commentContex)
    {
        if (string.IsNullOrEmpty(commentContex))
        {
            return;
        }

        if (Utils.IsStrFilter_Chat(commentContex))
        {
            GUIData.AddNotifyData("#{39025}");
            return;
        }

        LogModule.DebugLog("Comment:" + commentContex);

        ulong guid;
        if(_RetBlogComment != null)
            guid = _RetBlogComment.Roleguid;
        else
            guid = GlobeVar.INVALID_GUID;
        Community.Instance.SetCommentBlog(this, commentContex, guid);
    }

    public void UpdateMyComment(string commentContex, int index)
    {

        BlogComment blogComment = new BlogComment();
        blogComment.Index = index;
        blogComment.Roleguid = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid;
        blogComment.Rolename = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.RoleName;
        blogComment.Comment = commentContex;
        if (_RetBlogComment != null)
        {
            blogComment.Desguid = _RetBlogComment.Roleguid;
            blogComment.Desname = _RetBlogComment.Rolename;
        }
        
        _BlogInfo.Comments.Insert(0, blogComment);

        if (_BlogInfo.Comments.Count > 0)
        {
            Hashtable hash = new Hashtable();
            hash.Add("ShowDelete", true);
            hash.Add("BlogInfo", _BlogInfo);
            hash.Add("BlogItem", this);
            _CommentContainer._ContainerObj.gameObject.SetActive(true);
            _CommentContainer.gameObject.SetActive(true);

            _LastCommentIdx = -1;
            for (int i = 0; i < _BlogInfo.Comments.Count; ++i)
            {
                if (_LastCommentIdx < _BlogInfo.Comments[i].Index)
                {
                    _LastCommentIdx = _BlogInfo.Comments[i].Index;
                }
            }

            _CommentContainer.InitContentItem(_BlogInfo.Comments, OnCommentClick, hash);
        }
        else
        {
            _CommentContainer._ContainerObj.gameObject.SetActive(false);
            _CommentContainer.InitContentItem(null);
        }

        _CommentCnt.text = (++_BlogInfo.CommentCnt).ToString();

        OnBtnShowOpt();
    }

    #region comment list

    private int _LastCommentIdx = 0;

    public void ShowComments()
    {
        //if (!_IsFixedCommentHeight)
        {
            if (_BlogInfo.Comments.Count > 0)
            {
                Hashtable hash = new Hashtable();
                hash.Add("ShowDelete", true);
                hash.Add("BlogInfo", _BlogInfo);
                hash.Add("BlogItem", this);

                _CommentContainer._ContainerObj.gameObject.SetActive(true);
                _CommentContainer.gameObject.SetActive(true);

                _LastCommentIdx = -1;
                for (int i = 0; i < _BlogInfo.Comments.Count; ++i)
                {
                    if (_LastCommentIdx < _BlogInfo.Comments[i].Index)
                    {
                        _LastCommentIdx = _BlogInfo.Comments[i].Index;
                    }
                }

                _CommentContainer.InitContentItem(_BlogInfo.Comments, OnCommentClick, hash);
            }
            else
            {
                _CommentContainer._ContainerObj.gameObject.SetActive(false);
                _CommentContainer.InitContentItem(null);
            }
        }
        //else
        //{
        //    _BlogInfo.Comments.Clear();
        //    _CommentContainer.InitContentItem(null);
        //    _LastCommentIdx = 0;
        //}
    }

    public void ReloadComments()
    {
        CG_REQ_VIEW_BLOG_COMMENT packet = (CG_REQ_VIEW_BLOG_COMMENT)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_VIEW_BLOG_COMMENT);
        packet.Blogguid = _BlogInfo.GUID;
        packet.Curindex = 0;
        packet.Desindex = _BlogInfo.Comments.Count;
        packet.Count = _BlogInfo.Comments.Count;
        packet.SendPacket();
    }

    public void LoadMoreComment(int num = 20)
    {
        CG_REQ_VIEW_BLOG_COMMENT packet = (CG_REQ_VIEW_BLOG_COMMENT)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_VIEW_BLOG_COMMENT);
        packet.Blogguid = _BlogInfo.GUID;
        packet.Curindex = _LastCommentIdx;
        packet.Desindex = packet.Curindex + 10000;
        packet.Count = num;
        packet.SendPacket();

        //RetBlogDetailCommentsTest();
    }

    public void ReloadCommentRet(GC_RET_VIEW_BLOG_COMMENT packet)
    {
        if (packet.Blogguid != _BlogInfo.GUID)
            return;

        for (int i = 0; i < packet.commentCount; ++i)
        {
            BlogComment blogComment = new BlogComment();

            blogComment.Index = packet.GetIndex(i);
            blogComment.Roleguid = packet.GetRoleguid(i);
            blogComment.Rolename = packet.GetRolename(i);
            blogComment.Comment = packet.GetComment(i);
            blogComment.Desguid = packet.GetTargetguid(i);
            blogComment.Desname = packet.GetTargetname(i);

            if (_LastCommentIdx < blogComment.Index)
            {
                _LastCommentIdx = blogComment.Index;
            }
            _BlogInfo.Comments.Add(blogComment);
        }

        InitLikeBtn();

        if (_BlogInfo.Comments.Count > 0)
        {
            _CommentContainer._ContainerObj.gameObject.SetActive(true);
            _CommentContainer.gameObject.SetActive(true);

            Hashtable hash = new Hashtable();
            hash.Add("ShowDelete", true);
            hash.Add("BlogInfo", _BlogInfo);
            hash.Add("BlogItem", this);
            _CommentContainer.InitContentItem(_BlogInfo.Comments, OnCommentClick, hash);
        }
        else
        {
            _CommentContainer._ContainerObj.gameObject.SetActive(false);
            _CommentContainer.InitContentItem(null);
        }
    }

    public void RetBlogDetailComments(GC_RET_VIEW_BLOG_COMMENT packet)
    {
        if (packet.Blogguid != _BlogInfo.GUID)
            return;

        for (int i = 0; i < packet.commentCount; ++i)
        {
            BlogComment blogComment = new BlogComment();

            blogComment.Index = packet.GetIndex(i);
            blogComment.Roleguid = packet.GetRoleguid(i);
            blogComment.Rolename = packet.GetRolename(i);
            blogComment.Comment = packet.GetComment(i);
            blogComment.Desguid = packet.GetTargetguid(i);
            blogComment.Desname = packet.GetTargetname(i);

            if (_LastCommentIdx < blogComment.Index)
            {
                _LastCommentIdx = blogComment.Index;
            }

            _BlogInfo.Comments.Add(blogComment);
        }

        InitLikeBtn();

        if (_BlogInfo.Comments.Count > 0)
        {
            _CommentContainer._ContainerObj.gameObject.SetActive(true);
            _CommentContainer.gameObject.SetActive(true);

            Hashtable hash = new Hashtable();
            hash.Add("ShowDelete", true);
            hash.Add("BlogInfo", _BlogInfo);
            hash.Add("BlogItem", this);
            _CommentContainer.InitContentItem(_BlogInfo.Comments, OnCommentClick, hash);
        }
        else
        {
            _CommentContainer._ContainerObj.gameObject.SetActive(false);
            _CommentContainer.InitContentItem(null);
        }
    }

    public void RetBlogDetailCommentsTest()
    {

        for (int i = 0; i < 10; ++i)
        {
            BlogComment blogComment = new BlogComment();
            _BlogInfo.Comments.Add(blogComment);

            blogComment.Roleguid = (ulong)(i + _LastCommentIdx);
            blogComment.Rolename = _LastCommentIdx.ToString();
            blogComment.Comment = "test " + _LastCommentIdx;
            blogComment.Desguid = GlobeVar.INVALID_GUID;
            blogComment.Desname = "";

            ++_LastCommentIdx;
        }

        Hashtable hash = new Hashtable();
        hash.Add("ShowDelete", true);
        hash.Add("BlogInfo", _BlogInfo);
        hash.Add("BlogItem", this);
        _CommentContainer.InitContentItem(_BlogInfo.Comments, OnCommentClick, hash);
    }

    #endregion

    #region comment rect

    public LayoutElement _CommentLayout;
    private RectTransform _CommentRect;

    private bool _IsFixedCommentHeight;

    private void CommentHeightUpdate()
    {
        if (_IsFixedCommentHeight)
        {
            if (_CommentRect == null)
            {
                _CommentRect = _CommentLayout.GetComponent<RectTransform>();
            }
            _CommentLayout.preferredHeight = -_CommentRect.anchoredPosition.y + 30;
        }
        else
        {
            if (_CommentContainer._ContainerObj.gameObject.activeSelf)
            {
                _CommentLayout.preferredHeight = _CommentContainer._ContainerObj.sizeDelta.y;
            }
            else
            {
                _CommentLayout.preferredHeight = 0;
            }

            if (_LikePlayerNames.gameObject.activeSelf)
            {
                _CommentLayout.preferredHeight += _LikePlayerNames._MsgInfo._ContexText.rectTransform.sizeDelta.y + 5;
            }
        }

        if (_ContexText._ContexText.rectTransform.sizeDelta.y > 300)
        {
            _ContentLayout.enabled = true;
            _ContentLayout.preferredHeight = 300;
            _ContexText._ContexText.verticalOverflow = VerticalWrapMode.Truncate;
        }

    }

    #endregion

    #region 

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

        if (!_IsFixedCommentHeight)
        {
            CommunityLogic.Instance().ShowBlogDetail(_BlogInfo);
        }
    }

    #endregion
}