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

public class PowerUpHoverMovement : HoverMovement
{
    // Residual Thrust variables to handle the "glide" after the gameObject has moved
    public float residualThrustStep;
    public float residualThrustMax;
    public float residualThrustTimeTickMax;
    public float residualThrustDecayRate;
    public float residualThrustIdleDecayRate;

    private float _residualThrust;
    private float _residualThrustCurrentTime;

    // RNG Variables used for the fake AI (for testing) movement
    private float _rngVert;
    private float _rngHorz;

    // Start is called before the first frame update
    private void Start()
    {
        _residualThrust = 0.0f;
        _residualThrustCurrentTime = 0.0f;

        _rngHorz = 0.0f;
        _rngVert = 0.0f;

        BaseOnStart();
        // Custom start stuff can go here
    }

    /// <summary>
    /// Calculates some RNG movement for the attached core.
    /// </summary>
    /// <param name="currentTimeStep">The time unit currently used, typically deltatime or deltafixed time depending on if its the fixed update or not.</param>
    private void DoRNG(float currentTimeStep)
    {
        // Cores dont move at the moment but the movescript can be used above if they need to orbit around the blackhole
        _rngHorz = 0;
        _rngVert = 0;
    }

    /// <summary>
    /// Processes the inputs needed to move the gameobject, the turn direction, thrust direction and any residual thrust calculations so that we dont just stop in place.
    /// </summary>
    /// <param name="currentTimeStep">The time unit currently used, typically deltatime or deltafixed time depending on if its the fixed update or not.</param>
    protected override void ProcessPreMovement(float currentTimeStep)
    {
        // Check if the movement has been frozen
        if (!freezeMovement)
        {
            // Get the horz and vert values for the left/right, up/down movement
            float horzInput = _rngHorz;
            float vertInput = _rngVert;

            // Turn Right
            if (horzInput > 0)
            {
                _turnDirection = 1;
            }
            // Turn Left
            else if (horzInput < 0)
            {
                _turnDirection = -1;
            }
            else
            {
                _turnDirection = 0;
            }

            // Forward/Thrust
            if (vertInput > 0)
            {
                _thrusting = true;
                _thrustDirection = 1;
                // If our momentum isnt full keep adding to the value
                if (_residualThrust < residualThrustMax)
                {
                    _residualThrust += residualThrustStep * currentTimeStep;
                }
            }
            // Backwards/Breaking
            else if (vertInput < 0)
            {
                // If we have momentum drain it faster when hitting backwards
                if (_residualThrust > 0.0f)
                {
                    if (_residualThrustCurrentTime >= residualThrustTimeTickMax)
                    {
                        _residualThrust -= residualThrustDecayRate;
                    }
                    _thrustDirection = _residualThrust / residualThrustMax;
                }
                else
                {
                    _thrusting = true;
                    _thrustDirection = -1;
                }
            }
            // Continue movement if there is momentum remaining
            else
            {
                if (_residualThrust > 0.0f)
                {
                    if (_residualThrustCurrentTime >= residualThrustTimeTickMax)
                    {
                        _residualThrust -= residualThrustIdleDecayRate;
                    }
                    _thrustDirection = _residualThrust / residualThrustMax;
                }
                else
                {
                    _thrusting = false;
                    _thrustDirection = 0;
                }
            }

            // Clamp the thrust to 0 and the max value
            _residualThrust = Mathf.Clamp(_residualThrust, 0, residualThrustMax);
            if (_residualThrustCurrentTime >= residualThrustTimeTickMax)
            {
                _residualThrustCurrentTime = 0.0f;
            }
            else
            {
                _residualThrustCurrentTime += currentTimeStep;
            }

            // Run any base class stuff
            base.ProcessPreMovement(currentTimeStep);
        }
    }

    private void FixedUpdate()
    {
        if (!GameEngine.isPaused)
        {
            if (objectIsActive)
            {
                //ProcessJump();
            }
        }
    }

    // Update is called once per frame
    private void Update()
    {
        if (!GameEngine.isPaused)
        {
            if (objectIsActive)
            {
                ProcessWorkingStartValues(Time.deltaTime);
                DoRNG(Time.deltaTime);
                ProcessPreMovement(Time.deltaTime);
                ProcessCurrentMovement(Time.deltaTime);
                ProcessPostMovement();
            }
        }
    }
}
