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

public class PlayerController : MonoBehaviour {

	[Header("Player Attributes")]
	private Rigidbody2D body;
	public float speed = 3.0f;
	public float jumpSpeed = 2.5f;
	public bool isGrounded = false;
	public float groundDistance = 0.25f;
	[Header("Double Jump")]
	public bool doubleJump = false;
	public int jumpCount;
	public float jumpCooldown = 0.2f;
	public Sprite effectSprite;
	public GameObject effect;
	public AudioClip bassTwang;
	[Header("Keyboard Dash")]
	public float dashTimer = 3.0f;
	public float dashSpeed = 85.0f;
	public bool dashEnable = false;
	public Sprite keySprite;
	public GameObject keyEffect;
	public AudioClip keyRoll;

	// Use this for initialization
	void Start () {

		body = GetComponent<Rigidbody2D>();

		effect = new GameObject("String");
		effect.AddComponent<SpriteRenderer>().sprite = effectSprite;
		effect.GetComponent<SpriteRenderer>().color = Color.white;
		effect.AddComponent<AudioSource>();
		effect.GetComponent<AudioSource>().clip = bassTwang;
		effect.GetComponent<AudioSource>().playOnAwake = false;
		effect.GetComponent<AudioSource> ().volume = 0.3f;
		effect.transform.localScale = new Vector3(0.03f,0.03f,1f);
		effect.SetActive(false);

		keyEffect = new GameObject("Board");
		keyEffect.AddComponent<SpriteRenderer>().sprite = keySprite;
		keyEffect.GetComponent<SpriteRenderer>().color = Color.white;
		keyEffect.AddComponent<AudioSource>();
		keyEffect.GetComponent<AudioSource>().clip = keyRoll;
		keyEffect.GetComponent<AudioSource> ().volume = 0.3f;
		keyEffect.GetComponent<AudioSource>().playOnAwake = false;
		keyEffect.transform.localScale = new Vector3(0.275f,0.1f,1f);
		keyEffect.SetActive(false);
	}

	void Update(){
		//Ground detection
		if(!Physics2D.Raycast(transform.position,Vector3.down,groundDistance,~(3<<8))){
			isGrounded = false;
			jumpCooldown -= Time.deltaTime;
		}
		else{
			isGrounded = true;
			jumpCooldown = 0f;
			if(doubleJump)
				jumpCount = 2;
			else
				jumpCount = 1;
		}

		//double jump checks
		if(jumpCooldown <= 0f)
			jumpCooldown = 0f;

		//dash timer checks
		if(dashTimer < 0f)
			dashTimer = 0f;
		else
			dashTimer -= Time.deltaTime;

	}
	
	// Update is called once per frame
	void FixedUpdate () {
		//horizontal movement
		body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y);

		//jumping movement
		if((isGrounded || jumpCount > 0) && jumpCooldown <= 0f && Input.GetButtonDown("Jump")){
			body.velocity = new Vector2(body.velocity.x,Input.GetAxis("Jump") * jumpSpeed);
			if(!isGrounded){
				effect.transform.position = new Vector3(transform.position.x,transform.position.y-0.225f,transform.position.z);
				effect.GetComponent<SpriteRenderer>().color = Color.white;
				effect.SetActive(true);
				effect.GetComponent<AudioSource>().PlayOneShot(bassTwang);
			}
			jumpCount -= 1;
			jumpCooldown = 0.2f;
		}

		if(effect.activeSelf == true)
			Guitar();

		//dashing movement
		if(Input.GetButtonDown("Fire1") && dashTimer <= 0f && dashEnable){
			if(Input.GetAxis("Horizontal") >= 0){
				body.AddForce(Vector2.right * dashSpeed, ForceMode2D.Impulse);
				keyEffect.transform.position = new Vector3(transform.position.x+0.8f,transform.position.y,transform.position.z);
			}
			else{
				body.AddForce(Vector2.left * dashSpeed, ForceMode2D.Impulse);
				keyEffect.transform.position = new Vector3(transform.position.x-0.8f,transform.position.y,transform.position.z);
			}
			dashTimer = 3.0f;
			keyEffect.GetComponent<SpriteRenderer>().color = Color.white;
			keyEffect.SetActive(true);
			keyEffect.GetComponent<AudioSource>().PlayOneShot(keyRoll);
		}
		Keyboard();
	}

	void Guitar(){
		if(effect.GetComponent<SpriteRenderer>().color.a >= 0f){
			effect.GetComponent<SpriteRenderer>().color = Color.Lerp(effect.GetComponent<SpriteRenderer>().color,new Color(255,255,255,0),0.1f);
		}
		if(effect.GetComponent<SpriteRenderer>().color.a == 0f){
			effect.SetActive(false);
			effect.transform.position = new Vector3(0,0,0);
		}
	}

	void Keyboard(){
		if(keyEffect.GetComponent<SpriteRenderer>().color.a >= 0f){
			keyEffect.GetComponent<SpriteRenderer>().color = Color.Lerp(keyEffect.GetComponent<SpriteRenderer>().color,new Color(255,255,255,0),0.025f);
		}
		if(keyEffect.GetComponent<SpriteRenderer>().color.a == 0f){
			keyEffect.SetActive(false);
			keyEffect.transform.position = new Vector3(0,0,0);
		}
	}
}
