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

public class PlayerMovement : MonoBehaviour
{
    //TODO: Set keyboard controls for P2
    //0 for p1 1 for p2
    public int playerID;
    [Header("The names of the inputs that control the player as set in the input manager")]
    [Tooltip("0 is horizontal, 1 is vertical, 2 is fire, 3 is reload, 4 is switch")]
    public string[] inputNames;
    [Header("The Maximum Velocity the player can reach")]
    public int maxVelocity;
    //This is the velocity that will be applied to the player on the physics step
    private Vector3 movementVector;
    
    private Rigidbody rb;
    public GameObject thrusters;
    public GameObject thrusterEffect;
    public float thrusterMultiplier;
    private bool resettingThrusters=false;
    // Use this for initialization
    void Start()
    {

        rb = this.GetComponent<Rigidbody>();
        if (playerID == 0)
        {
            inputNames = InputManager.INPUTMANAGER.player1Inputs;
        }
        else
        {
            inputNames = InputManager.INPUTMANAGER.player2Inputs;
        }
    }

    // Update is called once per frame
    void Update()
    {
        movementVector = new Vector3(Input.GetAxis(inputNames[0]), Input.GetAxis(inputNames[1]));

        if (Input.GetAxisRaw(inputNames[1]) != 0)
        {
            StopCoroutine(ResetThrusters(0));
            resettingThrusters = false;
            thrusters.transform.Rotate(Vector3.right * Input.GetAxis(inputNames[1]) * -15);
            float angle = thrusters.transform.localEulerAngles.x;
            angle = (angle > 180) ? angle - 360 : angle;
            if (angle >= 45)
            {
                thrusters.transform.localEulerAngles = new Vector3(45, 0, 0);
            }
            else if (angle <= -45)
            {
                thrusters.transform.localEulerAngles = new Vector3(-45, 0, 0);
            }
        }
        else
        {
            if(resettingThrusters==false)
            {
                resettingThrusters = true;
                float x = thrusters.transform.localEulerAngles.x;
                float angle = thrusters.transform.localEulerAngles.x;
                angle = (angle > 180) ? angle - 360 : angle;
                StartCoroutine(ResetThrusters(angle));
            }
        }

        if (Input.GetAxisRaw(inputNames[0]) == 0)
        {
            thrusterMultiplier = 0.3f;
        }
        else if (Input.GetAxisRaw(inputNames[0]) > 0)
        {
            thrusterMultiplier = 0.4f;
        }
        else if(Input.GetAxisRaw(inputNames[0])<0)
        {
            thrusterMultiplier = 0.2f;
        }
        thrusterEffect.transform.GetChild(0).transform.localScale = new Vector3(thrusterMultiplier, thrusterMultiplier, thrusterMultiplier);
        thrusterEffect.transform.GetChild(1).transform.localScale = new Vector3(thrusterMultiplier, thrusterMultiplier, thrusterMultiplier);
    }

    private void FixedUpdate()
    {
        rb.velocity = movementVector * maxVelocity;
    }

    IEnumerator ResetThrusters(float x)
    {
        float a;
        for (float i = 0; i < 1; i+=0.1f)
        {
            a = Mathf.Lerp(Mathf.Abs(x),0,i);
            if(x<0)
            {
                thrusters.transform.localEulerAngles = new Vector3(a*-1, 0, 0);
            }
            else
            {
                thrusters.transform.localEulerAngles = new Vector3(a, 0, 0);
            }
            
            yield return new WaitForEndOfFrame();
        }
        resettingThrusters = false;
    }
}
