using System;
using Cute;
using UnityEngine;

namespace Wizard;

public class ChapterSelectButton : MonoBehaviour
{
	[SerializeField]
	private UIButton _button;

	[SerializeField]
	private UISprite _notSelectMask;

	[SerializeField]
	private UISprite _clearMask;

	[SerializeField]
	private UILabel _clearLabel;

	[SerializeField]
	private UILabel _chapterNo;

	[SerializeField]
	private UILabel _chapterName;

	[SerializeField]
	private UIMarquee _chapterNameMarquee;

	[SerializeField]
	private UITexture _bg;

	[SerializeField]
	private GameObject _tapEffectLocator;

	[SerializeField]
	private UIDragScrollView _dragScrollView;

	private StoryChapterData _chapterData;

	private const float CHAPTERLIST_SCALE_ACTIVE = 1f;

	private const float CHAPTERLIST_SCALE_NOTACTIVE = 0.75f;

	private const float CHAPTERLIST_ALPHA_ACTIVE = 1f;

	private const float CHAPTERLIST_ALPHA_NOTACTIVE = 0.85f;

	private const int CHAPTERLIST_DEPTH_ACTIVE = 4;

	private const int CHAPTERLIST_DEPTH_NOTACTIVE = 3;

	private const float CHAPTERLIST_X_IN_CIRCLE = 190f;

	private const float CHAPTERLIST_X_OUT_CIRCLE = 530f;

	private const float CHAPTER_TITLE_WIDTH = 324f;

	public Action OnClick { get; set; }

	public GameObject TapEffectLocator => _tapEffectLocator;

	public Transform ClearLabelTransform => _clearLabel.transform;

	private void Start()
	{
		_button.onClick.Add(new EventDelegate(delegate
		{
			OnClick.Call();
		}));
	}

	public void SetScrollEnable(bool enable)
	{
		_dragScrollView.enabled = enable;
	}

	public void SetClearVisible(bool visible)
	{
		_clearLabel.gameObject.SetActive(visible);
	}

	public static string GetHeaderPath(StoryChapterData chapterData, bool isFetch)
	{
		return Toolbox.ResourcesManager.GetAssetTypePath("story_chapter_header_" + chapterData.ChapterButtonBgId.ToString("00"), ResourcesManager.AssetLoadPathType.StoryChapterHeader, isFetch);
	}

	public void OnStartClearEffect()
	{
		_clearLabel.gameObject.SetActive(value: true);
		_clearLabel.enabled = false;
	}

	public void OnFinishClearEffect()
	{
		_clearLabel.enabled = true;
	}

	public void UpdateClearState(StoryChapterData.ChapterClearStatus clearState)
	{
		AreaSelectUtility.SetClearLabelColor(_clearLabel, clearState);
	}

	public void SetChapterData(StoryChapterData chapterData, AreaSelectUI parent, UIScrollView scrollView)
	{
		_chapterData = chapterData;
		_dragScrollView.scrollView = scrollView;
		_bg.mainTexture = Toolbox.ResourcesManager.LoadObject<Texture>(GetHeaderPath(chapterData, isFetch: true));
		_bg.gameObject.SetActive(value: true);
		_notSelectMask.gameObject.SetActive(value: false);
		UpdateClearState(chapterData.ClearStatus);
		_clearLabel.gameObject.SetActive(chapterData.IsPlayedChapter);
		if (chapterData.IsMaintenanceChapter)
		{
			Color color = _chapterName.color;
			UIManager.SetObjectToGrey(base.gameObject, b: true);
			_chapterName.color = color;
			_chapterName.text = Data.SystemText.Get("System_0022");
			_chapterNo.text = string.Empty;
		}
		else
		{
			string chapterTitleStory = parent.GetChapterTitleStory(chapterData.ChapterId);
			int num = chapterTitleStory.IndexOf(' ');
			_chapterNo.text = chapterTitleStory.Substring(0, num + 1);
			_chapterName.text = chapterTitleStory.Substring(num + 1);
			_chapterNameMarquee.SetWidthOfNeedsToScroll(324f - (float)_chapterNo.width);
			_chapterNameMarquee.Reset();
		}
	}

	public void UpdateSelectStatus(int SelectChapterIndex, int selfIndex, AreaSelectUI parent, bool isinterpscale, UIScrollView scrollView)
	{
		bool flag = selfIndex == SelectChapterIndex;
		int num = 0;
		float num2 = 0f;
		if (flag)
		{
			num = 4;
			num2 = 1f;
		}
		else
		{
			num = 3;
			num2 = 0.85f;
		}
		bool flag2 = parent.IsUseChapterListClearedMask && _chapterData.IsCleared;
		if (!_chapterData.IsMaintenanceChapter)
		{
			Color color = new Color(1f, 1f, 1f, 1f);
			color.a = num2;
			_bg.color = color;
			_notSelectMask.color = color;
			_chapterNo.color = color;
			_chapterName.color = color;
			_clearLabel.color = color;
		}
		if (_bg.gameObject.activeSelf != flag)
		{
			_bg.gameObject.SetActive(flag);
			SetMarqueeEnable(flag);
		}
		if (_notSelectMask.gameObject.activeSelf == flag)
		{
			_notSelectMask.gameObject.SetActive(!flag);
		}
		if (_clearMask.gameObject.activeSelf != flag2)
		{
			_clearMask.gameObject.SetActive(flag2);
		}
		UpdateInterpsScale(isinterpscale, flag);
		Vector3 localPosition = base.transform.localPosition;
		float num3 = Mathf.Abs(localPosition.y + scrollView.transform.localPosition.y);
		float num4 = 200f;
		if (num3 < num4)
		{
			float num5 = num4 - Mathf.Sqrt(num4 * num4 - num3 * num3);
			num5 *= 2.5f;
			localPosition.x = 190f + num5;
		}
		else
		{
			localPosition.x = 530f + num4;
		}
		base.transform.localPosition = localPosition;
		_bg.depth = num;
		_notSelectMask.depth = num;
	}

	public void SetMarqueeEnable(bool enable)
	{
		if (enable)
		{
			_chapterNameMarquee.StartMarquee();
		}
		else
		{
			_chapterNameMarquee.Reset();
		}
	}

	private void UpdateInterpsScale(bool isinterpscale, bool isSelected)
	{
		float num = (isSelected ? 1f : 0.75f);
		if (isinterpscale)
		{
			Vector3 localScale = base.transform.localScale;
			if (localScale.x < num)
			{
				localScale.x += Time.deltaTime;
				if (localScale.x >= num)
				{
					localScale.x = num;
				}
			}
			else if (localScale.x > num)
			{
				localScale.x -= Time.deltaTime;
				if (localScale.x <= num)
				{
					localScale.x = num;
				}
			}
			localScale.y = localScale.x;
			base.transform.localScale = localScale;
		}
		else
		{
			Vector3 localScale2 = base.transform.localScale;
			localScale2.x = num;
			localScale2.y = num;
			base.transform.localScale = localScale2;
		}
	}
}
