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

public abstract class HandViewBase
{
	protected readonly HandControl _handControl;

	protected readonly List<IBattleCardView> _battleCardViewList;

	protected const float REARRANGE_TIME = 0.2f;

	protected const float WATCH_REARRANGE_TIME = 0.1f;

	public HandViewBase()
	{
	}

	public HandViewBase(GameObject handGameObject, BattleCamera battleCamera)
	{
		_handControl = CreateHandControl(handGameObject, battleCamera);
		_battleCardViewList = new List<IBattleCardView>();
	}

	public virtual void SetClassBattleCardView(IBattleCardView classBattleCardView)
	{
	}

	protected abstract void RearrangeHand(float rearrangeTime, bool isNewReplayMoveTurn = false);

	protected abstract HandControl CreateHandControl(GameObject handGameObject, BattleCamera battleCamera);

	public virtual void AddCardToView(IBattleCardView cardViewToAdd, float deckRearrangeTime, bool isNewReplayMoveTurn = false)
	{
		if (!(cardViewToAdd is NullBattleCardView))
		{
			if (BattleManagerBase.GetIns().IsRecovery)
			{
				deckRearrangeTime = 0f;
			}
			AddCardToViewWithoutRearrange(cardViewToAdd);
			RearrangeHand(deckRearrangeTime, isNewReplayMoveTurn);
		}
	}

	public virtual void AddCardToViewWithoutRearrange(IBattleCardView cardViewToAdd)
	{
		if (!_battleCardViewList.Contains(cardViewToAdd))
		{
			_battleCardViewList.Add(cardViewToAdd);
		}
	}

	public void AddCardsToView(List<IBattleCardView> cardViewsToAdd, float deckRearrangeTime)
	{
		if (BattleManagerBase.GetIns().IsRecovery)
		{
			deckRearrangeTime = 0f;
		}
		List<IBattleCardView> list = cardViewsToAdd.FindAll((IBattleCardView cardView) => !_battleCardViewList.Contains(cardView));
		if (list.Any())
		{
			_battleCardViewList.AddRange(list);
			RearrangeHand(deckRearrangeTime);
		}
	}

	public virtual void RemoveCardFromView(IBattleCardView cardViewToRemove, float deckRearrangeTime)
	{
		if (BattleManagerBase.GetIns().IsRecovery)
		{
			deckRearrangeTime = 0f;
		}
		if (_battleCardViewList.Contains(cardViewToRemove))
		{
			RemoveCardFromViewWithoutRearrange(cardViewToRemove);
			RearrangeHand(deckRearrangeTime);
		}
	}

	public virtual void RemoveCardFromViewWithoutRearrange(IBattleCardView cardViewToRemove)
	{
		if (_battleCardViewList.Remove(cardViewToRemove))
		{
			cardViewToRemove.isHiddenFromHandView = false;
		}
	}

	public virtual VfxBase ShuffleHand()
	{
		return NullVfx.GetInstance();
	}

	public void HideCardFromView(IBattleCardView cardViewToHide)
	{
		float rearrangeTime = (BattleManagerBase.GetIns().IsRecovery ? 0f : 0.2f);
		if (_battleCardViewList.Contains(cardViewToHide))
		{
			cardViewToHide.isHiddenFromHandView = true;
			RearrangeHand(rearrangeTime);
		}
	}

	public void UnhideCardFromView(IBattleCardView cardViewToHide, float deckRearrangeTime = 0.2f)
	{
		if (BattleManagerBase.GetIns().IsRecovery)
		{
			deckRearrangeTime = 0f;
		}
		if (_battleCardViewList.Contains(cardViewToHide))
		{
			cardViewToHide.isHiddenFromHandView = false;
			RearrangeHand(deckRearrangeTime);
		}
	}

	public void LockHandState()
	{
		_handControl.LockHandControlState();
	}

	public virtual VfxBase HandUnfocus()
	{
		_handControl.SetHandState(HandControl.HandState.Unfocus);
		return ParallelVfxPlayer.Create(InstantVfx.Create(delegate
		{
			RearrangeHand(0.2f);
		}), WaitVfx.Create(0.2f));
	}

	public virtual VfxBase HandFocus()
	{
		_handControl.SetHandState(HandControl.HandState.Focus);
		return ParallelVfxPlayer.Create(InstantVfx.Create(delegate
		{
			RearrangeHand(0.2f);
		}), WaitVfx.Create(0.2f));
	}

	public virtual VfxBase FocusRearrangeHandHand()
	{
		if (_handControl.IsHandStateFocus())
		{
			return HandFocus();
		}
		return HandUnfocus();
	}

	public HandControl GetHandControl()
	{
		return _handControl;
	}

	public void ReplaceCardInView(IBattleCardView originalView, IBattleCardView newView)
	{
		ReplaceCardInViewWithoutRearrange(originalView, newView);
	}

	public void ReplaceCardInViewWithoutRearrange(IBattleCardView originalView, IBattleCardView newView)
	{
		_battleCardViewList.Insert(_battleCardViewList.IndexOf(originalView), newView);
		_battleCardViewList.RemoveAt(_battleCardViewList.IndexOf(originalView));
	}

	public int GetViewIndex(IBattleCardView viewCard)
	{
		return _battleCardViewList.IndexOf(viewCard);
	}

	public static VfxBase CreateHideCardMeshesVfx(IEnumerable<BattleCardBase> targetCards)
	{
		ParallelVfxPlayer parallelVfxPlayer = ParallelVfxPlayer.Create();
		if (!GameMgr.GetIns().IsAdmin)
		{
			foreach (BattleCardBase targetCard in targetCards)
			{
				parallelVfxPlayer.Register(new ShowCardNumberLabelVfx(targetCard.BattleCardView, isShow: false));
			}
		}
		return parallelVfxPlayer;
	}

	public void ChangeArrangeType(HandControl.ArrangeType type)
	{
		_handControl.ChangeArrangeType(type, 0.3f, _battleCardViewList);
	}

	public virtual VfxBase AsyncTouchCard(GameObject card)
	{
		return NullVfx.GetInstance();
	}
}
