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

public class QuestLVL2 : MonoBehaviour {

	public Text texto;
	public GameObject text;
	public int level;
	public GameObject marker;
	public GameObject tut;
	public GameObject toolbox;
	public GameObject finishObj;
	public Collider col;

	public bool completedTutorial;
	public bool tutTrigger;
	public bool isInTrigger;
	public bool canRepair;
	public bool hasTools;

	// Use this for initialization
	void Start () {
		text.SetActive (false);
		if(completedTutorial != true){
			texto.text = "Go to generator";
			text.SetActive (true);
			tutTrigger = false;
		}
	}
	
	// Update is called once per frame
	void Update () {


		if(Input.GetKeyDown(KeyCode.F) && isInTrigger){
			PickUp ();
		} else if (Input.GetKeyDown(KeyCode.F) && !completedTutorial && tutTrigger){
			completedTutorial = true;
			DoTutorial ();
		} else if (Input.GetKeyDown(KeyCode.F) && completedTutorial){
			tut.SetActive (false);
			text.SetActive (false);
		}
		if (Input.GetKeyDown(KeyCode.F) && canRepair && hasTools){
			Repair ();
			Debug.Log ("b");
		}
	}

	void OnTriggerEnter(Collider other){
		if(other.tag == "Quest0" && completedTutorial == false){
			texto.text = "Press F to continue";
			text.SetActive (true);
			tutTrigger = true;
		} else if (other.tag == "Quest1" && completedTutorial){
			isInTrigger = true;
			texto.text = "Press F to pick up tools";
			text.SetActive (true);
		} else if (other.tag == "Quest0" && hasTools){
			canRepair = true;
			texto.text = "Press F to repair";
			text.SetActive (true);
		} else if (other.tag == "House"){
			finish ();
		}
	}

	void OnTriggerExit(Collider other){
		if(other.tag == "Quest0" && completedTutorial){
			text.SetActive (false);
			tutTrigger = false;
			tut.SetActive (false);
		} else if (other.tag == "Quest1"){
			isInTrigger = false;
			text.SetActive (false);
			tut.SetActive (false);
		}
	}

	void PickUp(){
		Destroy (toolbox);
		hasTools = true;
		text.SetActive (false);
	}

	void DoTutorial(){
		tut.SetActive (true);
		completedTutorial = true;
		marker.SetActive (false);
	}

	void Repair(){
		level = 2;
		PlayerPrefs.SetInt ("level", level);
		col.isTrigger = true;
		texto.text = "Enter the house";
		text.SetActive (true);
	}

	void finish(){
		level = 2;
		PlayerPrefs.SetInt ("level", level);
		finishObj.SetActive (true);
	}
}
