using System;
using UnityEngine;
using UnityEngine.InputSystem;

public class Engine : MonoBehaviour
{
    public RocketDataScript rocketData;

    private InputAction jumpAction;
    private InputAction thrustLeverAction;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        jumpAction = InputSystem.actions.FindAction("Jump");
        thrustLeverAction = InputSystem.actions.FindAction("ThrustLever");
    }

    // Update is called once per frame
    void Update()
    {
        if (jumpAction.IsPressed())
            Thrust();

        var lever = thrustLeverAction.ReadValue<float>();
        if (lever != 0)
        {
            var newLever = rocketData.ThrustLever + lever * rocketData.thrustLeverStep * Time.deltaTime;
            newLever = Mathf.Clamp(newLever, 0, 1);
            rocketData.SetThrustLever(newLever);
        }
    }

    private void Thrust()
    {
        if (rocketData.Fuel > 0)
        {
            rocketData.SetThrust(rocketData.Force);
            var rb = GetComponent<Rigidbody>();
            rb.AddForce(rocketData.Force * rocketData.ThrustLever * Time.deltaTime * transform.up);
            rocketData.AddFuel(-1 * rocketData.ThrustLever * Time.deltaTime);
        }
    }
}
