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

namespace Wizard;

public class BannerDialog : MonoBehaviour
{
	private const float DRAG_DEGREE = 70f;

	private const float AUTO_SCROLL_TIME = 5f;

	[SerializeField]
	private UICenterOnChild _uiCenterOnChild;

	[SerializeField]
	private int _widthContentSize = 830;

	[SerializeField]
	private UITexture _bannerObj_1;

	[SerializeField]
	private UITexture _bannerObj_2;

	[SerializeField]
	private BoxCollider _flickCollider;

	[SerializeField]
	private BoxCollider _leftButton;

	[SerializeField]
	private BoxCollider _rightButton;

	[SerializeField]
	private UIToggle indicatorBase;

	private UITexture _currentBannerObj;

	private int _currentTextureIndex;

	private List<Texture> _listBannerTexture;

	private List<UIToggle> _indicatorList = new List<UIToggle>();

	private bool _isAnimation;

	private List<string> _bannerTitleList;

	private DialogBase _dialog;

	private float _autoScrollTimer;

	private bool _isCreateEnd;

	private void Start()
	{
		UIEventListener uIEventListener = UIEventListener.Get(_flickCollider.gameObject);
		uIEventListener.onDrag = (UIEventListener.VectorDelegate)Delegate.Combine(uIEventListener.onDrag, new UIEventListener.VectorDelegate(OnDragPanel));
		UIEventListener uIEventListener2 = UIEventListener.Get(_rightButton.gameObject);
		uIEventListener2.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener2.onClick, (UIEventListener.VoidDelegate)delegate
		{
			NextPage();
		});
		UIEventListener uIEventListener3 = UIEventListener.Get(_leftButton.gameObject);
		uIEventListener3.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener3.onClick, (UIEventListener.VoidDelegate)delegate
		{
			PrevPage();
		});
	}

	private void Update()
	{
		if (_isCreateEnd && _listBannerTexture.Count > 1 && !_isAnimation)
		{
			_autoScrollTimer += Time.deltaTime;
			if (_autoScrollTimer >= 5f)
			{
				NextPage();
			}
		}
	}

	public void Init(List<Texture> bannerTextureList, List<string> bannerTitleList, DialogBase dialog)
	{
		_listBannerTexture = bannerTextureList;
		_currentTextureIndex = 0;
		_currentBannerObj = _bannerObj_1;
		_bannerTitleList = bannerTitleList;
		_dialog = dialog;
		_uiCenterOnChild.onFinished = _OnFinishedCenterChild;
		_bannerObj_1.transform.localPosition = Vector3.zero;
		_bannerObj_2.transform.localPosition = new Vector3(_widthContentSize, 0f, 0f);
		GameObject gameObject = indicatorBase.transform.parent.gameObject;
		if (_listBannerTexture.Count <= 1)
		{
			gameObject.SetActive(value: false);
			_rightButton.gameObject.SetActive(value: false);
			_leftButton.gameObject.SetActive(value: false);
			_bannerObj_2.gameObject.SetActive(value: false);
		}
		else
		{
			gameObject.SetActive(value: true);
			_rightButton.gameObject.SetActive(value: true);
			_leftButton.gameObject.SetActive(value: true);
			_indicatorList.Add(indicatorBase);
			for (int i = 1; i < _listBannerTexture.Count; i++)
			{
				_indicatorList.Add(NGUITools.AddChild(gameObject, indicatorBase.gameObject).GetComponent<UIToggle>());
			}
			gameObject.GetComponent<UIGrid>().Reposition();
			UpdateIndicator(_currentTextureIndex);
		}
		_bannerObj_1.mainTexture = _listBannerTexture[_currentTextureIndex];
		UpdateDialogTitle(_currentTextureIndex);
		_isCreateEnd = true;
	}

	private void _OnFinishedCenterChild()
	{
		_isAnimation = false;
	}

	private void OnDragPanel(GameObject obj, Vector2 dir)
	{
		if (!_isAnimation)
		{
			if (dir.x >= 70f)
			{
				PrevPage();
			}
			else if (dir.x <= -70f)
			{
				NextPage();
			}
		}
	}

	private void NextPage()
	{
		ChangePage(_currentTextureIndex + 1);
	}

	private void PrevPage()
	{
		ChangePage(_currentTextureIndex - 1);
	}

	private void ChangePage(int index)
	{
		if (_currentTextureIndex != index && _listBannerTexture.Count > 1)
		{
			GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_SLIDE_BTN);
			_autoScrollTimer = 0f;
			int num = index;
			if (_listBannerTexture.Count <= index)
			{
				num = 0;
			}
			else if (index < 0)
			{
				num = _listBannerTexture.Count - 1;
			}
			UITexture uITexture = ((_currentBannerObj == _bannerObj_1) ? _bannerObj_2 : _bannerObj_1);
			Vector3 localPosition = _currentBannerObj.transform.localPosition;
			if (_currentTextureIndex > index)
			{
				localPosition.x -= _widthContentSize;
			}
			else
			{
				localPosition.x += _widthContentSize;
			}
			uITexture.transform.localPosition = localPosition;
			uITexture.mainTexture = _listBannerTexture[num];
			_uiCenterOnChild.CenterOn(uITexture.transform);
			_isAnimation = true;
			_currentTextureIndex = num;
			_currentBannerObj = uITexture;
			UpdateDialogTitle(num);
			UpdateIndicator(_currentTextureIndex);
		}
	}

	private void UpdateDialogTitle(int index)
	{
		if (_dialog != null && _bannerTitleList != null)
		{
			_dialog.SetTitleLabel(_bannerTitleList[index]);
		}
	}

	private void UpdateIndicator(int index)
	{
		if (_indicatorList.Count > 1)
		{
			_indicatorList[index].value = true;
		}
	}
}
