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

namespace Wizard;

public class DeckSelectUIDialog
{
	public enum eFormatChangeUIType
	{
		SingleFormat,
		NormalFormatOnly,
		WithPrerotation,
		WithCrossover,
		WithMyRotation,
		WithAvatar,
		UseOtherCategory
	}

	private const string PATH_DECK_LIST_PREFAB = "UI/DeckList/DeckSelectUI/DeckSelectUI";

	private static readonly Vector2 FORMAT_CHANGE_UI_POSITION = new Vector3(-508f, 65f);

	private DeckSelectUIDialogTitle _deckSelectDialogTitleUI;

	private DeckGroupListData _deckGroupListData;

	private bool _isVisibleCreateNew;

	private DeckSelectUI _deckSelectUI;

	private static readonly Format[] OTHER_CATEGORY_FORMATS = new Format[5]
	{
		Format.Crossover,
		Format.PreRotation,
		Format.MyRotation,
		Format.Avatar,
		Format.Max
	};

	private static readonly Dictionary<FormatChangeUI.FormatCategory, Format> FORMAT_CATEGORY_TO_FORMAT = new Dictionary<FormatChangeUI.FormatCategory, Format>
	{
		{
			FormatChangeUI.FormatCategory.Rotation,
			Format.Rotation
		},
		{
			FormatChangeUI.FormatCategory.Unlimited,
			Format.Unlimited
		},
		{
			FormatChangeUI.FormatCategory.PreRotation,
			Format.PreRotation
		},
		{
			FormatChangeUI.FormatCategory.Crossover,
			Format.Crossover
		},
		{
			FormatChangeUI.FormatCategory.Other,
			Format.Max
		},
		{
			FormatChangeUI.FormatCategory.Hof,
			Format.Hof
		},
		{
			FormatChangeUI.FormatCategory.Windfall,
			Format.Windfall
		},
		{
			FormatChangeUI.FormatCategory.MyRotation,
			Format.MyRotation
		},
		{
			FormatChangeUI.FormatCategory.Avatar,
			Format.Avatar
		}
	};

	public DialogBase Dialog { get; private set; }

	public static DeckSelectUIDialog Create(string dialogTitle, DeckGroupListData deckGroupListData, Format defaultFormat, eFormatChangeUIType formatChangeUIType, bool isVisibleCreateNew, Action<DialogBase, DeckData> OnSelectDeck, DeckSelectUI.InitOptions initOptions = null)
	{
		if (defaultFormat == Format.All)
		{
			defaultFormat = (Format)PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.LAST_SELECT_DECK_FORMAT);
		}
		if (formatChangeUIType == eFormatChangeUIType.UseOtherCategory && IsIncludeInOtherCategory(defaultFormat))
		{
			defaultFormat = Format.Max;
			bool num = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.LAST_BATTLE_IS_TRIALDECK);
			bool flag = PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.LAST_BATTLE_IS_DEFDECK);
			if (!num && !flag)
			{
				Format[] oTHER_CATEGORY_FORMATS = OTHER_CATEGORY_FORMATS;
				foreach (Format format in oTHER_CATEGORY_FORMATS)
				{
					if (deckGroupListData.IsExistDeckListByFormat(format))
					{
						defaultFormat = format;
						break;
					}
				}
			}
		}
		if (formatChangeUIType != eFormatChangeUIType.SingleFormat && !deckGroupListData.IsExistDeckListByFormat(defaultFormat))
		{
			defaultFormat = deckGroupListData.DeckGroupList.FirstOrDefault((DeckGroup deckGroup) => deckGroup.DeckDataList.Any((DeckData deck) => !deck.IsNoCard()))?.DeckFormat ?? Format.Rotation;
		}
		FormatChangeUI.FormatCategory formatCategoryByFormat = GetFormatCategoryByFormat(defaultFormat, formatChangeUIType == eFormatChangeUIType.UseOtherCategory);
		DeckSelectUIDialog deckSelectUIDialog = new DeckSelectUIDialog(dialogTitle, deckGroupListData, defaultFormat, formatCategoryByFormat, formatChangeUIType, isVisibleCreateNew, OnSelectDeck, initOptions);
		deckSelectUIDialog.CreateFormatChangeUI(formatCategoryByFormat, formatChangeUIType);
		return deckSelectUIDialog;
	}

	private DeckSelectUIDialog(string dialogTitle, DeckGroupListData deckGroupListData, Format format, FormatChangeUI.FormatCategory formatCategory, eFormatChangeUIType formatChangeUIType, bool isVisibleCreateNew, Action<DialogBase, DeckData> OnSelectDeck, DeckSelectUI.InitOptions initOptions)
	{
		DeckSelectUIDialog deckSelectUIDialog = this;
		_isVisibleCreateNew = isVisibleCreateNew;
		_deckGroupListData = deckGroupListData;
		Dialog = UIManager.GetInstance().CreateDialogClose();
		Dialog.SetTitleLabel(dialogTitle);
		Dialog.SetSize(DialogBase.Size.XL);
		Dialog.SetButtonLayout(DialogBase.ButtonLayout.NONE);
		Dialog.SetActive(inActive: false);
		int? longTextPositionOverride = initOptions?.LongTextTitlePosition;
		_deckSelectDialogTitleUI = DeckSelectUIDialogTitle.Create(Dialog, dialogTitle, format, formatChangeUIType != eFormatChangeUIType.SingleFormat, longTextPositionOverride);
		List<DeckGroup> deckGroupList = GetDeckGroupList(formatCategory);
		GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load("UI/DeckList/DeckSelectUI/DeckSelectUI")) as GameObject;
		_deckSelectUI = gameObject.GetComponent<DeckSelectUI>();
		_deckSelectUI.gameObject.SetActive(value: false);
		_deckSelectUI.Initialize(deckGroupList, format, isVisibleCreateNew, delegate(DeckData deck)
		{
			OnSelectDeck.Call(deckSelectUIDialog.Dialog, deck);
		}, delegate(Format changeFormat)
		{
			deckSelectUIDialog._deckSelectDialogTitleUI.UpdateFormatObj(changeFormat);
		}, delegate
		{
			deckSelectUIDialog.Dialog.SetActive(inActive: true);
			deckSelectUIDialog._deckSelectUI.gameObject.SetActive(value: true);
		}, initOptions);
		Dialog.SetObj(_deckSelectUI.gameObject);
		_deckSelectUI.gameObject.GetComponent<UIPanel>().alpha = 1f;
	}

	public void Close()
	{
		if (Dialog != null)
		{
			Dialog.Close();
		}
	}

	public static string GetTitleByBattleType(DataMgr.BattleType battleType)
	{
		string result = string.Empty;
		switch (battleType)
		{
		case DataMgr.BattleType.FreeBattle:
			result = Data.SystemText.Get("Battle_0005");
			break;
		case DataMgr.BattleType.RankBattle:
			result = Data.SystemText.Get("Battle_0006");
			break;
		case DataMgr.BattleType.RoomBattle:
		case DataMgr.BattleType.RoomTwoPick:
			result = Data.SystemText.Get("Battle_0007");
			break;
		case DataMgr.BattleType.ColosseumNormal:
		case DataMgr.BattleType.ColosseumHof:
			result = Data.SystemText.Get("Battle_0488");
			break;
		}
		return result;
	}

	public void UpdateAllDeckUICurrentPage()
	{
		_deckSelectUI.UpdateAllDeckUICurrentPage();
	}

	public void ShowCautionText(string cautionText)
	{
		_deckSelectUI.ShowCautionText(cautionText);
	}

	public void SetPanelDepth(int depth)
	{
		Dialog.SetPanelDepth(depth);
		_deckSelectUI.GetComponent<UIPanel>().depth = ++depth;
	}

	private void CreateFormatChangeUI(FormatChangeUI.FormatCategory defaultFormatCategory, eFormatChangeUIType formatChangeUIType)
	{
		if (formatChangeUIType != eFormatChangeUIType.SingleFormat)
		{
			FormatChangeUI.FormatCategory anotherFormatCategory = FormatChangeUI.FormatCategory.Invalid;
			switch (formatChangeUIType)
			{
			case eFormatChangeUIType.NormalFormatOnly:
				anotherFormatCategory = FormatChangeUI.FormatCategory.Invalid;
				break;
			case eFormatChangeUIType.UseOtherCategory:
				anotherFormatCategory = FormatChangeUI.FormatCategory.Other;
				break;
			case eFormatChangeUIType.WithPrerotation:
				anotherFormatCategory = FormatChangeUI.FormatCategory.PreRotation;
				break;
			case eFormatChangeUIType.WithCrossover:
				anotherFormatCategory = FormatChangeUI.FormatCategory.Crossover;
				break;
			case eFormatChangeUIType.WithMyRotation:
				anotherFormatCategory = FormatChangeUI.FormatCategory.MyRotation;
				break;
			}
			FormatChangeUI formatChangeUI = FormatChangeUI.Create(defaultFormatCategory, anotherFormatCategory, OnClickChangeFormatBtn);
			Dialog.AttachObjToTitleLabel(formatChangeUI.gameObject, FORMAT_CHANGE_UI_POSITION);
		}
	}

	private void OnClickChangeFormatBtn(FormatChangeUI.FormatCategory formatCategory)
	{
		Format formatByFormatCategory = GetFormatByFormatCategory(formatCategory);
		_deckSelectUI.gameObject.SetActive(value: false);
		List<DeckGroup> deckGroupList = GetDeckGroupList(formatCategory);
		_deckSelectUI.UpdateDeckView(deckGroupList, formatByFormatCategory, _isVisibleCreateNew, delegate
		{
			_deckSelectUI.gameObject.SetActive(value: true);
		});
	}

	private List<DeckGroup> GetDeckGroupList(FormatChangeUI.FormatCategory formatCategory)
	{
		List<DeckGroup> list = new List<DeckGroup>();
		if (formatCategory == FormatChangeUI.FormatCategory.Other)
		{
			Format[] oTHER_CATEGORY_FORMATS = OTHER_CATEGORY_FORMATS;
			foreach (Format format in oTHER_CATEGORY_FORMATS)
			{
				list.AddRange(_deckGroupListData.SelectDeckGroupList(format));
			}
		}
		else
		{
			Format formatByFormatCategory = GetFormatByFormatCategory(formatCategory);
			list.AddRange(_deckGroupListData.SelectDeckGroupList(formatByFormatCategory));
		}
		return list;
	}

	private static Format GetFormatByFormatCategory(FormatChangeUI.FormatCategory formatCategory)
	{
		return FORMAT_CATEGORY_TO_FORMAT[formatCategory];
	}

	private static FormatChangeUI.FormatCategory GetFormatCategoryByFormat(Format format, bool useOtherCategory)
	{
		if (useOtherCategory && IsIncludeInOtherCategory(format))
		{
			return FormatChangeUI.FormatCategory.Other;
		}
		FormatChangeUI.FormatCategory result = FormatChangeUI.FormatCategory.Invalid;
		foreach (KeyValuePair<FormatChangeUI.FormatCategory, Format> item in FORMAT_CATEGORY_TO_FORMAT)
		{
			if (item.Value == format)
			{
				result = item.Key;
				break;
			}
		}
		return result;
	}

	private static bool IsIncludeInOtherCategory(Format format)
	{
		return OTHER_CATEGORY_FORMATS.Any((Format f) => f == format);
	}
}
