using System;
using UnityEngine;
using static AffectingForcesManager;

public class NimbleZoneDetection : MonoBehaviour
{
	[SerializeField]
	private static float gravityFactor = 30f;
	[SerializeField]
	private Gravities outsideGravityFunction = Gravities.NoGravity;
	[SerializeField]
	private Zone zone = Zone.NimbleZone;
	public AffectingForcesManager forcesManager;

	public enum Gravities
	{
		DownGravity, NoGravity, InwardsGravity, OutwardsGravity
	}

	private Func<Vector3, Vector3>[] gravityFunctions = { DownGravity, NoGravity, InwardsGravity, OutwardsGravity };

	private static Func<Vector3, Vector3> NoGravity = new Func<Vector3, Vector3>(position => new Vector3());
	private static Func<Vector3, Vector3> DownGravity = new Func<Vector3, Vector3>(position => Vector3.down * gravityFactor);
	private static Func<Vector3, Vector3> InwardsGravity = new Func<Vector3, Vector3>
	(position => new Vector3(position.x, position.y, 0).normalized * -gravityFactor);
	private static Func<Vector3, Vector3> OutwardsGravity = new Func<Vector3, Vector3>
	(position => new Vector3(position.x, position.y, 0).normalized * gravityFactor);

	public Func<Vector3, Vector3> GetGravityFunction(Gravities gravity)
	{
		return gravityFunctions[(int)gravity];
	}

	private void OnTriggerEnter(Collider collider)
	{
		int instanceID = collider.gameObject.GetInstanceID();
		forcesManager.SetGravityForInstance(instanceID, NoGravity);
		forcesManager.SetZoneForInstance(instanceID, Zone.NimbleZone);
	}

	private void OnTriggerExit(Collider collider)
	{
		int instanceID = collider.gameObject.GetInstanceID();
		forcesManager.SetGravityForInstance(instanceID, GetGravityFunction(outsideGravityFunction));
		forcesManager.SetZoneForInstance(instanceID, Zone.OutsideZone);
	}

}
