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

public class PvpTeamInfoPanel : MonoBehaviour {

    public static PvpTeamInfoPanel Instance;
    private void Awake()
    {
        Instance = this;
    }

    private void OnEnable()
    {
        InitContainer();
        InitEnemyContainer();
        CountRemainTime(GameManager.gameManager.PlayerDataPool.pvpIfo._PvpFightRoomEndTime);
    }

    private void OnDestroy()
    {
        Instance = null;
    }

    public UIContainerBase _SelfRoleInfoItemContainer;
    public UIContainerBase _EnemyRoleInfoItemContainer;
    public void InitContainer()
    {
        var pvpInfo = GameManager.gameManager.PlayerDataPool.pvpIfo;
        if (pvpInfo._TeamMemberInfoList == null || pvpInfo._TeamMemberInfoList.Count <= 0)
        {
            return;
        }
        List<HonorBattlefieldMatchMemberInfo> teamMemberList = new List<HonorBattlefieldMatchMemberInfo>();
        for(int index = 0; index < pvpInfo._TeamMemberInfoList.Count; index++)
        {
            if ((ulong)pvpInfo._TeamMemberInfoList[index].Guid != GameManager.gameManager.PlayerDataPool.MainPlayerBaseAttr.Guid)
                teamMemberList.Add(pvpInfo._TeamMemberInfoList[index]);
        }
        if(teamMemberList.Count > 0)
            _SelfRoleInfoItemContainer.InitContentItem(teamMemberList);
    }

    public void InitEnemyContainer()
    {
        var pvpInfo = GameManager.gameManager.PlayerDataPool.pvpIfo;
        if (pvpInfo._EnemyTeamMemberInfoList == null || pvpInfo._EnemyTeamMemberInfoList.Count <= 0)
        {
            return;
        }
        
        List<HonorBattlefieldMatchMemberInfo> teamMemberList = new List<HonorBattlefieldMatchMemberInfo>();
        for (int index = 0; index < pvpInfo._EnemyTeamMemberInfoList.Count; index++)
        {
                teamMemberList.Add(pvpInfo._EnemyTeamMemberInfoList[index]);
        }
        if (teamMemberList.Count > 0)
            _EnemyRoleInfoItemContainer.InitContentItem(teamMemberList);
    }

    public void OnPacket(RespHonorBattlefieldMatchUpdateMember info)
    {
        if(info.RoomId == GameManager.gameManager.PlayerDataPool.pvpIfo._PvpRoomId)
        {
            _SelfRoleInfoItemContainer.ForeachActiveItem<PvpSelfTeamRoleInfoItem>((item) =>
            {
                if (item._Guid == info.MemberInfo.Guid)
                {
                    item.RefreshItemInfo(info.MemberInfo);
                }
            });
        }else
        {
            if(_EnemyRoleInfoItemContainer.GetItemCount() > 0)
            {
                _EnemyRoleInfoItemContainer.ForeachActiveItem<PvpTeamRoleInfoItem>((item) =>
                {
                    if (item._Guid == info.MemberInfo.Guid)
                    {
                        item.RefreshItemInfo(info.MemberInfo);
                    }
                });
            }
            else
            {
                _EnemyRoleInfoItemContainer.InitContentItem(GameManager.gameManager.PlayerDataPool.pvpIfo._EnemyTeamMemberInfoList);
                _EnemyRoleInfoItemContainer.ForeachActiveItem<PvpTeamRoleInfoItem>((item) =>
                {
                    if (item._Guid == info.MemberInfo.Guid)
                    {
                        item.RefreshItemInfo(info.MemberInfo);
                    }
                });
            }
        }
    }

    public Text _RemainTime;
    private int _CountEndTime;
    public void CountRemainTime(int endTime)
    {
        //Debug.LogError("ServerTime : " + GlobalData.ServerAnsiTime + " ,endTime : " + endTime);
        _CountEndTime = endTime;
        if(_CountEndTime - GlobalData.ServerAnsiTime > 0)
        {
            isCountTime = true;
            _RemainTime.text = GCGame.Table.StrDictionary.GetClientDictionaryString("#{1794}", _CountEndTime - GlobalData.ServerAnsiTime);
        }else
        {
            isCountTime = false;
        }
    }

    bool isCountTime = false;
    private float _CountTime;
    private void Update()
    {
        if(isCountTime)
        {
            _CountTime += Time.deltaTime;
            if (_CountTime >= 1.0f)
            {
                _CountTime = 1 - _CountTime;
                if (_CountEndTime - GlobalData.ServerAnsiTime > 0)
                {
                    _RemainTime.text = GCGame.Table.StrDictionary.GetClientDictionaryString("#{1794}", _CountEndTime - GlobalData.ServerAnsiTime);
                }
                else
                {
                    isCountTime = false;
                    _RemainTime.text = GCGame.Table.StrDictionary.GetClientDictionaryString("#{1794}", 0);
                }
            }
        }
    }
}