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

public class PlayerMovement : MonoBehaviour {

	public bool isGrounded; 
	public Transform groundCheck; 
	public float groundRadius = 0.25f; 
	public LayerMask whatIsGround;
   
	public bool jumped; 					/* !?!?!?!?!?!?!?! IF ANIMATION JUMP !?!?!?!?!?!?!?!? */ 
	public float jumpForce = 350f; 

	public float speed = 6.0f; 

	private Rigidbody2D rb2d; 
	private Animator anim;
    public bool coolDown = false;
    bool jumpHasHappened = false;
    private Text texti;


	void Awake()
	{
		rb2d = GetComponent<Rigidbody2D> (); 
		anim = GetComponent<Animator> (); 
	}


	void FixedUpdate()
	{
        float moveHorizontal = Input.GetAxis("Horizontal");
        Vector2 movement = new Vector3(moveHorizontal, 0);
        rb2d.velocity = new Vector2(movement.x * (speed), rb2d.velocity.y);
        if (moveHorizontal >=  -1 && moveHorizontal <= 1 && moveHorizontal != 0 & coolDown == false) { AudioSource auda = GetComponent<AudioSource>();  AudioSource.PlayClipAtPoint(gameObject.GetComponent<CreateObject>().audioclips[2], transform.position); coolDown = true; Invoke("SetCoolDown", 0.5f); GetComponent<Animator>().SetBool("isWalking", true); }
       if (moveHorizontal == 0) { GetComponent<Animator>().SetBool("isWalking", false); }
       if (moveHorizontal >0) { transform.localScale = new Vector3(-1, 1, 1); }
        if (moveHorizontal < 0) { transform.localScale = new Vector3(1, 1, 1); }
    }

    void SetCoolDown()
    {
        coolDown = false;
    }

	void Update ()
	{
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
        /////////////////////////////////////////	MOVEMENT	////////////////////////////////////////////////////////////////////////
        //float moveHorizontal, gets players horizontal input.
        
        // float moveVertical, gets players vertical input.
       
        
        /*rigi.velocity = movement * (speed * time.deltatime), takes the vector we made from earlier (determining the direction) and time it by a public speed float, allowing us to control at which speed
         * it will move along the direction given by the vector. In addition, we multiply the speed by Time.deltatime, to make sure the gameObject moves at the same speed, no matter the framerate the game
         is running at. This works because Time.deltatime is the amount of time between 2 frames in seconds.. So if you have 10 frames per second Time.deltatime = 0.1. When you take speed * time it took between
         frames, that would mean that if you have 0.1 deltatime (10fps) it will take speed f.ex 10. times 0.1. However, if you only had 5 frames, deltatime will be 0.2. Making the object move twice as fast. This
         way the object moves at the same speed no matter your fps. Since it will move f.ex 10*vector per second. And that would be a lot faster, than 5*vector per second(if you only had 5fps.) however,
         by slowing the people with higher fps down, it makes sure that despite the code being run several more times than people with lower fps, it wont move faster, because the speed is lower. Creating an 
         independent speed from fps. However, deltatime should not be used when you use addforce instead of transform.velocity.*/
        
        //doesn't actually change the position, simply leaves it as is (sets it to own position) as the code above is responsible for  changing the position. Instead, it uses Mathf.clamp to constrain the movement, within boundaries.
        

        /////////////////////////////////////////	JUMP	////////////////////////////////////////////////////////////////////////////
        if (Input.GetButtonDown ("Jump") && isGrounded) 
		{
            //rb2d.AddForce (Vector2.up * jumpForce); 
            rb2d.velocity = rb2d.velocity + Vector2.up * jumpForce;
            // !!?!?!?!?!?!?!??!?!?!!?!? ANIMATION ?!?!!??!?!?!?!?!?!?!?!?!??!?!
            //anim.SetTrigger("JumpTrigger"); 
            //jumped = true; 
            AudioSource audi = GetComponent<AudioSource>();
            AudioSource.PlayClipAtPoint(GetComponent<CreateObject>().audioclips[1], transform.position);
            if (jumpHasHappened == false)
            {
                texti = GameObject.FindObjectOfType<Text>();
                texti.text = "Oh praise the cows, Lucky for me I was wearing my superplated space pants.";
                texti.fontSize = 80;
                Invoke("ClearText", 3f);
                jumpHasHappened = true;
                Instantiate(texti);
            }
		}
	}

    void ClearText()
    {
        texti.text = "";
    }



}
