#nullable enable
using NaughtyAttributes;
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))]
public class PlayerMovement : MonoBehaviour {
	[SerializeField] [Required]
	GameFlowManager gameFlowManager = null!;

	[SerializeField] [field: Required]
	PlayerStats stats = null!;

	[field: Required]
	Rigidbody2D rb = null!;

	Vector2 moveDirection;
	BaseState currentState = null!;
	SafeZone? safeZone;

	bool lastJumpButton;

	#region Unity Messages

	void Awake() {
		rb = GetComponent<Rigidbody2D>();
		currentState = new ImmobileMovementState(this);
	}

	void Start() => currentState.EnterState();

	void Update() {
		if (gameFlowManager.Paused)
			return;

		if (currentState.UpdateState() is {} newState)
			SwitchState(newState);
	}

	void FixedUpdate() {
		if (gameFlowManager.Paused)
			return;

		if (currentState.FixedUpdateState() is {} newState)
			SwitchState(newState);
	}

	void OnDrawGizmos() => currentState?.OnDrawGizmos();

	#endregion

	#region Inputs

	public void OnMove(InputAction.CallbackContext ctx) {
		moveDirection = ctx.ReadValue<Vector2>();
		if (moveDirection.sqrMagnitude > 1.0f)
			moveDirection.Normalize();
	}

	public void OnJump(InputAction.CallbackContext ctx) {
		if (!ctx.WasPressedThisFrame(ref lastJumpButton))
			return;

		if (gameFlowManager.Paused || safeZone == null)
			return;

		if (safeZone.IsInSafeZone) {
			if (moveDirection.magnitude >= safeZone.Stats.MinJumpJoystickValue)
				SwitchState(new ExitSafeZoneMovementState(this, safeZone, moveDirection));
		} else //TODO if (AngleBetween(moveDirection, toSafeZone) < 90)
			SwitchState(new EnterSafeZoneMovementState(this, safeZone));
	}

	#endregion

	#region Rigidbody

	void OnTriggerEnter2D(Collider2D other) {
		if (other.GetComponent<SafeZone>() is {} triggeredSafeZone)
			safeZone = triggeredSafeZone;
	}

	//Probably should remove, mostly for ImmobileState's draw gizmos
	void OnTriggerStay2D(Collider2D other) {
		if (other.GetComponent<SafeZone>() is {} triggeredSafeZone)
			safeZone = triggeredSafeZone;
	}

	void OnTriggerExit2D(Collider2D other) {
		if (other.GetComponent<SafeZone>() is {})
			safeZone = null;
	}

	#endregion

	#region States

	void SwitchState(BaseState newState) {
		currentState.LeaveState();
		currentState = newState;
		newState.EnterState();
	}
	public bool IsInSafeZone(){
		if(safeZone is null){
			return false;
		}
		return safeZone.IsInSafeZone;
	}

	abstract class BaseStatePlayerMovement : BaseState {
		protected PlayerMovement playerMovement;

		public BaseStatePlayerMovement(PlayerMovement playerMovement) {
			this.playerMovement = playerMovement;
		}
	}

	class NormalMovementState : BaseStatePlayerMovement {
		public NormalMovementState(PlayerMovement playerMovement) : base(playerMovement) {}

		public override BaseState? FixedUpdateState() {
			playerMovement.rb.velocity = (Vector3)playerMovement.moveDirection * playerMovement.stats.movementSpeed;

			return null;
		}
	}

	class JumpingMovementState : BaseStatePlayerMovement {
		readonly float duration;
		readonly Vector3 target;

		Vector3 startPosition;
		float startTime;

		public JumpingMovementState(PlayerMovement playerMovement, float duration, Vector3 target) : base(playerMovement) {
			this.duration = duration;
			this.target = target;
		}

		public override void EnterState() {
			startPosition = playerMovement.transform.position;
			startTime = Time.time;
		}

		public override BaseState? FixedUpdateState() {
			float currentTime = Time.time - startTime;
			if (currentTime >= duration)
				return Transition();

			playerMovement.rb.MovePosition(Vector3.Lerp(
				startPosition,
				target,
				ModifyLerpTime(currentTime / duration)
			));

			return null;
		}

		protected virtual BaseState Transition() => new NormalMovementState(playerMovement);
		protected virtual float ModifyLerpTime(float t) => t;
	}

	class EnterSafeZoneMovementState : JumpingMovementState {
		readonly SafeZone safeZone;

		public EnterSafeZoneMovementState(PlayerMovement playerMovement, SafeZone safeZone) : base(playerMovement, safeZone.Stats.JumpDuration, safeZone.transform.position) {
			this.safeZone = safeZone;
		}

		public override void EnterState() {
			base.EnterState();

			safeZone.EnterSafeZone();
			playerMovement.rb.SetEnabled(false);
		}

		protected override BaseState Transition() => new ImmobileMovementState(playerMovement);

		protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t);
	}

	class ExitSafeZoneMovementState : JumpingMovementState {
		readonly SafeZone safeZone;

		public ExitSafeZoneMovementState(PlayerMovement playerMovement, SafeZone safeZone, Vector2 direction) : base(playerMovement, safeZone.Stats.JumpDuration, safeZone.GetOutsidePosition(direction)) {
			this.safeZone = safeZone;
		}

		public override void LeaveState() {
			base.EnterState();

			safeZone.ExitSafeZone();
			playerMovement.rb.SetEnabled(true);
		}

		protected override float ModifyLerpTime(float t) => safeZone.Stats.JumpSpeedCurve.Evaluate(t);
	}

	class ImmobileMovementState : BaseStatePlayerMovement {
		public ImmobileMovementState(PlayerMovement playerMovement) : base(playerMovement) {}
		public override void EnterState() {
			base.EnterState();

			if (!playerMovement.rb.isKinematic)
				Debug.LogWarning("Rigidbody should probably be kinematic when immobile (when in safe zone).");
		}
#if UNITY_EDITOR
		public override void OnDrawGizmos() {
			if (playerMovement.safeZone is null)
				return;

			Vector3 dropPosition = playerMovement.safeZone.GetOutsidePosition(playerMovement.moveDirection);
			bool canJump = playerMovement.moveDirection.magnitude >= playerMovement.safeZone.Stats.MinJumpJoystickValue;
			Gizmos.color = canJump ? Color.green : Color.red;
			Gizmos.DrawLine(playerMovement.transform.position, dropPosition);
			if (canJump)
				Gizmos.DrawSphere(dropPosition, .5f);
			else
				Gizmos.DrawWireSphere(dropPosition, .5f);
		}
#endif
	}

	#endregion
}