using System;
using UnityEngine;
using Wizard;

public class BattleUIContainer : MonoBehaviour
{
	[SerializeField]
	public BattleButtonControl ButtonControl;

	[SerializeField]
	private WizardUIButton BattleMenuBtn;

	[SerializeField]
	private UIButton TurnEndBtn;

	[SerializeField]
	public Transform EnemyChoiceBraveBtn;

	[SerializeField]
	private GameObject _battery;

	private const float LONG_PRESS_TIME = 0.2f;

	private float? _predictionWaitTime;

	public Action<bool> ShowPrediction;

	private bool _enableMenuRequest;

	private InputMgr _inputMgr;

	public bool PlayerCardPlaying;

	private bool _forceDisableMenu;

	public GameObject Battery => _battery;

	private void Start()
	{
		BattleMenuBtn.onPress.Clear();
		BattleMenuBtn.onPress.Add(new EventDelegate(delegate
		{
			ButtonControl.OnPressMenuBtn();
		}));
		if (!GameMgr.GetIns().IsWatchBattle)
		{
			TurnEndBtn.onClick.Clear();
			TurnEndBtn.onClick.Add(new EventDelegate(delegate
			{
				ButtonControl.OnPressTurnEnd();
			}));
		}
		_predictionWaitTime = null;
		UIEventListener.Get(TurnEndBtn.gameObject).onPress = delegate(GameObject obj, bool isPress)
		{
			if (isPress)
			{
				_predictionWaitTime = Time.realtimeSinceStartup;
			}
			else
			{
				_predictionWaitTime = null;
				if (ShowPrediction != null)
				{
					ShowPrediction(obj: false);
				}
			}
		};
		UIEventListener.Get(TurnEndBtn.gameObject).onHover = delegate(GameObject obj, bool isHover)
		{
			if (BattleManagerBase.UseCustomMouse && UIManager.GetInstance().ApplicationHasFocus)
			{
				if (isHover)
				{
					_predictionWaitTime = Time.realtimeSinceStartup;
				}
				else
				{
					_predictionWaitTime = null;
					if (ShowPrediction != null)
					{
						ShowPrediction(isHover);
					}
				}
			}
		};
		UIEventListener.Get(TurnEndBtn.gameObject).onDragOut = delegate
		{
			_predictionWaitTime = null;
			if (ShowPrediction != null)
			{
				ShowPrediction(obj: false);
			}
		};
		_inputMgr = GameMgr.GetIns().GetInputMgr();
	}

	public void EnableMenu()
	{
		if (!PlayerCardPlaying && !_forceDisableMenu)
		{
			BattleMenuBtn.isEnabled = true;
			SetEnableReset(isEnable: true);
			SetEnableHint(isEnable: true);
		}
	}

	public void RequestEnableMenuWhenTouchable()
	{
		_enableMenuRequest = true;
	}

	private void TryEnableMenu()
	{
		if (BattleManagerBase.GetIns().GetBattlePlayer(isPlayer: true).BattleView.IsTouchable())
		{
			EnableMenu();
			_enableMenuRequest = false;
		}
	}

	public void ForceEnableMenu()
	{
		_forceDisableMenu = false;
		EnableMenu();
	}

	public void DisableMenu(bool isForceDisable = false)
	{
		if (isForceDisable || (!BattleManagerBase.GetIns().IsRecovery && !GameMgr.GetIns().IsReplayBattle && !GameMgr.GetIns().IsWatchBattle))
		{
			BattleMenuBtn.isEnabled = false;
			SetEnableReset(isEnable: false);
			SetEnableHint(isEnable: false);
		}
	}

	public void ForceDisableMenu()
	{
		_forceDisableMenu = true;
		_enableMenuRequest = false;
		DisableMenu();
	}

	public void SetEnableReset(bool isEnable)
	{
		BattleManagerBase ins = BattleManagerBase.GetIns();
		if (ins is PuzzleBattleManager)
		{
			(ins as PuzzleBattleManager).ResetButton.SetState((!isEnable) ? UIButtonColor.State.Disabled : UIButtonColor.State.Normal, immediate: false);
		}
	}

	public void SetEnableHint(bool isEnable)
	{
		BattleManagerBase ins = BattleManagerBase.GetIns();
		if (ins is PuzzleBattleManager)
		{
			(ins as PuzzleBattleManager).HintButton.isEnabled = isEnable;
		}
	}

	public bool IsEnableMenu()
	{
		return BattleMenuBtn.isEnabled;
	}

	private void Update()
	{
		UIManager instance = UIManager.GetInstance();
		GameMgr ins = GameMgr.GetIns();
		BattleManagerBase ins2 = BattleManagerBase.GetIns();
		if (_enableMenuRequest)
		{
			if (BattleMenuBtn.isEnabled)
			{
				_enableMenuRequest = false;
			}
			else
			{
				TryEnableMenu();
			}
		}
		if (!Input.GetMouseButton(0) && instance != null && instance.IsCurrentScene(UIManager.ViewScene.Battle) && !instance.isOpenLoading() && !instance.isFading() && ins.GetInputMgr().isBackKeyEnable && !BattleManagerBase.IsTutorial)
		{
			if (BattleMenuBtn.gameObject.activeInHierarchy && BattleMenuBtn.isActiveAndEnabled && !instance.isOpenDialog())
			{
				if (Input.GetKeyDown(KeyCode.Escape) && IsEnableMenu())
				{
					ButtonControl.OnPressMenuBtn();
				}
				if (BattleManagerBase.UseKeyboardTurnEndSpaceShortcut && _inputMgr.IsKeyboardSpace() && ins2.BattlePlayer.PlayerBattleView.TurnEndButtonUI.GameObject.activeInHierarchy && ins2.BattlePlayer.PlayerBattleView.TurnEndButtonUI.GetEnableLabel && ins2.BattlePlayer.IsSelfTurn && _inputMgr.KeyboardEnableControlSpace)
				{
					ButtonControl.OnPressTurnEnd();
				}
			}
			else if (instance.isOpenDialog() && !ButtonControl.IsSettingMenuOpen && !ButtonControl.IsRetireMenuOpen && !ButtonControl.IsQuestMissionOpen && Input.GetKeyDown(KeyCode.Escape))
			{
				ins2.BattlePlayer.PlayerBattleView.IsMenuCloseEscape = true;
				ButtonControl.HideBattleMenu();
			}
		}
		if (TurnEndBtn.state == UIButtonColor.State.Pressed || BattleManagerBase.UseCustomMouse)
		{
			if (ShowPrediction != null && _predictionWaitTime.HasValue && _predictionWaitTime.Value + 0.2f < Time.realtimeSinceStartup)
			{
				ShowPrediction(obj: true);
				_predictionWaitTime = null;
			}
		}
		else
		{
			_predictionWaitTime = null;
		}
		if (ins.IsWatchBattle && ins2.IsBattleEnd)
		{
			ButtonControl.HideAllMenu(isWithoutSE: true);
		}
	}
}
