using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using Photon.Pun;

public class ForwardRocketScript : MonoBehaviourPunCallbacks {

	public RacerScript racer;
	public GameObject rocket;
	public bool rocketActive = false;
	public bool called = false;
	Vector3 velocity = Vector3.zero;
	public LayerMask hitMask, trackMask;

	// Start is called before the first frame update
	void Start() {
		if (!racer) {
			StartCoroutine (DestroySelf ());
		}
	}

	// Update is called once per frame
	void Update() {
		if (racer) {
			if (Input.GetKeyDown (KeyCode.LeftShift) || Input.GetKeyDown (KeyCode.RightShift)) {
				if (!called) {
					called = true;
					racer.itemCanvas.itemActivated = true;
					StartCoroutine (LaunchRocket ());
				}
			}
		}
	}

	void FixedUpdate() {
		
		if(!racer){

			RaycastHit wall;
			Physics.Raycast (transform.position + transform.forward, transform.forward, out wall, 1.5f, hitMask);


			RaycastHit floor;
			Physics.Raycast (new Vector3 (transform.position.x, transform.position.y + 1f, transform.position.z), -Vector3.up, out floor, Mathf.Infinity, trackMask);

			if (floor.transform) {
				transform.parent.up = floor.normal;
			}

			if (wall.transform) {
				transform.forward = Vector3.Reflect (transform.forward, wall.normal);
			}



			Vector3.SmoothDamp (transform.parent.position, transform.parent.position += transform.forward * 1.5f, ref velocity, 0.3f);
		}
	}

	void OnTriggerEnter (Collider col) {
		if (rocketActive) {
			if (col.transform.parent.GetChild (1).GetComponent<KartController> ()) {
				if(PhotonNetwork.IsConnected && !col.transform.parent.GetChild (1).GetComponent<KartController> ().photonView.IsMine){
					return;
				}
				StartCoroutine (RocketHit (col.transform.parent.GetChild (1).GetComponent<KartController> ()));
			} else if (col.transform.GetComponent<ForwardRocketScript> () && (!col.transform.parent.GetComponentInChildren<KartController>() || !col.GetComponentInChildren<KartController>())) {
				Destroy (transform.parent.gameObject);
			}
		}

	}

	IEnumerator DestroySelf () {
		yield return new WaitForSeconds (0.5f);
		rocketActive = true;
		yield return new WaitForSeconds (7f);
		if (!rocketActive) {
			yield break;
		} else {
			Destroy (transform.parent.gameObject);
		}
	}

	IEnumerator LaunchRocket () {
		if (PhotonNetwork.IsConnected) {
			GameObject forwardRocket = PhotonNetwork.Instantiate ("Bounce Rocket", racer.kart.transform.position, Quaternion.identity);
			forwardRocket.transform.GetChild (0).rotation = racer.kart.transform.rotation;
			yield return new WaitForSeconds (0.5f);
			forwardRocket.GetComponentInChildren<ForwardRocketScript> ().rocketActive = true;
		} else {
			GameObject forwardRocket = GameObject.Instantiate (rocket, racer.kart.transform.position, Quaternion.identity);
			forwardRocket.transform.GetChild (0).rotation = racer.kart.transform.rotation;
			yield return new WaitForSeconds (0.5f);
			forwardRocket.GetComponentInChildren<ForwardRocketScript> ().rocketActive = true;
		}
		called = false;
		racer.SetActiveItem (-1);
	}

	IEnumerator RocketHit (KartController kart) {
		rocketActive = false;
		GetComponent<MeshRenderer> ().enabled = false;
		GetComponent<Collider> ().enabled = false;
		if (PhotonNetwork.IsConnected) {
			photonView.RPC ("RocketCall", RpcTarget.OthersBuffered);
		}
		kart.acceleration = 0f;
		kart.transform.GetChild(0).GetChild(0).DOPunchPosition(transform.up * .3f, .3f, 5, 1);
		yield return new WaitForSeconds (1.5f);
		kart.transform.DOComplete();
		Debug.Log ("increase acceleration");
		kart.acceleration = 120f;
		Debug.Log (kart.acceleration);
		Destroy (transform.parent.gameObject);
	}

	[PunRPC]	
	public void RocketCall () {
		GetComponent<MeshRenderer> ().enabled = false;
		GetComponent<Collider> ().enabled = false;
		StartCoroutine (WaitToDelete ());
	}

	IEnumerator WaitToDelete () {
		yield return new WaitForSeconds (1.5f);
		Destroy (gameObject);
	}
}
