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

public enum WaveType {Player,Enemy};

public class Wave : MonoBehaviour {

	private Vector3 waveRotation;
	private AudioSource audioPlayer;

	public WaveType type;
	public int level;
	public Direction direction;
	public float velocity;
	public float bounds;
	public Renderer waveRender;
	//public float playerCBL;
	//public float enemyCBL;


	//attaches Wave srcipt to prefab
	static public void CreateNewWave(GameObject prefab, WaveType t, int lev, Direction d, float velo, float b){
		Wave w = prefab.AddComponent<Wave>(); //new Wave();
		w.type = t;
		w.level = lev;
		w.direction = d;
		w.velocity = velo;
		w.bounds = b;
		//w.waveRender = prefab.GetComponent<Renderer>();
		//w.ChangeLevelMat();
	}

	// Use this for initialization
	void Start () {
		this.gameObject.transform.localScale = new Vector3 (4, 4, 1);
		waveRender = this.gameObject.GetComponent<Renderer>();
		waveRotation = this.gameObject.transform.rotation.eulerAngles;
		audioPlayer = this.gameObject.GetComponent<AudioSource> ();
		if (type == WaveType.Enemy) {
			audioPlayer.clip = GameController.waveShotAudio [4];
		} else if (type == WaveType.Player) {
			audioPlayer.clip = GameController.waveShotAudio [level - 1];
		}
		SetWaveQuad ();
		audioPlayer.Play ();
	}

	public int Level{
		get{return level;}
	}
	
	// Update is called once per frame
	void Update () {
		if (type == WaveType.Player) {
			if (OutOfBounds ()) {
				Reverb ();
			}
		} 
		else if (type == WaveType.Enemy) {
			if (OutOfBounds ()) {
				Destroy (this.gameObject);
			}
		}
		UpdatePosition ();
		//TODO: figure out what to do with waves when the player dies
	}

	private void SetWaveQuad(){
		if (type == WaveType.Enemy) {
			waveRender.material.mainTexture = GameController.enemyWaveTex [level-1];
			gameObject.GetComponent<BoxCollider> ().center = new Vector3 (GameController.enemyWaveCBL [level-1], 0, 0);
		}
		else if (type == WaveType.Player){
			waveRender.material.mainTexture = GameController.playerWaveTex [level-1];
			gameObject.GetComponent<BoxCollider> ().center = new Vector3 (GameController.playerWaveCBL, 0, 0);
		}
		waveRender.material.shader = Shader.Find ("Sprites/Default");
	}


	private void UpdatePosition(){
		float dis = velocity * Time.deltaTime;
		switch (direction) {
		case Direction.Up:
			transform.position += new Vector3 (-(dis), 0, 0);
			break;
		case Direction.Down:
			transform.position += new Vector3 (dis, 0, 0);
			break;
		case Direction.Left:
			transform.position += new Vector3 (0, 0, -(dis));
			break;
		case Direction.Right:
			transform.position += new Vector3 (0, 0, dis);
			break;

		}
	}

	//checks if the wave has reached the boundry
	//TODO: update to account for differnt X and Y bounds
	private bool OutOfBounds(){
		switch (direction) {
		case Direction.Up:
			if(transform.position.x <= bounds)
			{return true;}
			else{break;}

		case Direction.Down:
			if(transform.position.x >= bounds)
			{return true;}
			else{break;}

		case Direction.Left:
			if(transform.position.z <= bounds)
			{return true;}
			else{break;}

		case Direction.Right:
			if(transform.position.z >= bounds)
			{return true;}
			else{break;}
		}
		return false;
	}

	//reverses wave
	private void Reverb(){
		if (type == WaveType.Player) {
			type = WaveType.Enemy;
			this.gameObject.tag = "WaveEnemy";
			bounds = 0;
		}
		switch (direction) {
		case Direction.Up:
			direction = Direction.Down;
			break;

		case Direction.Down:
			direction = Direction.Up;
			break;

		case Direction.Left:
			direction = Direction.Right;
			break;

		case Direction.Right:
			direction = Direction.Left;
			break;
		}
		SetWaveQuad ();
		this.gameObject.transform.rotation = Quaternion.Euler (new Vector3 (waveRotation.x, waveRotation.y + 180, waveRotation.z));
		waveRotation = this.gameObject.transform.rotation.eulerAngles;
		//OffsetWave (this.gameObject.transform.position);
		audioPlayer.clip = GameController.reverdAudio;
		audioPlayer.Play ();
	}
	/*
	private void OffsetWave(Vector3 ogPos){
		Vector3 newPos = ogPos;
		switch (direction) {
		case Direction.Up:
			newPos = new Vector3 (ogPos.x + (waveRender.bounds.size.x / GameController.waveLevelOffsetDivider [level-1]), ogPos.y, ogPos.z);
			break;
		case Direction.Down:
			newPos = new Vector3 (ogPos.x - (waveRender.bounds.size.x / GameController.waveLevelOffsetDivider [level-1]), ogPos.y, ogPos.z);
			break;
		case Direction.Left:
			newPos = new Vector3 (ogPos.x , ogPos.y, ogPos.z - (waveRender.bounds.size.z / GameController.waveLevelOffsetDivider [level-1]));
			break;
		case Direction.Right:
			newPos = new Vector3 (ogPos.x , ogPos.y, ogPos.z + (waveRender.bounds.size.z / GameController.waveLevelOffsetDivider [level-1]));
			break;
		}
		this.gameObject.transform.position = newPos;
	}
	*/

	void OnTriggerEnter(Collider other){
		if (type == WaveType.Player) {
			if (other.gameObject.tag == "WaveEnemy") {
				Wave enemyWave = other.gameObject.GetComponent<Wave>(); 
				int enemyLevel; 
				enemyLevel = enemyWave.Level;
				enemyWave.Collision(level);
				Collision(enemyLevel);
			}
		}
		/*
		if (type == WaveType.Enemy) {
			if (other.gameObject.tag == "WavePlayer") {
				Wave playerWave = other.gameObject.GetComponent<Wave>();
				int playerLevel = playerWave.Level;
				playerWave.Collision(level);
				Collision(playerLevel);
			}
		}*/
	}

	private void Collision(int otherLevel){
		level -= otherLevel;
		if (level < 1) {
			if (type == WaveType.Enemy) {
				Instantiate (GameController.destroyedWave, this.gameObject.transform.position, this.gameObject.transform.rotation);
			}
			Destroy (this.gameObject);
		} 
		else {
			SetWaveQuad();
			audioPlayer.clip = GameController.waveShrinkAudio;
			audioPlayer.Play ();
		}
	}
}
