using System;
using System.Collections;
using System.Diagnostics;
using UnityEngine;
using Wizard.Battle.View.Vfx;

namespace Wizard;

public class RankMatchEnemyAI : EnemyAI
{
	private Stopwatch _turnTimeStopWatch = new Stopwatch();

	public override bool IsStackAction => _isStackAction;

	public override bool IsConnectNetwork => _isConnectNetwork;

	public RankMatchEnemyAI()
	{
		base.IsRankMatchAI = true;
	}

	public override VfxBase GetEmote(AIEmoteCmdType cmdType, AISituationInfo situation = null, ClassCharaPrm.EmotionType receivedEmoteType = ClassCharaPrm.EmotionType.NULL, int emoteInput = -1)
	{
		return NullVfx.GetInstance();
	}

	protected override IEnumerator WeakLogicTimerCoroutine()
	{
		float currentMilliSec = 0f;
		float currentTimeOverLogicSec = 0f;
		_elapsedThinkingTime = 0f;
		_currentThinkingIntervalTime = 0f;
		_isRunTurnEndLimitTimer = false;
		_turnStartVfxFinishTime = 0f;
		_elapsedTurnTimeAfterTurnStartVfx = 0f;
		_turnTimeStopWatch.Reset();
		_turnTimeStopWatch.Start();
		while (true)
		{
			yield return null;
			if (base.IsBattleEnd)
			{
				break;
			}
			float num = (float)(_turnTimeStopWatch.Elapsed.TotalMilliseconds - (double)currentMilliSec) / 1000f;
			currentMilliSec = (float)_turnTimeStopWatch.Elapsed.TotalMilliseconds;
			float num2 = currentMilliSec / 1000f;
			if (_isRunTurnEndLimitTimer)
			{
				_elapsedTurnTimeAfterTurnStartVfx = num2 - _turnStartVfxFinishTime;
			}
			if (battleMgr.VfxMgr.IsEnd)
			{
				if (_enabledThinkingCounter)
				{
					_elapsedThinkingTime += num;
				}
				if (base.IsThinkingInterval)
				{
					_currentThinkingIntervalTime -= num;
				}
				if (!_isRunTurnEndLimitTimer)
				{
					_turnStartVfxFinishTime = num2;
					_isRunTurnEndLimitTimer = true;
				}
			}
			if (!base.IsRunWeakLogic)
			{
				currentTimeOverLogicSec += num;
				_timeOverLogicSec = (int)currentTimeOverLogicSec;
				if (_timeOverLogicSec > 7 && battleMgr.VfxMgr.IsEnd && AIOperationQueue.Count <= 0)
				{
					ChangeWeakLogic();
					currentTimeOverLogicSec = 0f;
				}
			}
		}
	}

	public override void Retire()
	{
		EnemyAICoroutine.GetInstance().StopAllCoroutines();
		BattleCoroutine instance = BattleCoroutine.GetInstance();
		if (weakLogicCoroutine != null)
		{
			instance.StopCoroutine(weakLogicCoroutine);
			weakLogicCoroutine = null;
		}
		battleMgr.VfxMgr.RegisterImmediateVfx(new CanNotTouchCardVfx());
		if (!battleMgr.GetBattlePlayer(isPlayer: true).Class.IsDead)
		{
			battleMgr.VfxMgr.RegisterSequentialVfx(battleMgr.DeadClass(PlayerDead: false, BattleManagerBase.FINISH_TYPE.RETIRE));
			battleMgr.VfxMgr.RegisterSequentialVfx(InstantVfx.Create(delegate
			{
				battleMgr.InitiateGameEndSequence(hasWon: true);
			}));
		}
	}

	public override void Disconnect()
	{
		_isConnectNetwork = false;
	}

	public override void Reconnect()
	{
		_isConnectNetwork = true;
	}

	public override void CleanupStackedAction()
	{
		base.IsStopThinkingLogic = true;
		ExecuteActionOperationQueue();
	}

	public override void RegistActionOperationQueue(Action action)
	{
		base.RegistActionOperationQueue(action);
		CheckIsStackAction();
	}

	public override void ExecuteActionOperationQueue()
	{
		base.ExecuteActionOperationQueue();
		CheckIsStackAction();
	}

	protected override void OnBeforeTurnEnd()
	{
		if (weakLogicCoroutine != null)
		{
			BattleCoroutine.GetInstance().StopCoroutine(weakLogicCoroutine);
			weakLogicCoroutine = null;
		}
	}

	protected override void OnFinishOprAttack()
	{
		CheckIsStackAction();
	}

	protected override void OnFinishOprTargetSelect()
	{
		SetupThinkingInterval();
		CheckIsStackAction();
	}

	protected override void CheckIsStackAction()
	{
		_isStackAction = AIOperationQueue.Count > 0;
	}

	protected override void SetupThinkingInterval()
	{
		_enabledThinkingCounter = false;
		float num = UnityEngine.Random.Range(0f, 3f);
		num = ((num <= _elapsedThinkingTime) ? 0f : num);
		_currentThinkingIntervalTime = num;
	}
}
