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

public class Wave : MonoBehaviour {

	public Transform graphics;
	private Transform player1;
	private Transform player2;
	RaycastHit hit;
	float speed = 2;
	public bool hasHit1 = false;
	public bool hasHit2 = false;
	public GameObject p1Ghost;
	public GameObject p2Ghost;
	Vector3 lastPos1;
	Vector3 lastPos2;
	public Material p1Mat;
	public Material p2Mat;

	void Start() {
		player1 = GameObject.Find ("Player1").transform;
		player2 = GameObject.Find ("Player2").transform;
		p1Ghost = GameObject.Find ("Player1Ghost");
		p2Ghost = GameObject.Find ("Player2Ghost");
	}

	void Update() {
		/*if (hasHit1) {
			player1.GetComponent<MeshRenderer> ().material.color = Color.LerpUnclamped(player1.GetComponent<MeshRenderer> ().material.color, new Color(0, 255, 0), 0.1f);
		}
		if (hasHit1) {
			player2.GetComponent<MeshRenderer> ().material.color = Color.LerpUnclamped(player2.GetComponent<MeshRenderer> ().material.color, new Color(0, 255, 0), 0.1f);
		}*/
	}

	void FixedUpdate () {
		graphics.localScale += Vector3.one * speed * Time.fixedDeltaTime;
		speed *= 1.125f;
		if (graphics.GetComponent<Collider>().bounds.Intersects(player1.GetComponent<Collider>().bounds) && !hasHit1) {
			hasHit1 = true;
			if (!player1.GetComponent<Powerups> ().inStealth) {
				StartCoroutine (HitPlayer1 ());
			}
		}
		if (graphics.GetComponent<Collider>().bounds.Intersects(player2.GetComponent<Collider>().bounds) && !hasHit2) {
			hasHit2 = true;
			if (!player2.GetComponent<Powerups> ().inStealth) {
				StartCoroutine (HitPlayer2 ());
			}
		}
		/*if (!hasHit1) {
			Vector3 r = player.position - transform.position;
			Ray ray = new Ray (transform.position, r);
			if (Physics.Raycast (ray, out hit, graphics.localScale.z / 2)) {
				if (hit.transform.tag == "Player") {
					
				}
			}
		}*/
	}
	public IEnumerator HitPlayer1() {
		//player1.GetComponent<MeshRenderer> ().material.color = new Color (0, 255, 0, 255);
		//p1Ghost.SetActive(true);
		lastPos1 = p1Ghost.transform.position;
		p1Ghost.transform.position = player1.position;

		p1Ghost.transform.GetChild (0).GetComponent<LineRenderer> ().numPositions = player1.GetComponent<Movement> ().pos.ToArray ().Length;
		p1Ghost.transform.GetChild(0).GetComponent<LineRenderer>().SetPositions (player1.GetComponent<Movement>().pos.ToArray());
		player1.GetComponent<Movement> ().pos = new List<Vector3> ();
		player1.GetComponent<Movement> ().pos.Add (player1.position);


		yield return new WaitForSeconds (0.5f);
		//player1.GetComponent<MeshRenderer> ().material.color = new Color (0, 0, 0, 0);
		if(hasHit2){
			Destroy (gameObject);
		}
	}
	public IEnumerator HitPlayer2() {
		//player2.GetComponent<MeshRenderer> ().material.color = new Color (0, 0, 255, 255);
		//p2Ghost.SetActive(true);
		lastPos2 = p2Ghost.transform.position;
		p2Ghost.transform.position = player2.position;

		p2Ghost.transform.GetChild (0).GetComponent<LineRenderer> ().numPositions = player2.GetComponent<Movement> ().pos.ToArray ().Length;
		p2Ghost.transform.GetChild(0).GetComponent<LineRenderer>().SetPositions (player2.GetComponent<Movement>().pos.ToArray());
		player2.GetComponent<Movement> ().pos = new List<Vector3>();
		player2.GetComponent<Movement> ().pos.Add (player2.position);


		yield return new WaitForSeconds (0.5f);
		//player2.GetComponent<MeshRenderer> ().material.color = new Color (0, 0, 0, 0);
		if(hasHit1){
			Destroy (gameObject);
		}
	}
}
