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

namespace Wizard;

public class BattleButtonControl : MonoBehaviour
{
	public bool _isBattleMenuOptionsButtonPressable = true;

	private bool _isBattleMenuOpen;

	private bool _isRetireMenuOpen;

	private bool _isSettingMenuOpen;

	private bool IsRetire;

	private BattleManagerBase _battleMgr;

	private IPlayerView _battlePlayerView;

	private BattleMenuMgr _battleMenu;

	private DialogBase _retireDialog;

	private DialogBase _settingDialog;

	private DialogBase _keyWordDialog;

	private DialogBase _questMissionDialog;

	private CanNotTouchCardVfx _canNotTouchCardVfx;

	public bool IsBattleMenuOpen => _isBattleMenuOpen;

	public bool IsRetireMenuOpen => _isRetireMenuOpen;

	public DialogBase RetireMenu => _retireDialog;

	public bool IsSettingMenuOpen => _isSettingMenuOpen;

	public DialogBase SettingMenu => _settingDialog;

	public bool IsQuestMissionOpen { get; private set; }

	public void Start()
	{
		_battleMgr = BattleManagerBase.GetIns();
		_battlePlayerView = _battleMgr.BattlePlayer.PlayerBattleView;
	}

	public void OnPressTurnEnd()
	{
		if (_battleMgr.BattlePlayer._isPlayerActive)
		{
			if (_battleMgr.TouchControl.HasTouchProcessor)
			{
				_battleMgr.Pause();
				_battleMgr.Resume();
			}
			else if (_battlePlayerView.IsShowTurnEndDialogOfNotAttackingOrPlaying || _battlePlayerView.IsShowTurnEndDialogOfNotUsingHeroSkill)
			{
				_battlePlayerView.ShowTurnEndDialog(base.gameObject);
			}
			else
			{
				TurnEndConfirm();
			}
		}
	}

	private void TurnEndConfirm()
	{
		_battleMgr.VfxMgr.RegisterSequentialVfx(_battleMgr.OperateMgr.PlayerTurnEnd());
	}

	public void OnPressKeyBtn(BattleCardBase card, GameObject labelObject, CardParameter baseParameter)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		_keyWordDialog = _battlePlayerView.CreateKeyPanel(card, labelObject.GetComponent<UILabel>(), CardMaster.BatttleCardMasterId, baseParameter);
		InitializeKeywordDialog(_keyWordDialog);
	}

	public void OnPressKeyBtn(string text, GameObject labelObject)
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		_keyWordDialog = BattlePlayerView.CreateKeyPanel(text, labelObject.GetComponent<UILabel>(), CardMaster.CardMasterId.Default);
		InitializeKeywordDialog(_keyWordDialog);
	}

	public void InitializeKeywordDialog(DialogBase dialog)
	{
		if (dialog != null)
		{
			bool isUpdateHandCardsPlayability = true;
			if (GameMgr.GetIns().IsWatchBattle)
			{
				isUpdateHandCardsPlayability = !(BattleManagerBase.GetIns() as NetworkBattleManagerBase).IsSkillSelectTiming;
			}
			CanNotTouchCardVfx canNotTouchVfx = new CanNotTouchCardVfx(isUpdateHandCardsPlayability);
			BattleManagerBase.GetIns().VfxMgr.RegisterImmediateVfx(canNotTouchVfx);
			dialog.OnClose = (Action)Delegate.Combine(dialog.OnClose, (Action)delegate
			{
				canNotTouchVfx.End();
			});
		}
	}

	public void OnPressBuffBtn()
	{
		_battlePlayerView.ShowKeyPanel(2);
	}

	public void KeyPanelCancel()
	{
		_battlePlayerView.HideKeyPanel();
	}

	public void OnPressMenuBtn()
	{
		if (!_battlePlayerView.IsMenuOpen && !_battleMgr.IsBattleEnd && !IsRetire)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
			ShowBattleMenu();
		}
	}

	public void ShowBattleMenu()
	{
		if (_canNotTouchCardVfx == null)
		{
			bool isUpdateHandCardsPlayability = true;
			if (GameMgr.GetIns().IsWatchBattle)
			{
				isUpdateHandCardsPlayability = !(BattleManagerBase.GetIns() as NetworkBattleManagerBase).IsSkillSelectTiming;
			}
			_canNotTouchCardVfx = new CanNotTouchCardVfx(isUpdateHandCardsPlayability);
			BattleManagerBase.GetIns().VfxMgr.RegisterImmediateVfx(_canNotTouchCardVfx);
		}
		_battlePlayerView.IsMenuOpen = true;
		_battlePlayerView.RearrangeHand();
		GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(Resources.Load("Prefab/UI/Menu/BattleMenu"), base.transform);
		gameObject.transform.localScale = Vector3.one;
		_battleMenu = gameObject.GetComponent<BattleMenuMgr>();
		_battleMenu.DisplayBattleMenu(Retire, Setting, delegate
		{
			_battlePlayerView.IsMenuOpen = false;
			if (_canNotTouchCardVfx != null)
			{
				_canNotTouchCardVfx.End();
				_canNotTouchCardVfx = null;
			}
			if (!_battlePlayerView.IsMenuCloseEscape)
			{
				_battlePlayerView.ResetTouchable();
			}
			_battlePlayerView.IsMenuCloseEscape = false;
			_battlePlayerView.RearrangeHand();
			if (_battleMgr.BattlePlayer.IsSelfTurn && !_battleMgr.BattlePlayer.IsTurnStartEffectNotFinished)
			{
				_battlePlayerView.ForceShowTurnEndButton();
			}
		}, ShowQuestMissionDialog);
		if (!_isBattleMenuOptionsButtonPressable)
		{
			_battleMenu.SetSettingButtonDisable();
		}
		_battlePlayerView.AddPopUpPanel(_battleMenu, BattlePlayerViewBase.BattleDialogItem.Menu);
	}

	public void HideBattleMenu(bool isWithoutSE = false)
	{
		if (_battleMenu != null)
		{
			if (!isWithoutSE)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_CANCEL);
			}
			_battleMenu.Close();
			_battleMenu = null;
		}
	}

	public void Retire()
	{
		if (_isRetireMenuOpen || _isSettingMenuOpen || IsQuestMissionOpen)
		{
			return;
		}
		_isRetireMenuOpen = true;
		DialogBase dialogBase = _battlePlayerView.ShowRetireConfirmPanel();
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			if (!_battleMgr.IsBattleEnd && !IsRetire)
			{
				ShowBattleMenu();
			}
			_isRetireMenuOpen = false;
		});
		dialogBase.SetReturnMsg(base.gameObject, "RetireConfirm");
		_retireDialog = dialogBase;
	}

	public void HideRetireMenu(bool isNotCloseEvent)
	{
		if (_retireDialog != null)
		{
			if (isNotCloseEvent)
			{
				_retireDialog.OnClose = null;
			}
			_retireDialog.Close();
			_retireDialog = null;
		}
	}

	public void RetireConfirm()
	{
		if (!BattleManagerBase.GetIns().BattlePlayer.Class.IsDead && !BattleManagerBase.GetIns().BattleEnemy.Class.IsDead)
		{
			IsRetire = true;
			if (!GameMgr.GetIns().IsWatchBattle)
			{
				BattleManagerBase.GetIns().MenuButtonObject.SetActive(value: false);
				BattleManagerBase.GetIns().PlayRetire();
			}
		}
	}

	public void Recording()
	{
	}

	public void Setting()
	{
		if (_isRetireMenuOpen || _isSettingMenuOpen || IsQuestMissionOpen)
		{
			return;
		}
		_isSettingMenuOpen = true;
		DialogBase dialogBase = _battlePlayerView.CreateBattleSetting();
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			if (!_battleMgr.IsBattleEnd && !IsRetire)
			{
				ShowBattleMenu();
			}
			_isSettingMenuOpen = false;
		});
		dialogBase.SetReturnMsg(base.gameObject, "");
		_settingDialog = dialogBase;
	}

	public void HideSettingMenu(bool isNotCloseEvent)
	{
		if (_settingDialog != null)
		{
			if (isNotCloseEvent)
			{
				_settingDialog.OnClose = null;
			}
			_settingDialog.Close();
			_settingDialog = null;
		}
	}

	public void HideKeyWordDialog(bool isNotCloseEvent)
	{
		if (_keyWordDialog != null)
		{
			if (isNotCloseEvent)
			{
				_keyWordDialog.OnClose = null;
			}
			_keyWordDialog.Close();
		}
	}

	private void ShowQuestMissionDialog()
	{
		if (_isRetireMenuOpen || _isSettingMenuOpen || IsQuestMissionOpen)
		{
			return;
		}
		IsQuestMissionOpen = true;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetTitleLabel(Data.SystemText.Get("Quest_0005"));
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.CloseBtn);
		dialogBase.OnClose = (Action)Delegate.Combine(dialogBase.OnClose, (Action)delegate
		{
			if (!_battleMgr.IsBattleEnd && !IsRetire)
			{
				ShowBattleMenu();
			}
			IsQuestMissionOpen = false;
		});
		QuestAllConfirmDialog component = UnityEngine.Object.Instantiate(_battleMenu.QuestMissionDialogPrefab).GetComponent<QuestAllConfirmDialog>();
		component.CreateQuestAllConfirmDialog();
		dialogBase.SetObj(component.gameObject);
	}

	private void HideQuestMissionDialog(bool isNotCloseEvent)
	{
		if (!(_questMissionDialog == null))
		{
			if (isNotCloseEvent)
			{
				_questMissionDialog.OnClose = null;
			}
			_questMissionDialog.Close();
		}
	}

	public void HideAllMenu(bool isWithoutSE = false)
	{
		HideBattleMenu(isWithoutSE);
		HideRetireMenu(isNotCloseEvent: true);
		HideSettingMenu(isNotCloseEvent: true);
		HideKeyWordDialog(isNotCloseEvent: true);
		HideQuestMissionDialog(isNotCloseEvent: true);
	}

	public void OnPressMulliganSubmit()
	{
		_battleMgr.MulliganSubmit();
	}
}
