using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Animator))]
public class Finish : TileAsset
{
	public bool isStandingOn;

	[SerializeField] private List<Finish> otherFinishes;
	[SerializeField] private List<Orientation> allowedOrientations;
	private Animator animator;
	private static readonly int StartTrigger = Animator.StringToHash("Start");

	private void Start()
	{
		animator = GetComponent<Animator>();
	}
	
	public override bool CanEnter(Vector2Int comingFrom, Vector2Int goingInto, Orientation orientation)
	{
		return allowedOrientations.Contains(orientation);
	}

	public override void OnEnter(GameObject player)
	{
		isStandingOn = true;

		foreach (var finish in otherFinishes)
		{
			if (!finish.isStandingOn)
			{
				return;
			}
		}
		
		animator.SetTrigger(StartTrigger);
		foreach (Finish finish in otherFinishes)
		{
			finish.animator.SetTrigger(StartTrigger);
		}
		SceneController.Nextlevel();
	}

	public override void OnLeave(GameObject player)
	{
		isStandingOn = false;
	}
}