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

public class InPlayCardControl
{
	private readonly GameObject m_gameObject;

	private const int cMax = 5;

	private const float cBaseX = 215f;

	private Vector3[] cPos;

	private readonly Vector3 PLAYER_FIELDCARD_SPACE = new Vector3(0f, -110f, 0f);

	private readonly Vector3 ENEMY_FIELDCARD_SPACE = new Vector3(0f, 158f, 0f);

	public Transform transform { get; private set; }

	public InPlayCardControl(GameObject gameObject)
	{
		m_gameObject = gameObject;
		transform = m_gameObject.transform;
		cPos = new Vector3[5];
	}

	private void SetInPlayPosition(List<IBattleCardView> battleCardViewList)
	{
		int num = battleCardViewList.Count;
		if (num >= 5)
		{
			num = 5;
		}
		for (int i = 0; i < num; i++)
		{
			switch (m_gameObject.name)
			{
			case "FieldCard":
				m_gameObject.transform.localPosition = PLAYER_FIELDCARD_SPACE;
				cPos[i] = new Vector3(215f * (float)i - 215f * (float)(num - 1) / 2f, 0f, 0f);
				break;
			case "EFieldCard":
				m_gameObject.transform.localPosition = ENEMY_FIELDCARD_SPACE;
				cPos[i] = new Vector3(-215f * (float)i + 215f * (float)(num - 1) / 2f, 0f, 0f);
				break;
			}
		}
	}

	public void RearrangePlayerInplay(float time, List<IBattleCardView> battleCardViewList)
	{
		SetInPlayPosition(battleCardViewList);
		int num = 0;
		foreach (IBattleCardView battleCardView in battleCardViewList)
		{
			if (!battleCardView.isHiddenFromInPlayView)
			{
				if (BattleManagerBase.GetIns().IsRecovery)
				{
					Transform transform = battleCardView.Transform;
					transform.localPosition = new Vector3(cPos[num].x, transform.localPosition.y, transform.localPosition.z);
				}
				else
				{
					if (battleCardView._inPlayRearrangeCoroutine != null)
					{
						BattleCoroutine.GetInstance().StopCoroutine(battleCardView._inPlayRearrangeCoroutine);
					}
					battleCardView._inPlayRearrangeCoroutine = BattleCoroutine.GetInstance().StartCoroutine(MoveCardToNewPosition(battleCardView, cPos[num], time));
				}
			}
			num++;
		}
	}

	private IEnumerator MoveCardToNewPosition(IBattleCardView cardView, Vector3 targetPosition, float time)
	{
		Transform cardTransform = cardView.Transform;
		float startTime = Time.time;
		while (true)
		{
			float num = (Time.time - startTime) / time;
			if (num > 1f)
			{
				break;
			}
			float x = Mathf.Lerp(cardTransform.localPosition.x, targetPosition.x, num);
			cardTransform.localPosition = new Vector3(x, cardTransform.localPosition.y, cardTransform.localPosition.z);
			yield return null;
		}
		cardTransform.localPosition = new Vector3(targetPosition.x, cardTransform.localPosition.y, cardTransform.localPosition.z);
	}

	public Vector3 GetPosition(int index)
	{
		return cPos[index];
	}

	public Vector3 GetOverflowPosition(int index, bool isPlayer)
	{
		float num = (isPlayer ? 1f : (-1f));
		return new Vector3((430f + 215f * (float)(index + 1)) * num, 0f, 0f);
	}

	public static Vector3 CalcPosition(int cardCount, int index, bool isPlayer)
	{
		if (isPlayer)
		{
			return new Vector3(215f * (float)index - 215f * (float)(cardCount - 1) / 2f, 0f, 0f);
		}
		return new Vector3(-215f * (float)index + 215f * (float)(cardCount - 1) / 2f, 0f, 0f);
	}
}
