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

namespace Wizard;

public class ChatSendStampUI : MonoBehaviour
{
	private const int SCROLL_OBJECT_NUM = 8;

	[SerializeField]
	private UIButton _buttonShowStampList;

	[SerializeField]
	private ChatOpenCloseAnimation _uiOpenCloseAnimation;

	[SerializeField]
	private UITexture _stampObjOrigin;

	[SerializeField]
	protected UIScrollView _scrollView;

	[SerializeField]
	private UIWrapMuchContent _wrapContent;

	[SerializeField]
	private WrapContentsScrollBarSizeByDirection _wrapContentsScrollBarSize;

	private List<UITexture> _listStampUITexture = new List<UITexture>(8);

	private Action<ChatOpenCloseAnimation> _onCloseStampListUI;

	private Action<int> _OnClickStamp;

	private List<int> _stampList;

	public bool IsOpen => _uiOpenCloseAnimation.IsOpen;

	public void Init(Action<int> onClickStamp, Action<ChatOpenCloseAnimation> onOpenStampListUI, Action<ChatOpenCloseAnimation> onCloseStampListUI, List<int> stampList)
	{
		_stampList = stampList;
		_onCloseStampListUI = onCloseStampListUI;
		_OnClickStamp = onClickStamp;
		_uiOpenCloseAnimation.Init(isOpenedDefault: false);
		_buttonShowStampList.onClick.Clear();
		_buttonShowStampList.onClick.Add(new EventDelegate(delegate
		{
			_uiOpenCloseAnimation.ToggleStateAndStartAnimation();
			if (_uiOpenCloseAnimation.IsOpen)
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_ON);
				ResetStampListScroll();
				onOpenStampListUI.Call(_uiOpenCloseAnimation);
			}
			else
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_TOGGLE_OFF);
				_onCloseStampListUI.Call(_uiOpenCloseAnimation);
			}
		}));
		CreateStampList();
	}

	public void CloseStampList(Action onAnimationEndCallBack = null)
	{
		if (IsOpen)
		{
			_uiOpenCloseAnimation.StartCloseAnimation(onAnimationEndCallBack);
			_onCloseStampListUI.Call(_uiOpenCloseAnimation);
		}
	}

	private void Update()
	{
		if (_uiOpenCloseAnimation.State == ChatOpenCloseAnimation.eState.OPENING)
		{
			for (int i = 0; i < 8; i++)
			{
				_listStampUITexture[i].MarkAsChanged();
			}
		}
	}

	private void CreateStampList()
	{
		SetupScrollView();
	}

	private void SetupScrollView()
	{
		_listStampUITexture.Clear();
		for (int i = 0; i < 8; i++)
		{
			UITexture component = NGUITools.AddChild(_wrapContent.gameObject, _stampObjOrigin.gameObject).GetComponent<UITexture>();
			_listStampUITexture.Add(component);
		}
		_stampObjOrigin.gameObject.SetActive(value: false);
		_wrapContent.onInitializeItem = OnInitializeItem;
		_wrapContent.minIndex = 0;
		_wrapContent.maxIndex = _stampList.Count - 1;
		ResetStampListScroll();
	}

	private void OnInitializeItem(GameObject go, int wrapIndex, int realIndex)
	{
		if (_uiOpenCloseAnimation.IsOpen)
		{
			go.SetActive(value: true);
			int id = _stampList[realIndex];
			Texture mainTexture = Toolbox.ResourcesManager.LoadObject(Toolbox.ResourcesManager.GetAssetTypePath(id.ToString(), ResourcesManager.AssetLoadPathType.Stamp, isfetch: true)) as Texture;
			go.GetComponent<UITexture>().mainTexture = mainTexture;
			UIEventListener.Get(go).onClick = delegate
			{
				GameMgr.GetIns().GetSoundMgr().PlaySe(Se.TYPE.SYS_COMMON_BUTTON);
				_OnClickStamp(id);
			};
		}
	}

	private void ResetStampListScroll()
	{
		_wrapContent.SortBasedOnScrollMovement();
		_scrollView.ResetPosition();
		_wrapContent.WrapContent();
		_wrapContentsScrollBarSize.ContentUpdate();
	}
}
