using UnityEngine;

namespace Wizard;

public class UIMarquee : MonoBehaviour
{
	private enum State
	{
		Stop,
		WaitForMove	}

	[SerializeField]
	[Range(0f, float.MaxValue)]
	private float _widthOfNeedsToScroll = 256f;

	[SerializeField]
	private UIPanel _panel;

	[SerializeField]
	private UIWidget _content;

	private State _state;

	private float _timeCount;

	private float _startX;

	public void StartMarquee()
	{
		if ((float)_content.width < _widthOfNeedsToScroll)
		{
			SetState(State.Stop);
		}
		else
		{
			SetState(State.WaitForMove);
		}
	}

	public void Reset()
	{
		SetState(State.Stop);
		SetAlpha(1f);
	}

	public void SetWidthOfNeedsToScroll(float width)
	{
		_widthOfNeedsToScroll = width;
	}

	private void SetState(State state)
	{
		_state = state;
		_timeCount = 0f;
		_panel.UpdateAnchors();
		_content.UpdateAnchors();
		_startX = CalcStartX();
		SetContentPosX(_startX);
	}

	private float CalcStartX()
	{
		return _panel.baseClipRegion.x - _panel.baseClipRegion.z * 0.5f;
	}

	private void SetContentPosX(float posX)
	{
		Transform obj = _content.transform;
		Vector3 localPosition = obj.localPosition;
		localPosition.x = posX;
		obj.localPosition = localPosition;
	}

	private void SetAlpha(float alpha)
	{
		_panel.alpha = alpha;
	}
}
