using UnityEngine;
using Wizard.DeckCardEdit;
using Wizard.Dialog.Setting;

namespace Wizard;

public class DeckCopyDialog : MonoBehaviour
{
	[SerializeField]
	private UILabel _textLabel;

	[SerializeField]
	private ItemToggle _sleeveAndLeaderSkinRootItemToggle;

	[SerializeField]
	private ItemToggle _subClassOptionRootItemToggle;

	[SerializeField]
	private ItemToggle _foilPreferredItemToggle;

	[SerializeField]
	private ItemToggle _prizePreferredItemToggle;

	[SerializeField]
	private ClassInfoParts _selectClassInfo;

	[SerializeField]
	private CenteringUIWidget _centeringUIWidget;

	[SerializeField]
	private FlexibleGrid _flexibleGrid;

	private void Start()
	{
		if (_sleeveAndLeaderSkinRootItemToggle != null)
		{
			_sleeveAndLeaderSkinRootItemToggle.SetTitleLabel(Data.SystemText.Get("Card_0181"));
			_sleeveAndLeaderSkinRootItemToggle.SetValue(PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.IS_COPY_SLEEVE_AND_SKIN));
			_sleeveAndLeaderSkinRootItemToggle.AddChangeCallback(delegate
			{
				PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.IS_COPY_SLEEVE_AND_SKIN, _sleeveAndLeaderSkinRootItemToggle.GetValue());
			});
		}
		if (_subClassOptionRootItemToggle != null)
		{
			_subClassOptionRootItemToggle.SetTitleLabel("Card_0281");
			_subClassOptionRootItemToggle.SetValue(PlayerPrefsWrapper.GetBool(PlayerPrefsWrapper.IS_COPY_SUBCLASS_CARDS));
			_subClassOptionRootItemToggle.AddChangeCallback(delegate
			{
				PlayerPrefsWrapper.SetBool(PlayerPrefsWrapper.IS_COPY_SUBCLASS_CARDS, _subClassOptionRootItemToggle.GetValue());
			});
		}
		if (_foilPreferredItemToggle != null)
		{
			_foilPreferredItemToggle.SetValue(Data.Load.data._userConfig.IsFoilPreferred);
			_foilPreferredItemToggle.AddChangeCallback(delegate
			{
				DeckCardEditUI.SendConfigUpdateFoilPreferred(_foilPreferredItemToggle.GetValue());
			});
		}
		if (_prizePreferredItemToggle != null)
		{
			_prizePreferredItemToggle.SetValue(Data.Load.data._userConfig.IsPrizePreferred);
			_prizePreferredItemToggle.AddChangeCallback(delegate
			{
				DeckCardEditUI.SendConfigUpdatePrizePreferred(_prizePreferredItemToggle.GetValue());
			});
		}
	}

	private void SetText(string text)
	{
		_textLabel.text = text;
	}

	private void SetClassesIconAndName(DeckData scrDeck)
	{
		if (!(_selectClassInfo == null) && !(_flexibleGrid == null) && !(_centeringUIWidget == null))
		{
			int skinId = scrDeck.GetSkinId();
			_selectClassInfo.InitBySkinId(skinId);
			_selectClassInfo.SetSubClass((CardBasePrm.ClanType)scrDeck.GetDeckSubClassID());
			_flexibleGrid.Reposition();
			_centeringUIWidget.Reposition();
			StartCoroutine(_flexibleGrid.RepositionNextFrame());
		}
	}

	private static DialogBase CreateDialog(DeckCopyDialog dialogParts, DeckData srcDeck)
	{
		SystemText systemText = Data.SystemText;
		DialogBase dialogBase = UIManager.GetInstance().CreateDialogClose();
		dialogParts.SetText(systemText.Get("Card_0112", srcDeck.GetDeckName()));
		dialogBase.SetTitleLabel(systemText.Get("Card_0121"));
		dialogBase.SetObj(dialogParts.gameObject);
		dialogBase.SetButtonLayout(DialogBase.ButtonLayout.BlueBtn_CancelBtn);
		dialogBase.SetButtonText(systemText.Get("Card_0122"));
		return dialogBase;
	}

	public static DialogBase CreateDeckCopyDialog(DeckCopyDialog deckCopyDialogPrefab, DeckData srcDeck)
	{
		return CreateDialog(Object.Instantiate(deckCopyDialogPrefab.gameObject).GetComponent<DeckCopyDialog>(), srcDeck);
	}

	public static DialogBase CreateDeckCopyDialogForMyRotation(DeckCopyDialog deckCopyDialogPrefab, DeckData srcDeck)
	{
		DeckCopyDialog component = Object.Instantiate(deckCopyDialogPrefab.gameObject).GetComponent<DeckCopyDialog>();
		DialogBase result = CreateDialog(component, srcDeck);
		component.SetText(Data.SystemText.Get("MyRotation_ID_06", srcDeck.GetDeckName()));
		return result;
	}

	public static DialogBase CreateDeckCopyDialogUseSubClass(DeckCopyDialog useSubClassDeckCopyDialogPrefab, DeckData srcDeck)
	{
		DeckCopyDialog component = Object.Instantiate(useSubClassDeckCopyDialogPrefab.gameObject).GetComponent<DeckCopyDialog>();
		DialogBase dialogBase = CreateDialog(component, srcDeck);
		component.SetClassesIconAndName(srcDeck);
		dialogBase.SetSize(DialogBase.Size.M);
		return dialogBase;
	}
}
