using System;
using System.Collections.Generic;
using System.Linq;
using Cute;
using UnityEngine;
using Wizard.RoomMatch;

namespace Wizard;

public class GatheringCreateDialog : MonoBehaviour
{
	[SerializeField]
	private UIButton _typeChangeButton;

	[SerializeField]
	private UILabel _typeLabel;

	[SerializeField]
	private UIButton _battleStyleChangeButton;

	[SerializeField]
	private UILabel _battleStyleLabel;

	[SerializeField]
	private UIButton _maxMemberChangeButton;

	[SerializeField]
	private UILabel _maxMemberLabel;

	[SerializeField]
	private UIButton _formatChangeButton;

	[SerializeField]
	private UILabel _formatLabel;

	[SerializeField]
	private UIButton _watchChangeButton;

	[SerializeField]
	private UILabel _watchLabel;

	[SerializeField]
	private UIButton _battleHourChangeButton;

	[SerializeField]
	private UILabel _battleHourLabel;

	[SerializeField]
	private UIButton _battleStartTimeChangeButton;

	[SerializeField]
	private UILabel _battleStartTimeLabel;

	[SerializeField]
	private UIToggle _isEntryDeckOnlyToggle;

	[SerializeField]
	private GameObject _isEntryDeckOnlyObjectRoot;

	[SerializeField]
	private GameObject _timeLimitObjectRoot;

	[SerializeField]
	private UIGrid _ruleCategoryGrid;

	[SerializeField]
	private GameObject _startTimeSelectPrefab;

	[SerializeField]
	private UIToggle _ownerEntryBattleToggle;

	public Action<GatheringRule> OnDecideRule;

	private DialogBase _dialog;

	private GatheringRule _rule = new GatheringRule();

	private bool _ownerEntryBattleToggleFirstCall = true;

	private bool _isEntryDeckOnlyToggleFirstCall = true;

	private List<Format> _formatList = new List<Format>();

	private Format[] ALL_FORMAT = new Format[2]
	{
		Format.Rotation,
		Format.Unlimited
	};

	private int[] BATTLE_HOUR_SETTING = new int[8] { 1, 2, 3, 4, 8, 12, 24, 48 };

	private int[] BATTLE_START_MINUTE_SETTING = new int[8] { 5, 15, 30, 60, 120, 240, 480, 1440 };

	private int[] MAX_MEMBER_SETTING = new int[7] { 2, 4, 8, 16, 32, 64, 128 };

	private static List<RoomConnectController.BattleRule> GetRuleListNormal(Format format)
	{
		List<RoomConnectController.BattleRule> list = new List<RoomConnectController.BattleRule>();
		list.Add(RoomConnectController.BattleRule.Bo1);
		if (format != Format.Avatar)
		{
			list.Add(RoomConnectController.BattleRule.Bo3);
			list.Add(RoomConnectController.BattleRule.Bo5);
			list.Add(RoomConnectController.BattleRule.Bo3Ban);
			list.Add(RoomConnectController.BattleRule.Bo5Ban);
		}
		return list;
	}

	public static GatheringCreateDialog Create(GameObject prefab)
	{
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		GatheringCreateDialog component = UnityEngine.Object.Instantiate(prefab).GetComponent<GatheringCreateDialog>();
		component._dialog = dialogBase;
		SystemText systemText = Data.SystemText;
		dialogBase.SetObj(component.gameObject);
		dialogBase.SetSize(DialogBase.Size.M);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetButtonText(systemText.Get("Gathering_0020"));
		dialogBase.SetTitleLabel(systemText.Get("Gathering_0006"));
		component._rule.LoadFromPlayerPrefs();
		component._rule.BattleStartLocalTime = DefaultStartTime();
		component.Refresh();
		return component;
	}

	private static DateTime DefaultStartTime()
	{
		DateTime nowTime = PlayerStaticData.UserTime.GetNowTime();
		int num = 15;
		int minute = nowTime.Minute;
		int num2 = minute / num * num;
		nowTime = nowTime.AddMinutes(num2 - minute);
		TimeSpan timeSpan = nowTime - PlayerStaticData.UserTime.GetNowTime();
		while (timeSpan.TotalMinutes < 16.0)
		{
			nowTime = nowTime.AddMinutes(15.0);
			timeSpan = nowTime - PlayerStaticData.UserTime.GetNowTime();
		}
		return nowTime;
	}

	private void Start()
	{
		_typeChangeButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickGatheringTypeChangeButton();
		}));
		_maxMemberChangeButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickMaxMemberChangeButton();
		}));
		_battleStyleChangeButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickBattleStyleChangeButton();
		}));
		_formatChangeButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickFormatChangeButton();
		}));
		_ownerEntryBattleToggle.onChange.Add(new EventDelegate(delegate
		{
			OnClickOwnerEntryBattle();
		}));
		_watchChangeButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickWatchChangeButton();
		}));
		_battleHourChangeButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickBattleHourChangeButton();
		}));
		_battleStartTimeChangeButton.onClick.Add(new EventDelegate(delegate
		{
			OnClickBattleStartHourChangeButton();
		}));
		_isEntryDeckOnlyToggle.onChange.Add(new EventDelegate(delegate
		{
			OnClickIsEntryDeckOnlyButton();
		}));
		_dialog.onPushButton1 = delegate
		{
			CreateGathering();
		};
	}

	private void OnClickGatheringTypeChangeButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		List<GatheringUtility.RuleTypeInfo> allGatheringRule = GatheringUtility.GetAllGatheringRule();
		GatheringUtility.RuleTypeInfo item = allGatheringRule.FirstOrDefault((GatheringUtility.RuleTypeInfo x) => x.Type == _rule.Type && x.TournamentType == _rule.TournamentType && x.IsReset == _rule.IsReset);
		List<string> textList = new List<string>();
		allGatheringRule.ForEach(delegate(GatheringUtility.RuleTypeInfo rule)
		{
			textList.Add(rule.Name);
		});
		DialogBase drumDialog = DrumrollDialog.Create(textList, allGatheringRule.IndexOf(item), null, null, OnDecideGatheringType, Data.SystemText.Get("Gathering_0036"));
		OnDrumUICommonSetting(drumDialog);
	}

	private void OnDecideGatheringType(int index)
	{
		GatheringUtility.RuleTypeInfo ruleTypeInfo = GatheringUtility.GetAllGatheringRule()[index];
		_rule.Type = ruleTypeInfo.Type;
		_rule.TournamentType = ruleTypeInfo.TournamentType;
		_rule.IsReset = ruleTypeInfo.IsReset;
		_rule.SaveToPlayerPrefs();
		if (ruleTypeInfo.MaxMember < _rule.MaxMember)
		{
			int num = MAX_MEMBER_SETTING.ToList().IndexOf(ruleTypeInfo.MaxMember);
			if (num == -1)
			{
				Debug.LogError("選択肢に含まれていない最大人数です");
				OnSelectMember(0);
			}
			else
			{
				OnSelectMember(num);
			}
		}
	}

	private void OnClickBattleStyleChangeButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		int defaultIndex = 0;
		List<string> list = new List<string>();
		List<RoomConnectController.BattleRule> ruleListNormal = GetRuleListNormal(_rule.BattleParameterInstance.DeckFormat);
		for (int i = 0; i < ruleListNormal.Count; i++)
		{
			if (_rule.BattleParameterInstance.Rule == ruleListNormal[i])
			{
				defaultIndex = i;
			}
			list.Add(RoomRuleSetting.GetWinTypeString(ruleListNormal[i]));
		}
		DialogBase drumDialog = DrumrollDialog.Create(list, defaultIndex, null, null, OnDecideBattleStyle, Data.SystemText.Get("Gathering_0037"));
		OnDrumUICommonSetting(drumDialog);
	}

	private void OnDecideBattleStyle(int index)
	{
		_rule.BattleParameterInstance.Rule = GetRuleListNormal(_rule.BattleParameterInstance.DeckFormat)[index];
		_rule.SaveToPlayerPrefs();
	}

	private void OnClickFormatChangeButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		int defaultIndex = 0;
		List<string> list = new List<string>();
		List<Format> maintenanceFormatList = new List<Format>();
		DialogBase dialog = null;
		_formatList.Clear();
		for (int i = 0; i < ALL_FORMAT.Length; i++)
		{
			if (_rule.BattleParameterInstance.DeckFormat == ALL_FORMAT[i])
			{
				defaultIndex = i;
			}
			list.Add(FormatBehaviorManager.GetFormatName(ALL_FORMAT[i]));
			_formatList.Add(ALL_FORMAT[i]);
		}
		if (Prerelease.Status == Prerelease.eStatus.PRE_ROTATION)
		{
			if (_rule.BattleParameterInstance.DeckFormat == Format.PreRotation)
			{
				defaultIndex = _formatList.Count;
			}
			_formatList.Add(Format.PreRotation);
			list.Add(FormatBehaviorManager.GetFormatName(Format.PreRotation));
		}
		if (Data.Crossover.IsWithinGatheringPeriod)
		{
			if (_rule.BattleParameterInstance.DeckFormat == Format.Crossover)
			{
				defaultIndex = _formatList.Count;
			}
			_formatList.Add(Format.Crossover);
			string text = FormatBehaviorManager.GetFormatName(Format.Crossover);
			if (Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.GATHERING_CROSSOVER))
			{
				maintenanceFormatList.Add(Format.Crossover);
				text += Data.SystemText.Get("RoomBattle_0099");
			}
			list.Add(text);
		}
		if (Data.MyRotationAllInfo.IsWithinGatheringPeriod)
		{
			if (_rule.BattleParameterInstance.DeckFormat == Format.MyRotation)
			{
				defaultIndex = _formatList.Count;
			}
			_formatList.Add(Format.MyRotation);
			string text2 = FormatBehaviorManager.GetFormatName(Format.MyRotation);
			if (Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.GATHERING_MYROTATION))
			{
				maintenanceFormatList.Add(Format.MyRotation);
				text2 += Data.SystemText.Get("RoomBattle_0099");
			}
			list.Add(text2);
		}
		if (Data.AvatarBattleAllInfo.IsWithinGatheringPeriod)
		{
			if (_rule.BattleParameterInstance.DeckFormat == Format.Avatar)
			{
				defaultIndex = _formatList.Count;
			}
			_formatList.Add(Format.Avatar);
			string text3 = FormatBehaviorManager.GetFormatName(Format.Avatar);
			if (Data.MaintenanceCodeList.Contains(NetworkDefine.MAINTENANCE_TYPE.GATHERING_AVATAR))
			{
				maintenanceFormatList.Add(Format.Avatar);
				text3 += Data.SystemText.Get("RoomBattle_0099");
			}
			list.Add(text3);
		}
		Action<int> selectCallback = delegate(int index)
		{
			UIManager.SetObjectToGrey(dialog.button1.gameObject, maintenanceFormatList.Contains(_formatList[index]));
			if (_formatList[index] == Format.Avatar)
			{
				_rule.BattleParameterInstance.Rule = RoomConnectController.BattleRule.Bo1;
			}
		};
		dialog = DrumrollDialog.Create(list, defaultIndex, selectCallback, null, OnDecideFormat, Data.SystemText.Get("Gathering_0038"));
		OnDrumUICommonSetting(dialog);
	}

	private void OnDecideFormat(int index)
	{
		_rule.BattleParameterInstance.DeckFormat = _formatList[index];
		_rule.SaveToPlayerPrefs();
	}

	private void OnClickMaxMemberChangeButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		int defaultIndex = 0;
		List<string> list = new List<string>();
		for (int i = 0; i < MAX_MEMBER_SETTING.Length; i++)
		{
			if (_rule.MaxMember == MAX_MEMBER_SETTING[i])
			{
				defaultIndex = i;
			}
			if (GatheringUtility.GetMaxMemberNumber(_rule.Type) < MAX_MEMBER_SETTING[i])
			{
				break;
			}
			list.Add(Data.SystemText.Get("Gathering_0008", MAX_MEMBER_SETTING[i].ToString()));
		}
		DialogBase drumDialog = DrumrollDialog.Create(list, defaultIndex, null, null, OnSelectMember, Data.SystemText.Get("Gathering_0039"));
		OnDrumUICommonSetting(drumDialog);
	}

	private void OnSelectMember(int index)
	{
		_rule.MaxMember = MAX_MEMBER_SETTING[index];
		_rule.SaveToPlayerPrefs();
	}

	private void OnDrumUICommonSetting(DialogBase drumDialog)
	{
		_dialog.SetDisp(inDisp: false);
		drumDialog.ResetBackViewAlpha();
		drumDialog.OnCloseStart = delegate
		{
			drumDialog.InactiveBackView();
			_dialog.ReOpen(isResetBackViewAlpha: true);
			Refresh();
		};
	}

	private void OnClickOwnerEntryBattle()
	{
		_rule.IsOwnerEntryBattle = _ownerEntryBattleToggle.value;
		if (!_ownerEntryBattleToggleFirstCall)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(_ownerEntryBattleToggle.value ? Se.TYPE.SYS_TOGGLE_ON : Se.TYPE.SYS_TOGGLE_OFF);
			_rule.SaveToPlayerPrefs();
		}
		_ownerEntryBattleToggleFirstCall = false;
	}

	private void OnClickIsEntryDeckOnlyButton()
	{
		_rule.IsEntryDeckOnly = _isEntryDeckOnlyToggle.value;
		if (!_isEntryDeckOnlyToggleFirstCall)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(_isEntryDeckOnlyToggle.value ? Se.TYPE.SYS_TOGGLE_ON : Se.TYPE.SYS_TOGGLE_OFF);
			_rule.SaveToPlayerPrefs();
		}
		_isEntryDeckOnlyToggleFirstCall = false;
	}

	private void OnClickWatchChangeButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		int defaultIndex = (int)(_rule.WatchSetting - 1);
		List<string> list = new List<string>();
		SystemText systemText = Data.SystemText;
		list.Add(systemText.Get("Gathering_0015"));
		list.Add(systemText.Get("Gathering_0016"));
		list.Add(systemText.Get("Gathering_0017"));
		DialogBase drumDialog = DrumrollDialog.Create(list, defaultIndex, null, null, OnDecideWatchSetting, Data.SystemText.Get("Gathering_0042"));
		OnDrumUICommonSetting(drumDialog);
	}

	private void OnDecideWatchSetting(int index)
	{
		_rule.WatchSetting = (GatheringRule.eWatchSetting)(index + 1);
		_rule.SaveToPlayerPrefs();
	}

	private void OnClickBattleHourChangeButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		List<string> list = new List<string>();
		int defaultIndex = 0;
		int num = 0;
		int[] bATTLE_HOUR_SETTING = BATTLE_HOUR_SETTING;
		for (int i = 0; i < bATTLE_HOUR_SETTING.Length; i++)
		{
			int num2 = bATTLE_HOUR_SETTING[i];
			list.Add(Data.SystemText.Get("Gathering_0013", num2.ToString()));
			if (num2 == _rule.BattleTimeHour)
			{
				defaultIndex = num;
			}
			num++;
		}
		DialogBase drumDialog = DrumrollDialog.Create(list, defaultIndex, null, null, OnDecideBattleHour, Data.SystemText.Get("Gathering_0041"));
		OnDrumUICommonSetting(drumDialog);
	}

	private void OnDecideBattleHour(int index)
	{
		_rule.BattleTimeHour = BATTLE_HOUR_SETTING[index];
		_rule.SaveToPlayerPrefs();
	}

	private void OnClickBattleStartHourChangeButton()
	{
		GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_BTN_DECIDE);
		DialogBase drumDialog = GatheringStartTimeSelectDialog.Create(_startTimeSelectPrefab, OnChangeBattleStartTime, _rule.BattleStartLocalTime);
		OnDrumUICommonSetting(drumDialog);
	}

	private void OnChangeBattleStartTime(DateTime time)
	{
		_rule.BattleStartLocalTime = time;
		Refresh();
	}

	private void Refresh()
	{
		SystemText systemText = Data.SystemText;
		_typeLabel.text = _rule.GetGatheringTypeString();
		_maxMemberLabel.text = systemText.Get("Gathering_0008", _rule.MaxMember.ToString());
		_battleStyleLabel.text = RoomRuleSetting.GetWinTypeString(_rule.BattleParameterInstance.Rule);
		_formatLabel.text = FormatBehaviorManager.GetFormatName(_rule.BattleParameterInstance.DeckFormat);
		_ownerEntryBattleToggle.value = _rule.IsOwnerEntryBattle;
		_battleHourLabel.text = systemText.Get("Gathering_0013", _rule.BattleTimeHour.ToString());
		_isEntryDeckOnlyToggle.value = _rule.IsEntryDeckOnly;
		_battleStartTimeLabel.text = ConvertTime.ToLocal(_rule.BattleStartUtcTime);
		_isEntryDeckOnlyObjectRoot.SetActive(!_rule.IsTournament);
		_timeLimitObjectRoot.SetActive(!_rule.IsTournament);
		UIManager.SetObjectToGrey(_battleStyleChangeButton.gameObject, IsOnlyOneWinType());
		_ruleCategoryGrid.Reposition();
		switch (_rule.WatchSetting)
		{
		case GatheringRule.eWatchSetting.ALL_MEMBER:
			_watchLabel.text = systemText.Get("Gathering_0016");
			break;
		case GatheringRule.eWatchSetting.NONE:
			_watchLabel.text = systemText.Get("Gathering_0017");
			break;
		case GatheringRule.eWatchSetting.OWNER_ONLY:
			_watchLabel.text = systemText.Get("Gathering_0015");
			break;
		}
	}

	private void CreateGathering()
	{
		if (_rule.IsTournament)
		{
			_rule.IsEntryDeckOnly = true;
		}
		OnDecideRule.Call(_rule);
	}

	private bool IsOnlyOneWinType()
	{
		return GetRuleListNormal(_rule.BattleParameterInstance.DeckFormat).Count == 1;
	}
}
