using Module.Log;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PvpBothInfoPanel : MonoBehaviour {

    private const float _MaxCountTime = 30.0f;  //界面存在超过30S强制关闭
    public static PvpBothInfoPanel Instance;
    private void Awake()
    {
        Instance = this;
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    private void OnEnable()
    {
        InitItem();
        if (GameManager.gameManager.PlayerDataPool.pvpIfo._CountMaskEndTime != -1)
            ShowMaskAndCountTimePanel(GameManager.gameManager.PlayerDataPool.pvpIfo._CountMaskEndTime);
    }

    //现在是收不到服务器的倒计时协议,所以会一直开着界面
    //强制在30S之后关闭界面
    private float _InstanceCountTime = 0.0f;
    private void Update()
    {
        _InstanceCountTime += Time.deltaTime;
        if(_InstanceCountTime >= _MaxCountTime)
        {
            UIManager.CloseUI(UIInfo.PvpBothInfoPanel);
        }
    }

    public List<PvpBothInfoItem> _BlueItemList;
    public List<PvpBothInfoItem> _RedItemList;

    public GameObject _BothInfoPanel;
    public GameObject _MaskAndCountTimePanel;

    public List<Sprite> _PvpHeadIconList;
    public void ShowBothInfoPanel()
    {
        if(_CountDownEndTime > GlobalData.ServerAnsiTime)
        {
            _BothInfoPanel.SetActive(false);
            _MaskAndCountTimePanel.SetActive(true);
        }else
        {
            _BothInfoPanel.SetActive(true);
            _MaskAndCountTimePanel.SetActive(false);
        }
    }

    public UIImgText _CountTimeText;
    private int _CountDownEndTime = -1;
    public void ShowMaskAndCountTimePanel(int endTime)
    {
        _CountDownEndTime = endTime;
        GameManager.gameManager.PlayerDataPool.pvpIfo._CountMaskEndTime = -1;
        var remainTime = endTime - GlobalData.ServerAnsiTime;
        _BothInfoPanel.SetActive(false);
        _MaskAndCountTimePanel.SetActive(true);
        if (remainTime > 0)
        {
            _CountTimeText.text = remainTime + "";
            StartCoroutine(CountMaskAndCounTime(endTime));
        }
        else
        {
            //在计时结束的时候自动战斗
            if (Singleton<ObjManager>.Instance.MainPlayer != null)
                Singleton<ObjManager>.Instance.MainPlayer.RefreshPlayerAutoCombat();
            UIManager.CloseUI(UIInfo.PvpBothInfoPanel);
        }
    }

    IEnumerator CountMaskAndCounTime(int endTime)
    {
        while(true)
        {
            yield return new WaitForSeconds(1.0f);
            var remainTime = endTime - GlobalData.ServerAnsiTime;
            if (remainTime < 0)
            {
                //在计时结束的时候自动战斗
                if (Singleton<ObjManager>.Instance.MainPlayer != null)
                    Singleton<ObjManager>.Instance.MainPlayer.RefreshPlayerAutoCombat();
                UIManager.CloseUI(UIInfo.PvpBothInfoPanel);
                yield break;
            }
            _CountTimeText.text = remainTime + "";
        }
    }
    
    public void InitItem()
    {
        var blueTeamList = GameManager.gameManager.PlayerDataPool.pvpIfo._BattleBlueTeamList;
        if (blueTeamList == null)
        {
            LogModule.ErrorLog("BlueTeamListCount is null, return");
            return;
        }
        LogModule.ErrorLog("BlueTeamListCount : " + blueTeamList.Count);
        for (int index = 0; index < blueTeamList.Count; index++)
        {
            if (index < _BlueItemList.Count)
            {
                if (!_BlueItemList[index].gameObject.activeInHierarchy)
                    _BlueItemList[index].gameObject.SetActive(true);
                _BlueItemList[index].InitItem(blueTeamList[index]);
            }
        }

        var redTeamList = GameManager.gameManager.PlayerDataPool.pvpIfo._BattleRedTeamList;
        if (redTeamList == null)
        {
            LogModule.ErrorLog("RedTeamList is null, return");
            return;
        }

        LogModule.ErrorLog("RedTemListCount : " + redTeamList.Count);
        for (int index = 0; index < redTeamList.Count; index++)
        {
            if (index < _RedItemList.Count)
            {
                if(!_RedItemList[index].gameObject.activeInHierarchy)
                    _RedItemList[index].gameObject.SetActive(true);
                _RedItemList[index].InitItem(redTeamList[index]);
            }
        }
        LogModule.ErrorLog("Init BothInfoPanel");
    }
}