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


public class RideWave : MonoBehaviour {

	float amplitude;
	float fanOffsetX;
	public Vector3 initOffset;
	public FanWave fanWave;

	public static RideWave instance;

	List<GameObject> components;

	//BONUS MALUS PARAMS
	public bool shieldAcitve;
	public float speed;
	public int score;
	public int scoreMalus;
	public bool malusJump;
	public int scoreMultiplier { get { return fanWave.deletedMult; } set { fanWave.deletedMult = value; } }
	public float jumpBoost;


	float yOffset;
	bool jumping;
	bool canJump;


	void Start() {
		if(instance == null) {
			instance = this;
		}
		components = new List<GameObject>();
		jumping = false;
		malusJump = false;
		jumpBoost = 1f;
		fanWave = GameObject.Find("FanWave").GetComponent<FanWave>();
		amplitude = fanWave.amplitude;
		fanOffsetX = fanWave.fanOffsetX;
		scoreMalus = 0;
		scoreMultiplier = 1;
		foreach (Transform child in transform)
		{
			if(child.gameObject.tag.Equals("Guitar") || child.gameObject.tag.Equals("Drum") || child.gameObject.tag.Equals("Singer")) {
				components.Add(child.gameObject);
			}
		}
		AudioMgr.instance.initGame();
	}

	void Update () {

//		if(fanWave.fanDeleted % 100 == 0) {
//			speed *= 1.02f;
//		}

		score = fanWave.fanDeleted - (scoreMalus);
		if (score < 0) score = 0;
		bool _jump = Input.GetKeyDown(KeyCode.Space);
		canJump = yOffset <= 0f;
		if(_jump && !jumping && canJump && !malusJump) {
			fanWave.upHands(transform.position);
			jumping = true;
			StartCoroutine(jump());
		}
		float sin = Mathf.Sin(Time.time * speed);
		yOffset -= Mathf.Pow(Time.deltaTime, .6f);
		yOffset = Mathf.Clamp(yOffset, 0f, 100f);
		transform.position = Vector3.Lerp(transform.position, 
			new Vector3(Time.time * fanOffsetX * 1.9f * speed, (sin * amplitude) + yOffset, 0f), 1f) + initOffset;
		transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, (1f-Mathf.Abs(sin))*50f), 0.1f);
	}

	IEnumerator jump() {
		GetComponent<RandomSound>().playRandom();
		float time = 0f;
		while(time < (.34f * jumpBoost) ) {
			time += Time.deltaTime;
			if(jumping)
				yOffset += Mathf.Pow(time, .7f);
			yield return null;
		}
		jumping = false;
		//yOffset = 0f;
	}


	public IEnumerator coolDownNoJump(float time) {
		float _time = 0; 
		while(_time < time) {
			_time += Time.deltaTime;
			yield return null;
		}
		malusJump = false;
	}

	public IEnumerator coolScoreMultiPlier(float time) {
		float _time = 0; 
		while(_time < time) {
			_time += Time.deltaTime;
			yield return null;
		}
		scoreMultiplier = 1;
	}

	public IEnumerator coolDownJumpBoost(float time) {
		float _time = 0; 
		while(_time < time) {
			_time += Time.deltaTime;
			yield return null;
		}
		jumpBoost = 1f;
	}

	public IEnumerator removeShieldAfter(float time) {
		float _time = 0; 
		while(_time < time) {
			_time += Time.deltaTime;
			yield return null;
		}
		shieldAcitve = false;
	}

	public void killComponent() {
		int random = Random.Range(0, components.Count);
		GameObject comp = components[random];
		components.RemoveAt(random);
		comp.SetActive(false);

		string tag = comp.tag;
		AudioMgr.instance.muteTrack(tag);

		if(components.Count == 0) {
			LevelManager.instance.GameOverMenu();
		}
	}


}
