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

namespace Wizard;

public class SimpleScrollViewUI : MonoBehaviour
{
	public enum VerticalMovement
	{
		Top,
		Center,
		Bottom
	}

	[SerializeField]
	private int SCROLL_OBJECT_NUM = 6;

	[SerializeField]
	private UIScrollView _scrollView;

	[SerializeField]
	private UIWrapContent _wrapContent;

	[SerializeField]
	private UIScrollBarWrapContent _scrollBarWrapContent;

	[SerializeField]
	private WrapContentsScrollBarSize _wrapContentScrollBarSize;

	[SerializeField]
	private GameObject _plateOriginal;

	[SerializeField]
	private List<GameObject> _plateOriginals;

	private List<GameObject> _plateList = new List<GameObject>();

	private int _contentsNum;

	private Action<int, GameObject> _initializePlate;

	public int ScrollObjectNum => SCROLL_OBJECT_NUM;

	public List<GameObject> ActivePlateList => _plateList.Where((GameObject p) => p.activeInHierarchy).ToList();

	public void CreateScrollView(int contentsNum, Action<int, GameObject> InitializePlate)
	{
		_contentsNum = contentsNum;
		_initializePlate = InitializePlate;
		InitPlateList();
		SetScrollView();
	}

	public void CreateScrollView(List<int> indexes, Action<int, GameObject> InitializePlate)
	{
		_contentsNum = indexes.Count();
		_initializePlate = InitializePlate;
		InitPlateList();
		SetScrollView(indexes);
	}

	public void SetVisiable(bool isVisiable)
	{
		_scrollView.gameObject.SetActive(isVisiable);
		_scrollBarWrapContent.gameObject.SetActive(isVisiable);
		if (_plateOriginal != null)
		{
			_plateOriginal.gameObject.SetActive(isVisiable);
		}
		else
		{
			if (_plateOriginals == null)
			{
				return;
			}
			foreach (GameObject plateOriginal in _plateOriginals)
			{
				plateOriginal.SetActive(isVisiable);
			}
		}
	}

	private void InitPlateList()
	{
		for (int i = 0; i < _plateList.Count; i++)
		{
			UnityEngine.Object.DestroyImmediate(_plateList[i].gameObject);
		}
		_plateList.Clear();
		if (_plateOriginal != null)
		{
			_plateOriginal.gameObject.SetActive(value: false);
		}
		else
		{
			if (_plateOriginals == null)
			{
				return;
			}
			foreach (GameObject plateOriginal in _plateOriginals)
			{
				plateOriginal.SetActive(value: false);
			}
		}
	}

	private void SetScrollView(List<int> indexes = null)
	{
		_scrollView.gameObject.SetActive(value: true);
		_scrollBarWrapContent.gameObject.SetActive(value: true);
		_wrapContent.gameObject.SetActive(value: false);
		_wrapContent.cullContent = false;
		_wrapContent.EnableNoLimit = false;
		int num = Mathf.Min(_contentsNum, SCROLL_OBJECT_NUM);
		for (int i = 0; i < num; i++)
		{
			GameObject prefab = _plateOriginal;
			if (indexes != null && _plateOriginals != null)
			{
				prefab = _plateOriginals[indexes[i]];
			}
			GameObject gameObject = NGUITools.AddChild(_wrapContent.gameObject, prefab);
			gameObject.SetActive(value: false);
			_plateList.Add(gameObject);
		}
		_wrapContent.minIndex = -(_contentsNum - 1);
		_wrapContent.maxIndex = 0;
		_wrapContent.onInitializeItem = OnInitializeItem;
		_wrapContent.gameObject.SetActive(value: true);
		_wrapContent.enabled = true;
		ResetScrollView();
	}

	private void ResetScrollView()
	{
		_wrapContentScrollBarSize.ContentUpdate();
		_wrapContent.SortBasedOnScrollMovement();
		_scrollView.ResetPosition();
		_wrapContent.WrapContent();
	}

	private void OnInitializeItem(GameObject obj, int wrapIndex, int realIndex)
	{
		int num = -realIndex;
		if (num >= _contentsNum || num < 0)
		{
			obj.SetActive(value: false);
			return;
		}
		obj.SetActive(value: true);
		_initializePlate(num, obj);
	}

	public void CenteringPlate()
	{
		if (_scrollView.movement == UIScrollView.Movement.Vertical)
		{
			UIPanel component = _scrollView.GetComponent<UIPanel>();
			float num = component.height / 2f - component.clipSoftness.y;
			float y = (float)_wrapContent.itemSize * (float)_contentsNum / 2f - num;
			Vector3 relative = new Vector3(0f, y, 0f);
			_scrollView.ResetPosition();
			_scrollView.MoveRelative(relative);
			_wrapContent.WrapContent();
		}
	}

	public bool IsAllPlateWithinPanel()
	{
		if (_scrollView.movement != UIScrollView.Movement.Vertical)
		{
			return false;
		}
		UIPanel component = _scrollView.GetComponent<UIPanel>();
		float num = component.height - 2f * component.clipSoftness.y;
		float num2 = (float)_wrapContent.itemSize * (float)_contentsNum;
		return num > num2;
	}

	public void MovePlateByIndex(int index, VerticalMovement move, bool allowMargin = false)
	{
		if (index < 0 || _contentsNum <= index || _scrollView.movement != UIScrollView.Movement.Vertical)
		{
			return;
		}
		UIPanel component = _scrollView.GetComponent<UIPanel>();
		float viewPanelHeight = component.height - 2f * component.clipSoftness.y;
		float num = _wrapContent.itemSize;
		if (!allowMargin)
		{
			if (HasMarginOnTopAfterMove(index, move, viewPanelHeight, num))
			{
				index = 0;
				move = VerticalMovement.Top;
			}
			else if (HasMarginOnButtonAfterMove(index, move, viewPanelHeight, num))
			{
				index = _contentsNum - 1;
				move = VerticalMovement.Bottom;
			}
		}
		float num2 = CalcVerticalMovementOffset(move, viewPanelHeight, num);
		float y = num * (float)index - num2;
		Vector3 relative = new Vector3(0f, y, 0f);
		_scrollView.ResetPosition();
		_scrollView.MoveRelative(relative);
		_wrapContent.WrapContent();
	}

	private bool HasMarginOnTopAfterMove(int index, VerticalMovement move, float viewPanelHeight, float plateSize)
	{
		return move switch
		{
			VerticalMovement.Top => false, 
			VerticalMovement.Center => plateSize * (float)index + plateSize / 2f < viewPanelHeight / 2f, 
			VerticalMovement.Bottom => plateSize * (float)(index + 1) < viewPanelHeight, 
			_ => false, 
		};
	}

	private bool HasMarginOnButtonAfterMove(int index, VerticalMovement move, float viewPanelHeight, float plateSize)
	{
		return move switch
		{
			VerticalMovement.Top => plateSize * (float)(_contentsNum - index) < viewPanelHeight, 
			VerticalMovement.Center => plateSize * (float)(_contentsNum - (index + 1)) + plateSize / 2f < viewPanelHeight / 2f, 
			VerticalMovement.Bottom => false, 
			_ => false, 
		};
	}

	private float CalcVerticalMovementOffset(VerticalMovement move, float viewPanelHeight, float plateSize)
	{
		return move switch
		{
			VerticalMovement.Top => 0f, 
			VerticalMovement.Center => (viewPanelHeight - plateSize) / 2f, 
			VerticalMovement.Bottom => viewPanelHeight - plateSize, 
			_ => 0f, 
		};
	}

	public void SetScrollViewLayoutByAcquireSkinCardPack()
	{
		SCROLL_OBJECT_NUM = 5;
		Transform obj = base.transform.Find("List");
		UIUtil.SetPositionY(obj, 0f);
		UIPanel component = obj.GetComponent<UIPanel>();
		Vector4 baseClipRegion = component.baseClipRegion;
		baseClipRegion.w = 472f;
		component.baseClipRegion = baseClipRegion;
		Transform obj2 = obj.transform.Find("AllScrollBG");
		UIUtil.SetPositionY(obj2, -5f);
		BoxCollider component2 = obj2.GetComponent<BoxCollider>();
		Vector3 size = component2.size;
		size.y = 472f;
		component2.size = size;
		_scrollBarWrapContent.foregroundWidget.height = 400;
		_scrollBarWrapContent.backgroundWidget.height = 400;
		UIPanel component3 = _scrollView.GetComponent<UIPanel>();
		Vector4 baseClipRegion2 = component3.baseClipRegion;
		baseClipRegion2.w = 443f;
		component3.baseClipRegion = baseClipRegion2;
	}
}
