using System;
using System.Collections.Generic;
using UnityEngine;
using Wizard;

public class MyPageCharaMenu : MonoBehaviour
{
	private struct DragInfo
	{
		public float time;

		public float posX;

		public DragInfo(float time, float posX)
		{
			this.time = time;
			this.posX = posX;
		}
	}

	private readonly Vector3 RIGHT_BASE_NO_CUSTOM_BG_POSITION = new Vector3(505f, 0f, -50f);

	[SerializeField]
	private UIButton sinkaButton;

	[SerializeField]
	private UILabel labelSinkaButton;

	[SerializeField]
	private UITexture originalTex;

	[SerializeField]
	private GameObject texBase;

	private UITexture[] caharaTex;

	[SerializeField]
	private GameObject _rightBase;

	private Vector3 _rightBaseDefaultPosition;

	private bool _isFirstCallOnEnable = true;

	[SerializeField]
	private MyPageCenterCard cardManager;

	[SerializeField]
	private GameObject CardMainObj;

	[SerializeField]
	private UIScrollView scroll;

	[SerializeField]
	private GameObject scrollColliderObj;

	[SerializeField]
	private UICenterOnChild scrollCenterOnChild;

	private const float SCROLL_X_SIZE = 100f;

	private float _beforeCircleAngle;

	[SerializeField]
	private float InitialSpeed = 22f;

	[SerializeField]
	private float DecelerationBase = 3.5f;

	[SerializeField]
	private float DecelerationAdd = 10f;

	[SerializeField]
	private float DragSpeed = 2.1f;

	[SerializeField]
	private float DragSpeed2 = 0.1f;

	private bool _isClockwise;

	private float _currentSpeed;

	private float _currentDeceleration;

	private bool _isOnEnable;

	private IList<DragInfo> _dragInfoList;

	private int _currentIndex;

	private int _oldIndex = -1;

	private float _soundTimer;

	private bool _isFirstUpdate = true;

	private const string LABEL_BUTTON_EVO_TEXT = "Card_0030";

	private const string LABEL_BUTTON_RETURN_TEXT = "Card_0067";

	public bool IsForceActive { get; set; }

	public UIButton EvolutionButton => sinkaButton;

	private void Start()
	{
		caharaTex = new UITexture[40];
		for (int i = 0; i < 40; i++)
		{
			caharaTex[i] = UnityEngine.Object.Instantiate(originalTex);
			caharaTex[i].transform.parent = texBase.transform;
			caharaTex[i].transform.localScale = new Vector3(0.35f, 0.35f, 1f);
			caharaTex[i].transform.transform.localPosition = new Vector3((float)i * 100f, 0f, 0f);
			caharaTex[i].gameObject.name = i.ToString();
		}
		originalTex.gameObject.SetActive(value: false);
		LoadCenterCardIndex();
		sinkaButton.onClick.Clear();
		sinkaButton.onClick.Add(new EventDelegate(delegate
		{
			if (!cardManager.CardDetail.IsCardObjRotateTween)
			{
				cardManager.ChangeEvoDisplay(_currentIndex);
				cardManager.PlayCardRotationAnime(_currentIndex, cardManager.IsAfterEvolution(_currentIndex));
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_MYPAGE_EVOLVE);
				GameMgr.GetIns().GetEffectMgr().Stop(EffectMgr.EffectType.CMN_MYPAGE_EVO_1);
				GameMgr.GetIns().GetEffectMgr().Start(EffectMgr.EffectType.CMN_MYPAGE_EVO_1, new Vector3(0f, 0.3f, 0f));
				ChangeStateSinkaButton(_currentIndex);
			}
		}));
		ChangeStateSinkaButton(_currentIndex);
		if (_dragInfoList == null)
		{
			_dragInfoList = new List<DragInfo>();
		}
		UIEventListener uIEventListener = UIEventListener.Get(scrollColliderObj);
		uIEventListener.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener.onClick, (UIEventListener.VoidDelegate)delegate
		{
			_currentSpeed = 0f;
		});
		UIEventListener uIEventListener2 = UIEventListener.Get(scrollColliderObj);
		uIEventListener2.onDragStart = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onDragStart, (UIEventListener.VoidDelegate)delegate
		{
			_dragInfoList.Clear();
			_dragInfoList.Add(new DragInfo(Time.time, GetLastTouchPos().x));
		});
		UIEventListener uIEventListener3 = UIEventListener.Get(scrollColliderObj);
		uIEventListener3.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uIEventListener3.onDrag, (UIEventListener.VectorDelegate)delegate(GameObject g, Vector2 delta)
		{
			if (_isClockwise == delta.x < 0f)
			{
				_dragInfoList.Add(new DragInfo(Time.time, GetLastTouchPos().x));
			}
			else
			{
				_currentSpeed = 0f;
				_isClockwise = !_isClockwise;
				_dragInfoList.Clear();
				_dragInfoList.Add(new DragInfo(Time.time, GetLastTouchPos().x));
			}
		});
		UIEventListener uIEventListener4 = UIEventListener.Get(scrollColliderObj);
		uIEventListener4.onDragEnd = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener4.onDragEnd, (UIEventListener.VoidDelegate)delegate
		{
			_dragInfoList.Add(new DragInfo(Time.time, GetLastTouchPos().x));
			int num = _dragInfoList.Count - 1;
			_currentSpeed = 0f;
			float num2 = Mathf.Abs(_dragInfoList[num].time - _dragInfoList[0].time);
			if (!(num2 <= float.Epsilon))
			{
				float num3 = Mathf.Abs(_dragInfoList[num].posX - _dragInfoList[0].posX);
				if (!(num3 <= float.Epsilon))
				{
					float num4 = num3 / num2;
					if (!(num4 < DragSpeed))
					{
						int num5 = 2;
						if (num >= num5)
						{
							num2 = Mathf.Abs(_dragInfoList[num].time - _dragInfoList[num - num5].time);
							if (num2 <= float.Epsilon)
							{
								return;
							}
							num3 = Mathf.Abs(_dragInfoList[num].posX - _dragInfoList[num - num5].posX);
							if (num3 <= float.Epsilon)
							{
								return;
							}
							num4 = num3 / num2;
							if (num4 < DragSpeed2)
							{
								return;
							}
						}
						_currentSpeed = InitialSpeed * num4 * 0.1f;
						_currentDeceleration = DecelerationBase;
					}
				}
			}
		});
	}

	private bool IsAbleUpdate()
	{
		if (!UIManager.GetInstance().getUIBase_CardManager().getCreateEndFlag())
		{
			return false;
		}
		if (!MyPageMenu.Instance.IsCardLoadFinish)
		{
			return false;
		}
		if (!cardManager.canMyPageCardMove())
		{
			return false;
		}
		return true;
	}

	private void Update()
	{
		if (!IsAbleUpdate())
		{
			return;
		}
		if (_isFirstUpdate)
		{
			_isFirstUpdate = false;
			ScrollCenterOn(_currentIndex, 100f);
			return;
		}
		if (_soundTimer > 0f)
		{
			_soundTimer -= Time.deltaTime;
		}
		if (_isOnEnable)
		{
			_isOnEnable = false;
			ChangeStateSinkaButton(_currentIndex);
		}
		Vector3 localPosition = scroll.gameObject.transform.localPosition;
		float num = -4000f;
		float num2 = num / 360f;
		float num3 = localPosition.x / num2;
		float num4;
		for (num4 = localPosition.x - 50f; num4 >= 0f; num4 += num)
		{
		}
		while (num4 < num)
		{
			num4 -= num;
		}
		int num5 = (int)Mathf.Abs(num4 / 100f);
		if (_oldIndex != num5)
		{
			if (!UIManager.GetInstance().isFading() && _soundTimer <= 0f)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_CARD_SCROLL);
				_soundTimer = 0.1f;
			}
			_currentIndex = num5;
			_oldIndex = _currentIndex;
			cardManager.UpdateSelectCard();
			ChangeStateSinkaButton(_currentIndex);
			GameMgr.GetIns().GetEffectMgr().Stop(EffectMgr.EffectType.CMN_MYPAGE_EVO_1);
		}
		if (_beforeCircleAngle != num3)
		{
			_beforeCircleAngle = num3;
			CardMainObj.transform.eulerAngles = new Vector3(0f, num3, 0f);
			cardManager.SetCardTransform();
		}
		if (_currentSpeed > 0f)
		{
			float deltaTime = Time.deltaTime;
			_currentDeceleration += DecelerationAdd * deltaTime;
			_currentSpeed -= _currentDeceleration * deltaTime;
			if (_currentSpeed > 0f)
			{
				float num6 = _currentSpeed * deltaTime * (_isClockwise ? 1f : (-1f));
				num6 /= 10f;
				scroll.Scroll(num6);
			}
			else
			{
				ScrollCenterOn(num5, 1f);
			}
		}
	}

	private void ChangeStateSinkaButton(int index)
	{
		if (!cardManager.isEvo(index))
		{
			sinkaButton.gameObject.SetActive(value: false);
			return;
		}
		sinkaButton.gameObject.SetActive(value: true);
		if (cardManager.IsAfterEvolution(index))
		{
			labelSinkaButton.text = Data.SystemText.Get("Card_0067");
		}
		else
		{
			labelSinkaButton.text = Data.SystemText.Get("Card_0030");
		}
	}

	private void OnEnable()
	{
		if (_isFirstCallOnEnable)
		{
			_isFirstCallOnEnable = false;
			_rightBaseDefaultPosition = _rightBase.transform.localPosition;
		}
		if (Data.Load.data.AcquiredMyPageBGList.Count == 0)
		{
			_rightBase.transform.localPosition = RIGHT_BASE_NO_CUSTOM_BG_POSITION;
		}
		else
		{
			_rightBase.transform.localPosition = _rightBaseDefaultPosition;
		}
		_currentSpeed = 0f;
		_isOnEnable = true;
		cardManager.CardDetail.ResetAngleCardObj();
		scrollColliderObj.SetActive(!IsForceActive);
	}

	private void OnDisable()
	{
		if (!(cardManager.CardDetail.CardDetailWindow == null))
		{
			cardManager.CardDetail.CardDetailWindow.HideDetail();
		}
	}

	private void LoadCenterCardIndex()
	{
		_currentIndex = PlayerPrefsWrapper.GetValue(PlayerPrefsWrapper.HOME_CENTER_CARD_INDEX);
	}

	public void SaveCenterCardIndex()
	{
		PlayerPrefsWrapper.SetValue(PlayerPrefsWrapper.HOME_CENTER_CARD_INDEX, _currentIndex);
	}

	public int getNowSelIndex()
	{
		return _currentIndex;
	}

	private Vector2 GetLastTouchPos()
	{
		return UICamera.currentCamera.ScreenToWorldPoint(UICamera.lastEventPosition);
	}

	private void ScrollCenterOn(int index)
	{
		scrollCenterOnChild.CenterOn(caharaTex[index].transform);
	}

	private void ScrollCenterOn(int index, float springStrength)
	{
		float springStrength2 = scrollCenterOnChild.springStrength;
		scrollCenterOnChild.springStrength = springStrength;
		scrollCenterOnChild.CenterOn(caharaTex[index].transform);
		scrollCenterOnChild.springStrength = springStrength2;
	}
}
