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

public class UIWrapVariableContentVertical : UIWrapContent
{
	public class ItemParam
	{
		public float Position { get; set; }

		public int Size { get; set; }

		public ItemParam(float pos, int size)
		{
			Position = pos;
			Size = size;
		}
	}

	private int _cashNum;

	private int _nowIndex;

	private float _marginSize = float.MaxValue;

	public List<ItemParam> ItemParamList { get; } = new List<ItemParam>();

	public void Init(int cashNum, int nowIndex, int maxIndex, OnInitializeItem _onInitializeItem)
	{
		_cashNum = cashNum;
		_nowIndex = nowIndex;
		maxIndex = 0;
		onInitializeItem = _onInitializeItem;
	}

	public bool IsBottom()
	{
		if (!IsScrollEnableSize())
		{
			return true;
		}
		return mPanel.transform.localPosition.y > GetPosYBottomItem();
	}

	public void UpdateItemSize(int index, int size)
	{
		if (0 <= index && index > ItemParamList.Count)
		{
			return;
		}
		ItemParam itemParam = ItemParamList[index];
		int num = itemParam.Size - size;
		itemParam.Size = size;
		MoveScrollPanel(mPanel.gameObject.transform.localPosition.y - (float)num);
		if (index++ < ItemParamList.Count)
		{
			for (int i = index; i < ItemParamList.Count; i++)
			{
				ItemParamList[i].Position += num;
			}
		}
		UpdateItemCurrentAll();
		WrapContent();
		mScroll.UpdateScrollbars();
	}

	public void AddItemList(List<int> addItemSizeList, bool isBottom)
	{
		if (isBottom)
		{
			for (int i = 0; i < addItemSizeList.Count; i++)
			{
				float pos = 0f;
				if (ItemParamList.Count > 0)
				{
					ItemParam itemParam = ItemParamList[ItemParamList.Count - 1];
					pos = itemParam.Position - (float)itemParam.Size;
				}
				AddItemListBottom(pos, addItemSizeList[i]);
			}
			SortChildrenVertical(isAscendingOrder: false);
		}
		else
		{
			if (ItemParamList.Count > 0)
			{
				_nowIndex += addItemSizeList.Count;
			}
			for (int num = addItemSizeList.Count - 1; num >= 0; num--)
			{
				int num2 = addItemSizeList[num];
				float pos2 = 0f;
				if (ItemParamList.Count > 0)
				{
					pos2 = ItemParamList[0].Position + (float)num2;
				}
				AddItemListTop(pos2, num2);
			}
			SortChildrenVertical(isAscendingOrder: true);
		}
		minIndex = -(ItemParamList.Count - 1);
	}

	private void AddItemListBottom(float pos, int size)
	{
		ItemParamList.Add(new ItemParam(pos, size));
		if ((float)size < _marginSize)
		{
			_marginSize = size;
		}
	}

	private void AddItemListTop(float pos, int size)
	{
		ItemParamList.Insert(0, new ItemParam(pos, size));
		if ((float)size < _marginSize)
		{
			_marginSize = size;
		}
	}

	protected override void ResetChildPositions()
	{
		ResetChildPositionsByIndex(0);
	}

	private void ResetChildPositionsByIndex(int index)
	{
		_nowIndex = index;
		for (int i = 0; i < mChildren.Count; i++)
		{
			int num = index + i;
			Transform transform = mChildren[i];
			if (num < ItemParamList.Count)
			{
				transform.localPosition = new Vector3(0f, ItemParamList[num].Position, 0f);
			}
			UpdateItem(transform, i);
		}
	}

	public void ResetScrollPositionByIndex(int centerIndex)
	{
		centerIndex = Mathf.Clamp(centerIndex, 0, ItemParamList.Count - 1);
		int num = 0;
		int num2 = ItemParamList.Count - 1;
		float num3 = mPanel.GetViewSize().y - mPanel.clipSoftness.y * 2f;
		float num4 = num3 / 2f;
		for (int i = 0; i < ItemParamList.Count; i++)
		{
			num += ItemParamList[i].Size;
			if (i >= centerIndex)
			{
				num4 -= (float)ItemParamList[i].Size;
				if (num4 < 0f)
				{
					num2 = i;
					break;
				}
			}
		}
		mScroll.ResetPosition();
		if (num3 > (float)num)
		{
			return;
		}
		Vector3 vector = new Vector3(0f, (float)num - num3);
		mScroll.transform.localPosition += vector;
		mPanel.clipOffset -= (Vector2)vector;
		int num5 = Mathf.Max(0, ItemParamList.Count - _cashNum);
		int value = num5;
		int num6 = 0;
		for (int j = 0; j < _cashNum; j++)
		{
			int num7 = num2 - j;
			num6 += ItemParamList[num7].Size;
			if ((float)num6 > num3)
			{
				value = num7;
				break;
			}
		}
		value = Mathf.Clamp(value, 0, num5);
		ResetChildPositionsByIndex(value);
	}

	public void ResetScrollPositionBottomEasing()
	{
		StartCoroutine(SetPositionBottomEasing());
	}

	private IEnumerator SetPositionBottomEasing()
	{
		mScroll.currentMomentum = Vector3.zero;
		mScroll.DisableSpring();
		WrapContent();
		mScroll.UpdateScrollbars();
		float targetPosY = GetPosYBottomItem() + (float)ItemParamList[ItemParamList.Count - 1].Size;
		CustomEasing easing = new CustomEasing(CustomEasing.eType.outQuad, mPanel.gameObject.transform.localPosition.y, targetPosY, 0.3f);
		while (easing.IsMoving)
		{
			MoveScrollPanel(easing.GetCurVal(Time.deltaTime));
			yield return null;
		}
		MoveScrollPanel(targetPosY);
	}

	private float GetPosYBottomItem()
	{
		return mPanel.baseClipRegion.y - mPanel.baseClipRegion.w * 0.5f + mPanel.clipSoftness.y * 2f - ItemParamList[ItemParamList.Count - 1].Position;
	}

	public float GetDistanceBetweenBottomItemAndScrollBottomPos()
	{
		return GetPosYBottomItem() + (float)ItemParamList[ItemParamList.Count - 1].Size - mPanel.gameObject.transform.localPosition.y;
	}

	public void MoveScrollPanel(float targetPosY)
	{
		Vector3 vector = (mPanel.gameObject.transform.localPosition.y - targetPosY) * Vector3.up;
		mPanel.gameObject.transform.localPosition -= vector;
		mPanel.clipOffset += (Vector2)vector;
		mScroll.UpdateScrollbars();
	}

	public bool IsScrollEnableSize()
	{
		float num = mPanel.GetViewSize().y;
		for (int i = 0; i < ItemParamList.Count; i++)
		{
			num -= (float)ItemParamList[i].Size;
			if (num <= 0f)
			{
				return true;
			}
		}
		return false;
	}

	public float TopOverflowItemSize()
	{
		Vector3[] worldCorners = mPanel.worldCorners;
		float num = mTrans.InverseTransformPoint(worldCorners[1]).y - mPanel.clipSoftness.y;
		return ItemParamList[0].Position - num;
	}

	public override bool WrapContent()
	{
		if (mHorizontal)
		{
			return true;
		}
		if (mChildren.Count <= 0)
		{
			return true;
		}
		bool result = false;
		bool flag = true;
		Vector3[] worldCorners = mPanel.worldCorners;
		for (int i = 0; i < worldCorners.Length; i++)
		{
			Vector3 position = worldCorners[i];
			position = mTrans.InverseTransformPoint(position);
			worldCorners[i] = position;
		}
		List<bool> list = new List<bool>(mChildren.Count);
		int j = 0;
		for (int count = mChildren.Count; j < count; j++)
		{
			list.Add(item: false);
		}
		SortChildrenVertical(isAscendingOrder: true);
		float y = mChildren[mChildren.Count - 1].localPosition.y;
		float num = mChildren[0].localPosition.y - (float)ItemParamList[-1 * GetRealIndex(mChildren[0].localPosition)].Size;
		float num2 = worldCorners[2].y + _marginSize;
		float num3 = worldCorners[0].y - _marginSize;
		if (y < num2)
		{
			int num4 = 0;
			for (int k = 0; k < ItemParamList.Count; k++)
			{
				Transform transform = mChildren[num4];
				int num5 = _nowIndex - 1;
				int num6 = ((num5 < 0) ? 1 : (num5 * -1));
				if (minIndex > num6 || num6 > maxIndex)
				{
					flag = false;
					break;
				}
				if (transform.localPosition.y > num3)
				{
					break;
				}
				transform.localPosition = new Vector3(transform.localPosition.x, ItemParamList[num5].Position, transform.localPosition.z);
				_nowIndex--;
				list[num4] = true;
				num4 = (num4 + 1) % mChildren.Count;
			}
		}
		else if (num > num3)
		{
			int num7 = mChildren.Count - 1;
			for (int l = 0; l < ItemParamList.Count; l++)
			{
				Transform transform2 = mChildren[num7];
				int num8 = _nowIndex + _cashNum;
				int num9 = ((num8 >= ItemParamList.Count) ? (minIndex - 1) : (num8 * -1));
				if (minIndex > num9 || num9 > maxIndex)
				{
					flag = false;
					break;
				}
				if (transform2.localPosition.y - (float)ItemParamList[_nowIndex].Size < num2)
				{
					break;
				}
				transform2.localPosition = new Vector3(transform2.localPosition.x, ItemParamList[num8].Position, transform2.localPosition.z);
				_nowIndex++;
				list[num7] = true;
				num7 = (num7 - 1 + mChildren.Count) % mChildren.Count;
			}
		}
		else
		{
			if (mFirstTime)
			{
				int m = 0;
				for (int count2 = mChildren.Count; m < count2; m++)
				{
					Transform item = mChildren[m];
					UpdateItem(item, m);
				}
			}
			result = true;
		}
		int n = 0;
		for (int count3 = mChildren.Count; n < count3; n++)
		{
			if (list[n])
			{
				Transform item2 = mChildren[n];
				UpdateItem(item2, n);
			}
		}
		mScroll.restrictWithinPanel = !flag;
		return result;
	}

	protected override void UpdateItem(Transform item, int index)
	{
		if (onInitializeItem != null)
		{
			int realIndex = GetRealIndex(item.localPosition);
			onInitializeItem(item.gameObject, index, realIndex);
		}
	}

	private void UpdateItemCurrentAll()
	{
		int i = 0;
		for (int count = mChildren.Count; i < count; i++)
		{
			Transform transform = mChildren[i];
			int num = _nowIndex + i;
			if (num <= ItemParamList.Count)
			{
				Vector3 localPosition = transform.localPosition;
				localPosition.y = ItemParamList[num].Position;
				transform.localPosition = localPosition;
				onInitializeItem?.Invoke(transform.gameObject, i, num * -1);
				continue;
			}
			break;
		}
	}

	private int GetRealIndex(Vector3 item)
	{
		int result = 0;
		for (int i = 0; i < ItemParamList.Count; i++)
		{
			if (item.y == ItemParamList[i].Position)
			{
				result = i * -1;
				break;
			}
		}
		return result;
	}

	private void SortChildrenVertical(bool isAscendingOrder)
	{
		if (isAscendingOrder)
		{
			mChildren.Sort((Transform a, Transform b) => a.localPosition.y.CompareTo(b.localPosition.y));
		}
		else
		{
			mChildren.Sort((Transform a, Transform b) => b.localPosition.y.CompareTo(a.localPosition.y));
		}
	}
}
