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

public class QuestLVL1 : MonoBehaviour {

	public GameObject axe;
	public Animator chop;
	public bool completedTutorial;
	public bool hasAxe;
	public bool isInTrigger;
	public bool canChop;
	public bool choppedTheTree;
	public bool tutTrigger;
	public Text texto;
	public GameObject text;
	public GameObject tut;
	public GameObject marker;
	public GameObject finishObj;
	public int level;


	// Use this for initialization
	void Start () {
		text.SetActive (false);
		if(completedTutorial != true){
			texto.text = "Go to houses front door";
			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);
		}
		if (Input.GetKeyDown(KeyCode.F) && canChop && hasAxe){
			ChopDown ();
			Debug.Log ("b");
		}

		if(completedTutorial){
			marker.SetActive (false);
		}
	}

	void OnTriggerEnter(Collider other){
		if (other.tag == "Quest2" && completedTutorial == false){
			texto.text = "Press F to continue";
			text.SetActive (true);
			tutTrigger = true;
		} else if(other.tag == "Quest1" && completedTutorial){
			//make button visible
			texto.text = "Press F to pickup axe";
			text.SetActive (true);
			isInTrigger = true;
		} else if (other.tag == "Quest2" && hasAxe){
			texto.text = "Press F to chop down the tree";
			text.SetActive (true);
			canChop = true;
		} else if  (other.tag == "House"){
			Finish ();
		}
	}

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

	public void PickUp(){
		Destroy (axe);
		hasAxe = true;
	}

	public void ChopDown(){
		Debug.Log ("a");
		chop.SetInteger ("Play", 1);
	}

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

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