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

public class FakeItemBoxScript : MonoBehaviourPunCallbacks {

	public RacerScript racer;
	public GameObject mine;
	public bool mineActive = false;
	public bool called = false;

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

	// 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 (ActivateMine ());
				}
			}
		} else {
			transform.RotateAround (transform.position, Vector3.right, 30f * Time.deltaTime);
			transform.GetChild(0).LookAt (2 * transform.GetChild(0).position - Camera.main.transform.position, Vector3.down);
		}
	}

	void OnTriggerEnter (Collider col) {
		if (mineActive) {
			if (col.transform.parent.GetChild (1).GetComponent<KartController> ()) {
				if(PhotonNetwork.IsConnected && !col.transform.parent.GetChild (1).GetComponent<KartController> ().photonView.IsMine){
					return;
				}
				StartCoroutine (MineHit (col.transform.parent.GetChild (1).GetComponent<KartController> ()));
			}
		}
	}

	IEnumerator ActivateMine () {
		if (PhotonNetwork.IsConnected) {
			GameObject rocketMine = PhotonNetwork.Instantiate ("Fake Item Box", racer.kart.transform.position + (racer.kart.kartNormal.up * 0.75f), mine.transform.rotation);
			yield return new WaitForSeconds (0.5f);
			rocketMine.GetComponent<FakeItemBoxScript> ().mineActive = true;
		} else {
			GameObject rocketMine = GameObject.Instantiate (mine, racer.kart.transform.position + (racer.kart.kartNormal.up * 0.75f), mine.transform.rotation);
			yield return new WaitForSeconds (0.5f);
			rocketMine.GetComponent<FakeItemBoxScript> ().mineActive = true;
		}
		called = false;
		racer.SetActiveItem (-1);
	}

	IEnumerator MineHit (KartController kart) {
		mineActive = false;
		GetComponent<MeshRenderer> ().enabled = false;
		GetComponent<Collider> ().enabled = false;
		transform.GetChild (0).gameObject.SetActive(false);

		if (PhotonNetwork.IsConnected) {
			photonView.RPC ("DeactivationCall", 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();
		kart.acceleration = 120f;
		Destroy (gameObject);
	}

	[PunRPC]	
	public void DeactivationCall () {
		mineActive = false;
		GetComponent<MeshRenderer> ().enabled = false;
		GetComponent<Collider> ().enabled = false;
		transform.GetChild (0).gameObject.SetActive(false);
		StartCoroutine (WaitToDelete ());
	}

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

	IEnumerator DelayActivation () {
		yield return new WaitForSeconds (0.5f);
		mineActive = true;
	}
}
