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

public class Shooting : MonoBehaviour {

	public Material laserMat;
	public bool p1 = true;
	RaycastHit hit;
	bool canShoot = true;
	public AudioClip shootSound;
	public Transform lastLaser;

	void Start () {
		
	}

	void Update () {
		if (p1) {
			if ((Input.GetAxisRaw ("SHorizontal") != 0 || Input.GetAxisRaw ("SVertical") != 0) && canShoot) {
				if (Physics.Raycast (transform.position, new Vector3 (Input.GetAxisRaw ("SHorizontal"), 0, Input.GetAxisRaw ("SVertical")), out hit)) {
					Debug.DrawLine (transform.position, hit.point);
					GameObject lnt = new GameObject  ("Laser");
					LineRenderer ln = lnt.AddComponent <LineRenderer> ();
					lnt.transform.position = (hit.point + transform.position) / 2;
					BoxCollider bc = lnt.AddComponent<BoxCollider> ();
					bc.size = new Vector3 (1, 1, hit.distance);
					bc.center = Vector3.zero;
					lnt.transform.LookAt (new Vector3 (Input.GetAxisRaw ("SHorizontal"), 0, Input.GetAxisRaw ("SVertical")) + lnt.transform.position);
					ln.material = laserMat;
					ln.SetPositions (new Vector3[]{ transform.position, hit.point});
					ln.startColor = Color.red;
					ln.endColor = Color.red;
					ln.startWidth = .5f;
					ln.endWidth = .5f;
					Destroy (lnt, .1f);
					if(hit.transform.name == "Player2") {
						GameManager.WinP1 ("laser");
					}
					if(hit.transform.name == "Mirror") {
						GameManager.WinP2 ("mirror");
					}
					if(hit.transform.GetComponent<Powerup>()) {
						Destroy (hit.transform.gameObject);
					}
					Camera.main.GetComponent<AudioSource> ().PlayOneShot (shootSound);
					GameManager.lastWave.p1Ghost.transform.LookAt(new Vector3 (Input.GetAxisRaw ("SHorizontal"), 0, Input.GetAxisRaw ("SVertical")) + GameManager.lastWave.p1Ghost.transform.position);
					GameManager.lastWave.p1Ghost.transform.rotation *= Quaternion.Euler (-90, 0, 90);
					GameManager.lastWave.HitPlayer1	 ();
					StartCoroutine (Cooldown ());
				}
			}
		} else {
			if ((Input.GetButton ("2SHorizontal") || Input.GetButton ("2SVertical")) && canShoot) {
				if (Physics.Raycast (transform.position, new Vector3 (Input.GetAxisRaw ("2SHorizontal"), 0, Input.GetAxisRaw ("2SVertical")), out hit)) {
					Debug.DrawLine (transform.position, hit.point);
					GameObject lnt = new GameObject ();
					LineRenderer ln = lnt.AddComponent <LineRenderer> ();
					ln.material = laserMat;
					ln.SetPositions (new Vector3[]{ transform.position, hit.point });
					ln.startColor = Color.red;
					ln.endColor = Color.red;
					ln.startWidth = .5f;
					ln.endWidth = .5f;
					Destroy (lnt, 0.1f);
					if (hit.transform.name == "Player1") {
						GameManager.WinP2 ("laser");
					}
					if (hit.transform.name == "Mirror") {
						GameManager.WinP1 ("mirror");
					}
					if (hit.transform.GetComponent<Powerup> ()) {
						Destroy (hit.transform.gameObject);
					}
					Camera.main.GetComponent<AudioSource> ().PlayOneShot (shootSound);
					GameManager.lastWave.p2Ghost.transform.LookAt (new Vector3 (Input.GetAxisRaw ("2SHorizontal"), 0, Input.GetAxisRaw ("2SVertical")) + GameManager.lastWave.p2Ghost.transform.position);
					GameManager.lastWave.p2Ghost.transform.rotation *= Quaternion.Euler (-90, 0, 90);
					GameManager.lastWave.HitPlayer2 ();
					StartCoroutine (Cooldown ());
				}
			}
		}
	}
	IEnumerator Cooldown() {
		canShoot = false;
		yield return new WaitForSeconds (2.1f);
		canShoot = true;
	}
}
