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

namespace Wizard;

public class FormatChangeUI : MonoBehaviour
{
	public enum FormatCategory
	{
		Invalid,
		Rotation,
		Unlimited,
		PreRotation,
		Crossover,
		Hof,
		Windfall,
		MyRotation,
		Avatar,
		Other
	}

	private const string UNLIMITED_CENTER_SPRITE_OFF = "btn_unlimited_center_off";

	private const string UNLIMITED_CENTER_SPRITE_ON = "btn_unlimited_center_on";

	private const string UNLIMITED_RIGHT_SPRITE_OFF = "btn_gacha_unlimited_off";

	private const string UNLIMITED_RIGHT_SPRITE_ON = "btn_gacha_unlimited_on";

	private const string OLD_ROTATION_SPRITE_ON = "btn_gacha_rotation_on";

	private const string OLD_ROTATION_SPRITE_OFF = "btn_gacha_rotation_off";

	private static readonly Dictionary<FormatCategory, (string ON, string OFF)> ANOTHER_BTN_SPRITES = new Dictionary<FormatCategory, (string, string)>
	{
		{
			FormatCategory.Other,
			("btn_gacha_other_on", "btn_gacha_other_off")
		},
		{
			FormatCategory.PreRotation,
			("btn_prerotation_on", "btn_prerotation_off")
		},
		{
			FormatCategory.Crossover,
			("btn_gacha_crossover_on", "btn_gacha_crossover_off")
		},
		{
			FormatCategory.MyRotation,
			("btn_gacha_myrotation_on", "btn_gacha_myrotation_off")
		}
	};

	[SerializeField]
	private UIButton _btnRotation;

	[SerializeField]
	private UIButton _btnUnlimited;

	[SerializeField]
	private UIButton _btnAnother;

	private FormatCategory _currentFormatCategory = FormatCategory.Rotation;

	private Action<FormatCategory> _onChangeFormat;

	public static FormatChangeUI Create(FormatCategory defaultFormatCategory, FormatCategory anotherFormatCategory, Action<FormatCategory> onChangeFormat)
	{
		FormatChangeUI component = (UnityEngine.Object.Instantiate(Resources.Load("UI/layoutParts/Guild/FormatChangeUI")) as GameObject).GetComponent<FormatChangeUI>();
		component.gameObject.SetActive(value: true);
		component.Initialize(defaultFormatCategory, anotherFormatCategory, onChangeFormat);
		return component;
	}

	public void ShowOldRotationIcon()
	{
		_btnRotation.normalSprite = "btn_gacha_rotation_on";
		_btnRotation.pressedSprite = "btn_gacha_rotation_off";
	}

	public void ChangeFormat(FormatCategory inFormatCategory)
	{
		_currentFormatCategory = inFormatCategory;
		UpdateFormatBtnSprite(inFormatCategory);
	}

	public void SetEnableFormatButton(FormatCategory formatCategory, bool isEnable)
	{
		switch (formatCategory)
		{
		case FormatCategory.Rotation:
			UIManager.SetObjectToGrey(_btnRotation.gameObject, !isEnable);
			return;
		case FormatCategory.Unlimited:
			UIManager.SetObjectToGrey(_btnUnlimited.gameObject, !isEnable);
			return;
		}
		if (ANOTHER_BTN_SPRITES.ContainsKey(formatCategory))
		{
			UIManager.SetObjectToGrey(_btnAnother.gameObject, !isEnable);
		}
	}

	public void UpdateAnotherFormatButton(FormatCategory anotherFormatCategory)
	{
		bool flag = anotherFormatCategory != FormatCategory.Invalid;
		_btnAnother.gameObject.SetActive(flag);
		_btnUnlimited.normalSprite = (flag ? "btn_unlimited_center_off" : "btn_gacha_unlimited_off");
		_btnUnlimited.pressedSprite = (flag ? "btn_unlimited_center_on" : "btn_gacha_unlimited_on");
		if (flag)
		{
			_btnAnother.onClick.Add(new EventDelegate(delegate
			{
				OnClickChangeFormatBtn(anotherFormatCategory);
			}));
			_btnAnother.normalSprite = ANOTHER_BTN_SPRITES[anotherFormatCategory].OFF;
			_btnAnother.pressedSprite = ANOTHER_BTN_SPRITES[anotherFormatCategory].ON;
		}
		else
		{
			_btnAnother.onClick.Clear();
		}
	}

	private void Initialize(FormatCategory defaultFormatCategory, FormatCategory anotherFormatCategory, Action<FormatCategory> onChangeFormat)
	{
		InitializeFormatCategoryButtons(anotherFormatCategory);
		_onChangeFormat = onChangeFormat;
		ChangeFormat(defaultFormatCategory);
	}

	private void InitializeFormatCategoryButtons(FormatCategory anotherFormatCategory)
	{
		_btnRotation.onClick.Add(new EventDelegate(delegate
		{
			OnClickChangeFormatBtn(FormatCategory.Rotation);
		}));
		_btnUnlimited.onClick.Add(new EventDelegate(delegate
		{
			OnClickChangeFormatBtn(FormatCategory.Unlimited);
		}));
		UpdateAnotherFormatButton(anotherFormatCategory);
	}

	private void OnClickChangeFormatBtn(FormatCategory formatCategory)
	{
		if (_currentFormatCategory != formatCategory)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
			ChangeFormat(formatCategory);
			_onChangeFormat.Call(formatCategory);
			StartCoroutine(WaitForCanClickFormatBtns());
		}
	}

	private IEnumerator WaitForCanClickFormatBtns()
	{
		SetEnableAllBtns(isEnable: false);
		yield return new WaitForSeconds(0.4f);
		SetEnableAllBtns(isEnable: true);
	}

	private void SetEnableAllBtns(bool isEnable)
	{
		_btnRotation.isEnabled = isEnable;
		_btnUnlimited.isEnabled = isEnable;
		_btnAnother.isEnabled = isEnable;
	}

	private string RenameSpriteString(string str, bool isEnable)
	{
		string oldValue = (isEnable ? "_off" : "_on");
		string newValue = (isEnable ? "_on" : "_off");
		return str.Replace(oldValue, newValue);
	}

	private void UpdateFormatBtnSprite(FormatCategory inFormatCategory)
	{
		switch (inFormatCategory)
		{
		case FormatCategory.Rotation:
			_btnRotation.normalSprite = RenameSpriteString(_btnRotation.normalSprite, isEnable: true);
			_btnUnlimited.normalSprite = RenameSpriteString(_btnUnlimited.normalSprite, isEnable: false);
			_btnAnother.normalSprite = RenameSpriteString(_btnAnother.normalSprite, isEnable: false);
			return;
		case FormatCategory.Unlimited:
			_btnRotation.normalSprite = RenameSpriteString(_btnRotation.normalSprite, isEnable: false);
			_btnUnlimited.normalSprite = RenameSpriteString(_btnUnlimited.normalSprite, isEnable: true);
			_btnAnother.normalSprite = RenameSpriteString(_btnAnother.normalSprite, isEnable: false);
			return;
		}
		if (ANOTHER_BTN_SPRITES.ContainsKey(inFormatCategory))
		{
			_btnRotation.normalSprite = RenameSpriteString(_btnRotation.normalSprite, isEnable: false);
			_btnUnlimited.normalSprite = RenameSpriteString(_btnUnlimited.normalSprite, isEnable: false);
			_btnAnother.normalSprite = RenameSpriteString(_btnAnother.normalSprite, isEnable: true);
		}
	}
}
