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

public class PlayerJetpack : MonoBehaviour
{
    private Rigidbody2D rigidBody;

    public Animator animUpperbody;
    public Animator animLowerbody;

    public ParticleSystem particlesystem;

    [SerializeField]
    public Slider jetpackSlider;

    private const float max = 100;
    private float current;

    [SerializeField]
    private float useRate = 50; // Per second
    [SerializeField]
    private float regenrate = 10; // Per second

    [SerializeField]
    private float jetpackForce = 80;
    [SerializeField]
    public float maxJetpackSpeed = 22f;             // The fastest the player can travel in the y axis.

    public bool depleted;
    private bool jetpacking;

	[SerializeField]
    private AudioClip jetpackSound;

	private bool playingJetpackAudio;

    void Start()
    {
        rigidBody = GetComponent<Rigidbody2D>();
        //animUpperbody = GetComponent<Animator>();
        //animLowerbody = GetComponent<Animator>();
//		particlesystem = GetComponent<ParticleSystem>();
		jetpackSound = Instantiate(AudioManager.Instance.getSound(AudioManager.Sounds.JETPACK_THRUST).SoundEffect) as AudioClip;

    }

    private void OnEnable()
    {
        current = max;
        UpdateUI();
    }

    void Update()
    {
        if (jetpacking)
        {
            if (!depleted)
            {
                // Use the stuffz
                current -= useRate * Time.deltaTime;
            }
            else
            {
                jetpacking = false;
            }

            if (current <= 0)
            {
                current = 0;
                jetpacking = false;
				Debug.Log("Depleted");
                depleted = true;
            }
        }

        if (!jetpacking)
        {
            current += regenrate * Time.deltaTime;

            if (current > max)
            {
                current = max;
            }

            if (current >= 30)
            {
                depleted = false;
            }
        }

        UpdateUI();
    }

    private void UpdateUI()
    {
        if(jetpackSlider)
            jetpackSlider.value = current /100;
    }

    private void FixedUpdate()
    {
        animLowerbody.SetBool("Jetpacking", jetpacking);

        if (jetpacking && !depleted)
        {

			// Play sound
			if (!playingJetpackAudio)
			{
//                Debug.Log("THRUST SOUND == null: "+(soundBoard.EngineMainThrust == null ? "true" : "false"));
//                audio.clip = mainBoosterAudio;
                GetComponent<AudioSource>().clip = jetpackSound;
				GetComponent<AudioSource>().Play();
				GetComponent<AudioSource>().mute= false;
				playingJetpackAudio = true;
			}


            rigidBody.AddForce(new Vector2(0, jetpackForce), ForceMode2D.Force);
			particlesystem.enableEmission = true;

            if (rigidBody.velocity.y > maxJetpackSpeed)
                rigidBody.velocity = new Vector2(rigidBody.velocity.x, Mathf.Sign(rigidBody.velocity.y) * maxJetpackSpeed);
        }
        else {

			if (playingJetpackAudio)
                {
				playingJetpackAudio = false;
                }
                if (GetComponent<AudioSource>().mute == false)
                {
				    GetComponent<AudioSource>().mute= true;
                }

			particlesystem.enableEmission = false;
        }
         
    }

    public void setJetpacking(bool jp)
    {
        jetpacking = jp;
    }


}
