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

public class PlayerController : MonoBehaviour {



    public AudioClip BounceSound, lose, win;

    //components
    WaveController wc;


    public AudioSource audioSourceForAll;
    //float
    public float hitSpeed;
	private float groundheight = 1.0f;
    float distanceFromGround;
    //int   
    public static float totalSpeed = 1.5f;

    public Vector3 startPos;

    //bool
    public bool airborne, rebounding, dead, facingRight, waveCooled;
    public int playerNumber;

    string jumpButton = "P1 Jump";
    string xAxis = "P1 Horizontal";
    string yAxis = "P1 Vertical";

	//Vector3
	Vector2 JacobVelocity;
	float yVelocity;
	Vector2 inputForce;
	float inputJump;
	float previousY;
    public Vector3 groundNormal;

	private Animator ani;

	// Use this for initialization
	void Start () 
	{
        //

		ani = GetComponent<Animator> ();
		ani.logWarnings = false;
        wc = GameObject.FindGameObjectWithTag("Trampoline").GetComponent<WaveController>();
		wc.RegisterPlayerTransform (transform);
        waveCooled = true;
        startPos = transform.position;        
        switch (playerNumber)
        {
            case 1:
                jumpButton = "P1 Jump";
                xAxis = "P1 Horizontal";
                yAxis = "P1 Vertical";
                break;
            case 2:
                jumpButton = "P2 Jump";
                xAxis = "P2 Horizontal";
                yAxis = "P2 Vertical";
                break;
            case 3:
                jumpButton = "P3 Jump";
                xAxis = "P3 Horizontal";
                yAxis = "P3 Vertical";
                break;
            case 4:
                jumpButton = "P4 Jump";
                xAxis = "P4 Horizontal";
                yAxis = "P4 Vertical";
                break;
        }



	}
	
	// Update is called once per frame
	void Update () {
		
		if(!facingRight && Input.GetAxis(xAxis) > 0.1f)
        {
            facingRight = true;
        }
		else if(Input.GetAxis(xAxis) < -0.1f)
        {
            facingRight = false;
        }

        GetComponent<SpriteRenderer>().flipX = facingRight;

		yVelocity -= 9.81f * totalSpeed * Time.deltaTime;
		HadynMovement ();
        if ((Vector3.zero - new Vector3(transform.position.x, 0, transform.position.z)).sqrMagnitude < 100)
        {
            JacobGamePhysics();
            //PlayerMovement();
        }else
        {
            JacobDeadPhysics();
            //PlayerMovement();
          
            //Dead.
        }

		transform.position += new Vector3(JacobVelocity.x, yVelocity, JacobVelocity.y) * Time.deltaTime;    

		ani.SetFloat("yVelocity", yVelocity);
		ani.SetFloat("yPosistion", transform.position.y);
		ani.SetFloat("yBlendTreeVel", (yVelocity/Mathf.Abs(hitSpeed))*10f);  	
    }
   
    void JacobDeadPhysics()
    {
        //gravity
        if (transform.position.y <= groundheight)
		{
            //JacobVelocity = Vector3.zero;
            JacobVelocity = Vector2.zero;
			yVelocity = 0f;
            if(transform.position.y != groundheight)
            {
                transform.position= new Vector3(transform.position.x, groundheight, transform.position.z);
            }
            if (!dead)
            {
                dead = true;
               	ani.SetBool("Dead", true);
                Camera.main.GetComponent<GameManager>().PlayerKilled();
                audioSourceForAll.PlayOneShot(lose);    
            }
        }
        else
		{
			//JacobVelocity += ((Vector3.down * 9.81f) * totalSpeed) * Time.deltaTime;
            
        }

        //transform.position += JacobVelocity * Time.deltaTime;
        JacobVelocity = Vector2.ClampMagnitude(JacobVelocity, 5f);
		transform.position += new Vector3(JacobVelocity.x, yVelocity, JacobVelocity.y) * Time.deltaTime;
    }

    void JacobGamePhysics() //best physics.
    {

		WaveController.WaveInfo wave = wc.GetWaveAtPosition (new Vector3 (transform.position.x, 0, transform.position.z));
		float waveHeight = wave.height+1.5f;
		Vector3 waveNormal = wave.normal;

		//gravity
		//JacobVelocity += ((Vector3.down*9.81f) * totalSpeed) * Time.deltaTime;

        
        //trampolinePhysics
        if (transform.position.y <= waveHeight)
        {
			Debug.DrawRay (transform.position, Vector3.up * (transform.position.y - waveHeight));

			if (airborne == true) 
			{
				// became grounded this frame
				hitSpeed = yVelocity;
                audioSourceForAll.PlayOneShot(BounceSound);

			}
			airborne = false;

            JacobVelocity *= 0.95f;
            JacobVelocity += wave.pushDir * wave.height * 2.5f * Mathf.Abs(transform.position.y-waveHeight) * totalSpeed * Time.deltaTime;
			yVelocity += 100f*Mathf.Abs(transform.position.y-waveHeight)*totalSpeed * Time.deltaTime;
                     
        }
        else
        {
            JacobVelocity += inputForce * Time.deltaTime;
			if (airborne == false) 
			{
				// became airborne this frame

				if (waveCooled)
				{
					waveCooled = false;
					wc.MakeWave(new Vector3(transform.position.x, 0, transform.position.z), Mathf.Abs(hitSpeed/totalSpeed)*0.2f);
					Invoke("resetWaveCooldown", 1f);
				}
				yVelocity = Mathf.Max(yVelocity, 2.0f);					
			}
            hitSpeed = 0;       
            airborne = true;
		}

		yVelocity += inputJump * Time.deltaTime;

        yVelocity = Mathf.Clamp(yVelocity, -14, 12);
    }

    void resetWaveCooldown()
    {
        waveCooled = true;
    }

	void HadynMovement()
	{
		//Vector3 dir;
		inputForce = Vector2.ClampMagnitude(Vector2.right * Input.GetAxis(xAxis) + Vector2.up * Input.GetAxis(yAxis), 1.0f) * 10.0f;

		inputJump = (Input.GetButton (jumpButton)?-1f:0f) * 9.81f * totalSpeed;
	}

    void PlayerMovement()
    {
        //Player movement
        if (airborne)
		{
			//Vector3 dir;
			Vector2 dir2D;
			dir2D = Vector2.ClampMagnitude(Vector2.right * Input.GetAxis(xAxis) + Vector2.up * Input.GetAxis(yAxis), 1.0f);
            if (rebounding)
            {

				//dir = Vector3.ClampMagnitude((Vector3.forward * Input.GetAxis(yAxis)) + (Vector3.right * Input.GetAxis(xAxis)), 1.0f);
                //dir *= (6*totalSpeed);
				dir2D *= (6*totalSpeed);
                rebounding = false;
                hitSpeed = 0;
            }
            else
			{
//				dir = Vector3.ClampMagnitude((Vector3.forward * Input.GetAxis(yAxis)) + (Vector3.right * Input.GetAxis(xAxis)), 1.0f);
//				dir *= (2*totalSpeed);
				dir2D *= (2*totalSpeed);
			}

            //JacobVelocity += dir * 20f * Time.deltaTime;
            JacobVelocity += dir2D * 5f * Time.deltaTime;
			//JacobVelocity = Vector3.Lerp(JacobVelocity, new Vector3(dir.x, yVelocity, dir.z), 1f);// (dir * Time.deltaTime);
        }
        else { 
            rebounding = true;
        }


        if (Input.GetButton(jumpButton))
		{
			//JacobVelocity += (Vector3.down*9.81f*totalSpeed)*Time.deltaTime;
			yVelocity -= 9.81f*totalSpeed*Time.deltaTime;
        }
    }
}