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

public class PlayerMovement : MonoBehaviour {

    public float speed = 6f;            // The speed that the player will move at.
    public bool isJumping = false;
    public bool walking = false;
    public int playerNumber = 1;

    Vector3 movement;                   // The vector to store the direction of the player's movement.
    Animator anim;                      // Reference to the animator component.
    Rigidbody playerRigidbody;          // Reference to the player's rigidbody.

	public bool invulnerable = false;
	public int vidas = 3;
	public bool muerto = false;

    public ParticleSystem stepParticleSystem;

    public Image v1, v2, v3;
    

    void Awake()
    {
        // Set up references.
        anim = GetComponent<Animator>();
        playerRigidbody = GetComponent<Rigidbody>();
        if(playerNumber == 1)
        {
            Turning(new Vector3(1f, 0, -1f));
            transform.position = new Vector3(-6, 0, - 2);
        }
        else if(playerNumber == 2)
        {
            Turning(new Vector3(-1f, 0, -1f));
            transform.position = new Vector3(6, 0, - 2);
        }

    }


    void FixedUpdate()
    {
        // Store the input axes.

        float h = 0;
        float v = 0;

        if (playerNumber == 1 && !muerto)
        {
            h = Input.GetAxisRaw("HorizontalJ1");
            v = Input.GetAxisRaw("VerticalJ1");
        }

        else if (playerNumber == 2 && !muerto)
        {
            h = Input.GetAxisRaw("HorizontalJ2");
            v = Input.GetAxisRaw("VerticalJ2");
        }

        movement.Set(h, 0f, v);

        // Move the player around the scene.
        Move(movement);

        // Turn the player to face the mouse cursor.
        Turning(movement);

        // Animate the player.
        Animating(movement);
    }

    void Move(Vector3 movement)
    {
        if(movement.magnitude >= 0.1f)
        {
            // Normalise the movement vector and make it proportional to the speed per second.
            movement = movement.normalized * speed * Time.deltaTime;

            // Move the player to it's current position plus the movement.
            playerRigidbody.MovePosition(transform.position + movement);

            //Aumentamos la emisión de polvo al andar;
            var em = stepParticleSystem.emission;
            em.rateOverTime = 8f;
        }
        else
        {
            //Eliminamos la emisión de polvo al andar;
            var em = stepParticleSystem.emission;
            em.rateOverTime = 0f;
        }

    }

    void Turning(Vector3 movement)
    {
        if (movement.magnitude >= 0.1f)
        {
            // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
            Quaternion newRotation = Quaternion.LookRotation(movement);

            // Set the player's rotation to this new rotation.
            playerRigidbody.MoveRotation(newRotation);
        }

    }

    
    void Animating(Vector3 movement)
    {
        walking = false;

        if (movement.magnitude >= 0.1f)
        {
            walking = true;           
        }

        if (Input.GetKeyDown(KeyCode.Space) && playerNumber == 1)
        {
            isJumping = true;
            Invoke("stopJump", 1f);
            anim.SetTrigger("Jump");
        }

        if (Input.GetKeyDown(KeyCode.RightControl) && playerNumber == 2)
        {
            isJumping = true;
            Invoke("stopJump", 1f);
            anim.SetTrigger("Jump");
        }

        // Tell the animator whether or not the player is walking.
        anim.SetBool("IsRunning", true);
    }

	private void ResetInvulnerable(){
		invulnerable = false;
	} 

    void OnTriggerEnter(Collider col)
    {
        //Si el objeto con el que colisiona es un rayo.
        if(col.gameObject.CompareTag("Rayo")){
            if(isJumping == false && !invulnerable)
            {
				vidas--;
                if(vidas == 2)
                {
                    v1.enabled = false;
                }
                else if(vidas == 1)
                {
                    v2.enabled = false;
                }
				else if (vidas == 0) {

                    v3.enabled = false;
                    muerto = true;
					//Muere
					print ("Muere");
					anim.SetTrigger ("Die");
					playerRigidbody.constraints = RigidbodyConstraints.None;
					//Eliminamos la emisión de polvo al morir;
					var em = stepParticleSystem.emission;
					em.rateOverTime = 0f;

					GameManager.instance.PlayerWin (playerNumber);
				}
				invulnerable = true;
				Invoke ("ResetInvulnerable", 1.5f);
            }
        }
    }

    private void stopJump()
    {
        isJumping = false;
    }
    
}
