using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GCGame.Table;

public class PvpRoomPanel : MonoBehaviour {

    public static PvpRoomPanel Instance;

    private void Awake()
    {
        Instance = this;
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    public void LeaveRoom()
    {
        _RoleItemContainer.InitContentItem(null);

        //_CreateRoomBtn.SetActive(true);
        //_EnterRoomBtn.SetActive(true);
        _InvateBtn.SetActive(false);
        _LeaveBtn.SetActive(false);
    }

    public void EnterRoom()
    {
        //_CreateRoomBtn.SetActive(false);
        //_EnterRoomBtn.SetActive(false);
        _InvateBtn.SetActive(false);
        _LeaveBtn.SetActive(false);
    }


    public UIContainerBase _RoleItemContainer;
    public Text _RemainTime;
    public GameObject _InfoPanel;
    public GameObject _ShowBtn;
    public GameObject _HideBtn;

    public GameObject _CreateRoomBtn;
    public GameObject _EnterRoomBtn;
    public GameObject _InvateBtn;
    public GameObject _LeaveBtn;

    private const int _MaxTeamNum = 5;
    private bool _IsRoomMaster;
    //队伍最多五人
    public void OnPacket(RespHonorBattlefieldMatchGetRoomInfo packet)
    {
        List<PvpRoleInfoStruct> roleInfoList = new List<PvpRoleInfoStruct>();
        _IsRoomMaster = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid == (ulong)packet.RoomInfo.LeaderGuid;
        bool isSelfMaster = GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid == (ulong)packet.RoomInfo.LeaderGuid;
        for (int index = 0; index < packet.MemberList.Count; index++)
        {
            var roleInfo = packet.MemberList[index];
            bool isMaster = roleInfo.Guid == packet.RoomInfo.LeaderGuid;
            bool isSelf = (ulong)roleInfo.Guid == GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid;
            if (isSelf)
                continue;
            roleInfoList.Add(new PvpRoleInfoStruct(roleInfo.Name, roleInfo.Job, 
                roleInfo.Segment, isMaster, isSelfMaster && !isMaster, roleInfo.Guid, true, index, packet.RoomInfo.LeaderGuid, roleInfo.Hp, roleInfo.MaxHp, roleInfo.Level));
        }

        for(int index = 0; index < roleInfoList.Count; index++)
        {
            if(roleInfoList[index].isMaster && index != 0)
            {
                var temp = roleInfoList[index];
                roleInfoList.RemoveAt(index);
                roleInfoList.Insert(0, temp);
            }
        }
        _RoleItemContainer.InitContentItem(roleInfoList);

        RefreshRoomState();
    }

    public void RefreshRoomState()
    {
        if(GameManager.gameManager.PlayerDataPool.pvpIfo._IsHavePvpRoom)
        {
            EnterRoom();
        }else
        {
            LeaveRoom();
        }
    }

    private void OnEnable()
    {
        CountRemainTime();
        OnShowBtn();
        RefreshRoomState();
    }

    public void CountRemainTime()
    {
        int endTime = GameManager.gameManager.PlayerDataPool.pvpIfo._RoomMatchRemainTime;
        var remainTime = endTime - GlobalData.ServerAnsiTime;
        if (remainTime > 0)
        {
            SetTimeText(remainTime);
            StopAllCoroutines();
            StartCoroutine(CountRemainTimeSyc());
        }
        else
        {
            SetTimeText(0);
        }
    }

    public void SetTimeText(int time)
    {
        if(time > 10)
        {
            _RemainTime.text = "<color=#FEF509FF>" + time + "</color>";
        }
        else
        {
            _RemainTime.text = time.ToString();
            _RemainTime.color = Color.red;
        }
    }

    IEnumerator CountRemainTimeSyc()
    {
        while(true)
        {
            yield return new WaitForSeconds(1.0f);
            var remainTime = GameManager.gameManager.PlayerDataPool.pvpIfo._RoomMatchRemainTime - GlobalData.ServerAnsiTime;
            if (remainTime <= 0)
            {
                SetTimeText(0);
                yield break;
            }
            SetTimeText(remainTime);
        }
     }

    private bool isShow = true;
    public void OnShowBtn()
    {
        isShow = true;
        _InfoPanel.SetActive(false);

        _ShowBtn.SetActive(false);
        _HideBtn.SetActive(false);
    }

    public void OnHideBtn()
    {
        isShow = false;
        _InfoPanel.SetActive(false);

        _ShowBtn.SetActive(false);
        _HideBtn.SetActive(false);
    }

    public void OnTitleIconBtn()
    {
        if(isShow)
        {
            OnHideBtn();
        }else
        {
            OnShowBtn();
        }
    }

    public void OnExitBtn()
    {
        ReqHonorBattlefieldMatchLeaveRoom req = new ReqHonorBattlefieldMatchLeaveRoom();
        req.Flag = 1;
        req.SendMsg();

        //UIManager.CloseUI(UIInfo.PvpRoomPanel);
    }

    public void OnEnterRoom()
    {
        UIManager.ShowUI(UIInfo.PvpRoomListPanel);
    }

    public void OnCreateRoom()
    {
        UIManager.ShowUI(UIInfo.PvpRoomListPanel, delegate(bool bSucess, object param) {
            if(bSucess)
            {
                PvpRoomListPanel.Instance.OnCreateRoomBtn();
            }
        });
    }

    public void OnInvateBtn()
    {
        if(!_IsRoomMaster)
        {
            GUIData.AddNotifyData(StrDictionary.GetClientDictionaryString("#{82041}"));
            return;
        }
        UIManager.ShowUI(UIInfo.PvpInvateRoot);
    }
}