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

public class CommunityBlogPageLogic : UIControllerBase<CommunityBlogPageLogic>
{


    void OnEnable ()
    {
        SetInstance(this);
        InitPage();
    }

    void OnDisable()
    {
        SetInstance(null);
    }

    public void CloseWindow()
    {
        gameObject.SetActive(false);
    }

    #region 

    public GameObject _FocusBlogPanel;
    public GameObject _MyBlogPanel;
    public GameObject _OtherBlogPanel;
    public GameObject _FocusPlayersPanel;
    public GameObject _MsgHistoryPanel;

    public void InitPage()
    {
        if (CommunityLogic.CommunityPlayerInfo.GUID == GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid)
        {
            InitFocusBlogPage();
        }
        else
        {
            InitOtherBlogPage();
        }
    }

    #endregion

    #region focus Blog page

    public UIContainerBlog _FocusBlogContainer;

    public void UpdateNoMoreFocus()
    {
        _FocusBlogContainer.IsLoadAllItems = true;

        if (Community.Instance._LoadedPlayerBlogs.Count > 0)
        {
            CommunityLogic.Instance()._EmptyTip.text = "";
        }
        else
        {
            CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39004}");
        }
    }

    public void UpdateFocusBlogs()
    {
        Hashtable hash = new Hashtable();
        hash.Add("IsShowPlayerInfo", true);
        hash.Add("IsFixedCommentHeight", false);

        if (_FocusBlogContainer.isActiveAndEnabled)
        {
            _FocusBlogContainer.InitContentItem(Community.Instance._LoadedPlayerBlogs, null, hash);
        }

        if (Community.Instance._LoadedPlayerBlogs.Count > 0)
        {
            CommunityLogic.Instance()._EmptyTip.text = "";
        }
        else
        {
            CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39004}");
        }
    }

    public void InitFocusBlogPage()
    {
        _FocusBlogPanel.SetActive(true);
        _MyBlogPanel.SetActive(false);
        _FocusPlayersPanel.SetActive(false);
        _MsgHistoryPanel.SetActive(false);
        _OtherBlogPanel.SetActive(false);

        RefreshTitle();
        Community.Instance.ReLoadMoreFocusBlog();
    }

    public void LoadMoreFocusBlog()
    {
        Community.Instance.LoadMoreFocusBlog();
    }

    #endregion

    #region my blog page

    public UIContainerBlog _MyBlogContainer;
    public Text _PageTitle;

    public void RefreshTitle()
    {
        if (_FocusBlogPanel.activeSelf)
        {
            if (CommunityLogic.CommunityPlayerInfo.GUID == GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid)
            {
                _PageTitle.text = StrDictionary.GetClientDictionaryString("#{41322}");
            }
            else
            {
                _PageTitle.text = StrDictionary.GetClientDictionaryString("#{41321}", CommunityLogic.CommunityPlayerInfo.Name);
            }
        }
        else
        {
            if (CommunityLogic.CommunityPlayerInfo.GUID == GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid)
            {
                _PageTitle.text = StrDictionary.GetClientDictionaryString("#{41323}");
            }
            else
            {
                _PageTitle.text = StrDictionary.GetClientDictionaryString("#{41320}", CommunityLogic.CommunityPlayerInfo.Name);
            }
        }
    }

    public void InitMyBlogPage()
    {
        _FocusBlogPanel.SetActive(false);
        _MyBlogPanel.SetActive(true);
        _FocusPlayersPanel.SetActive(false);
        _MsgHistoryPanel.SetActive(false);
        _OtherBlogPanel.SetActive(false);

        RefreshTitle();

        Community.Instance.ReLoadMoreMyBlog();
    }

    public void UpdateNoMoreBlogs()
    {
        _BlogContainer.IsLoadAllItems = true;
        _MyBlogContainer.IsLoadAllItems = true;

        UpdatePlayerBlogs();
    }

    public void UpdatePlayerBlogs()
    {
        Hashtable hash = new Hashtable();
        hash.Add("IsShowPlayerInfo", false);
        hash.Add("IsFixedCommentHeight", false);

        if (_BlogContainer.isActiveAndEnabled)
        {
            _BlogContainer.InitContentItem(Community.Instance._LoadedPlayerBlogs, null, hash);
        }
        else
        {
            hash.Add("IsShowDeleteBtn", true);
            _MyBlogContainer.InitContentItem(Community.Instance._LoadedPlayerBlogs, null, hash);
        }

        if (Community.Instance._LoadedPlayerBlogs.Count > 0)
        {
            CommunityLogic.Instance()._EmptyTip.text = "";
        }
        else if (CommunityLogic.Instance().IsShowingMyBlog())
        {
            CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39020}");
        }
        else if (!CommunityLogic.Instance().IsShowingMyBlog())
        {
            CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39004}");
        }
    }

    public void LoadMoreMyBlog()
    {
        Community.Instance.LoadMoreMyBlog();
    }

    #endregion

    #region other blog

    public UIContainerBlog _BlogContainer;
    public GameObject _BtnFocus;
    public GameObject _BtnCancelFocus;
    public GameObject _BtnFriend;
    public GameObject _AlreadyFriend;

    public void InitOtherBlogPage()
    {
        _FocusBlogPanel.SetActive(false);
        _MyBlogPanel.SetActive(false);
        _FocusPlayersPanel.SetActive(false);
        _MsgHistoryPanel.SetActive(false);
        _OtherBlogPanel.SetActive(true);

        Community.Instance.LoadMoreFocus();
        Community.Instance.ReloadFocus();
        UpdateFocusBtn();

        RefreshTitle();

        Community.Instance.ReLoadMoreMyBlog();
    }

    public void UpdateFocusBtn()
    {
        if (GameManager.gameManager.PlayerDataPool.FriendList.IsExist(CommunityLogic.CommunityPlayerInfo.GUID))
        {
            _BtnFriend.SetActive(false);
            _AlreadyFriend.SetActive(true);
        }
        else
        {
            _BtnFriend.SetActive(true);
            _AlreadyFriend.SetActive(false);
        }

        for (int i = 0; i < Community.Instance._CommunityPlayerInfos.Count; ++i)
        {
            if (Community.Instance._CommunityPlayerInfos[i].GUID == CommunityLogic.CommunityPlayerInfo.GUID)
            {
                _BtnFocus.SetActive(false);
                _BtnCancelFocus.SetActive(true);
                return;
            }
        }

        _BtnFocus.SetActive(true);
        _BtnCancelFocus.SetActive(false);

    }

    public void OnBtnFocus()
    {
        CG_REQ_WATCH_ROLE packet = (CG_REQ_WATCH_ROLE)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_WATCH_ROLE);
        packet.Roleguid = CommunityLogic.CommunityPlayerInfo.GUID;
        packet.SendPacket();
    }

    public void OnBtnCancelFocus()
    {
        CG_REQ_DISWATCH_ROLE packet = (CG_REQ_DISWATCH_ROLE)PacketDistributed.CreatePacket(MessageID.PACKET_CG_REQ_DISWATCH_ROLE);
        packet.Roleguid = CommunityLogic.CommunityPlayerInfo.GUID;
        packet.SendPacket();
    }

    public void OnBtnFriend()
    {
        //如果非玩家,则不显示
        if (GlobeVar.INVALID_GUID == CommunityLogic.CommunityPlayerInfo.GUID)
        {
            return;
        }

        if (Singleton<ObjManager>.Instance.MainPlayer != null)
        {
            Singleton<ObjManager>.Instance.MainPlayer.ReqAddFriend(CommunityLogic.CommunityPlayerInfo.GUID);
        }
    }

    public void RetFocus(GC_RET_WATCH_ROLE packet)
    {
        Community.Instance.ReloadFocus();
    }

    public void RetFocus(GC_RET_DISWATCH_ROLE packet)
    {
        Community.Instance.ReloadFocus();
    }

    #endregion

    #region focus page

    public UIContainerBase _FocusPlayersContainer;

    public void InitFocusPlayersPage()
    {
        _FocusBlogPanel.SetActive(false);
        _MyBlogPanel.SetActive(false);
        _FocusPlayersPanel.SetActive(true);
        _MsgHistoryPanel.SetActive(false);
        _OtherBlogPanel.SetActive(false);

        if (Community.Instance._CommunityPlayerInfos == null || Community.Instance._CommunityPlayerInfos.Count == 0)
        {
            Community.Instance.ReloadFocus();
        }
        else
        {
            UpdateFocusList();
        }
    }

    public void UpdateFocusList()
    {
        _FocusPlayersContainer.InitContentItem(Community.Instance._CommunityPlayerInfos);
        CommunityLogic.Instance()._EmptyTip.text = "";

        if (Community.Instance._CommunityPlayerInfos.Count > 0)
        {
            CommunityLogic.Instance()._EmptyTip.text = "";
        }
        else
        {
            CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39019}");
        }
    }

    public void BackToMyBlog()
    {
        _FocusBlogPanel.SetActive(false);
        _MyBlogPanel.SetActive(true);
        _FocusPlayersPanel.SetActive(false);
        _MsgHistoryPanel.SetActive(false);

        //if (Community.Instance._LoadedPlayerBlogs.Count > 0)
        //{
        //    CommunityLogic.Instance()._EmptyTip.text = "";
        //}
        //else if(CommunityLogic.Instance().IsShowingMyBlog())
        //{
        //    CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39020}");
        //}
        //else if (!CommunityLogic.Instance().IsShowingMyBlog())
        //{
        //    CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39004}");
        //}

        Community.Instance.ReLoadMoreMyBlog();

    }

    public void BackToMain()
    {
        if (_MyBlogPanel.activeSelf)
        {
            BackToMyBlog();
        }
        else if (_OtherBlogPanel.activeSelf)
        {
            Community.Instance.ReLoadMoreMyBlog();
        }
        else
        {
            InitFocusBlogPage();
        }
    }

    #endregion

    #region history msg page

    public UIContainerBlog _HistoryMsgContainer;

    public void InitHistoryMsgPage()
    {
        _FocusBlogPanel.SetActive(false);
        _MyBlogPanel.SetActive(false);
        _FocusPlayersPanel.SetActive(false);
        _MsgHistoryPanel.SetActive(true);
        _OtherBlogPanel.SetActive(false);

        _HistoryMsgContainer.InitContentItem(null);
        Community.Instance.ReLoadMoreHistory();
    }

    public void LoadMoreHistory()
    {
        Community.Instance.LoadMoreHistory();
    }

    public void UpdateHistoryMsg()
    {
        
        _HistoryMsgContainer.InitContentItem(Community.Instance._MyHistoryMsg, OnClickHistoryMsg);

        if (Community.Instance._MyHistoryMsg.Count > 0)
        {
            CommunityLogic.Instance()._EmptyTip.text = "";
        }
        else
        {
            CommunityLogic.Instance()._EmptyTip.text = StrDictionary.GetClientDictionaryString("#{39017}");
        }
    }

    private void OnClickHistoryMsg(object msgObj)
    {
        CommunityHistoryMsgInfo msgInfo = msgObj as CommunityHistoryMsgInfo;
        if (msgInfo == null)
            return;

        LogModule.DebugLog("Click history Msg");
    }

    #endregion

    #region tip

    public void OnLoadNewMsg()
    {
        InitHistoryMsgPage();
        Community.Instance.ResetBlogMsg();
    }

    #endregion
}