using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerJump : MonoBehaviour
{
    public PlayerHeightEntity playerHeight;
    public InputActionReference jump;
    public InputActionReference battleJump;
    public PlayerInput playerControls;
    public float spriteHeightForce;
    public float heightForce;
    public AnimationCurve spriteHeightCurve;
    public AnimationCurve heightCurve;
    public bool isJumping;
    public bool onFloor;


    public void FloorChecks()
    {
        spriteHeightCurve = new AnimationCurve(new Keyframe(0, playerHeight.currentSpriteFloorHeight, 1, 1, 0.35f, 0), 
            new Keyframe(1, playerHeight.currentSpriteFloorHeight + 1, 0, 0, 0.7f, 0));
        heightCurve = new AnimationCurve(new Keyframe(0, playerHeight.currentFloorHeight, 1, 1, 0.35f, 0), 
            new Keyframe(1, playerHeight.currentFloorHeight + 5, 0, 0, 0.7f, 0));
        if(playerHeight.spriteHeight <= playerHeight.currentSpriteFloorHeight)
        {
            playerHeight.spriteHeight = playerHeight.currentSpriteFloorHeight;
            playerHeight.spriteRenderer.transform.localPosition -= new Vector3(0, playerHeight.spriteRenderer.transform.localPosition.y 
                - playerHeight.currentSpriteFloorHeight);
            playerHeight.entityHeight = playerHeight.currentFloorHeight;
            onFloor = true; 
        }
        else
        {
            onFloor = false;
        }
    }

    public void Update()
    {
        FloorChecks();
        if(playerControls.currentActionMap == playerControls.actions.FindActionMap("Overworld") 
            && jump.action.WasPressedThisFrame())
        {
            Jump(false, spriteHeightForce, heightForce, jump);
        }
        if(playerControls.currentActionMap == playerControls.actions.FindActionMap("Battle") 
            && battleJump.action.WasPressedThisFrame())
        {
            Jump(false, spriteHeightForce, heightForce, battleJump);
        }
    }
    
    public void Jump(bool isExtreme, float spriteHeightForce, float heightForce, InputActionReference correctAction)
    {
        if(!isJumping && onFloor)
        {
            StartCoroutine(JumpCo(spriteHeightForce, heightForce, correctAction));
            if(isExtreme)
            {
            }
        }
    }

    public IEnumerator JumpCo(float spriteHeightForce, float heightForce, InputActionReference correctAction)
    {
        InputActionReference jumpAction = correctAction;
        isJumping = true;
        float jumpStartTime = Time.time;
        AnimationCurve startingspriteHeightCurve = spriteHeightCurve;
        AnimationCurve startingHeightCurve = heightCurve;
        while(isJumping)
        {
            float jumpCompletionPercentage = (Time.time - jumpStartTime) / playerHeight.jumpDuration;
            jumpCompletionPercentage = Mathf.Clamp01(jumpCompletionPercentage);

            if(jumpAction.action.IsPressed())
            {
                playerHeight.spriteRenderer.transform.localPosition = new Vector3
                    (0, startingspriteHeightCurve.Evaluate(jumpCompletionPercentage) * spriteHeightForce);
                playerHeight.entityHeight = startingHeightCurve.Evaluate(jumpCompletionPercentage) * heightForce;
            }
            if(jumpAction.action.WasReleasedThisFrame())
            {
                break;
            }

            if(jumpCompletionPercentage == 1f)
            {
                break;
            }
            yield return null;
        }
        isJumping = false;   
    }
}