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

public class FlameFriend_2 : MonoBehaviour {

	public GameObject lightSprite;
	//public GameObject buddiesHome;

	//mini flame var
	public GameObject flameCharge; 
	int ChargesAllowedOut = 3;
	public int chargesOut = 0; 
	Vector3 _currentChargePos; 

	// Little Lighter Var
	float outTimer = 0f;
	public float howLongICanBeOut = 10f;
	Vector3 movePos;
	Vector3 startPos; 
	public float timeToPointS;
	float currentTime;




	// Use this for initialization
	void Start () {
		GameObject[] lightFogs = GameObject.FindGameObjectsWithTag("LightFog");
		if (lightFogs.Length > 0)
		{
			lightFogs[0].GetComponent<LightFogManager>().AddLightTarget(gameObject);
		}
	}// end start
	
	// Update is called once per frame
	void Update () {
		
		if(Input.GetKeyUp(KeyCode.Mouse0)){
			Debug.Log ("moving to point");
			outTimer = howLongICanBeOut;
			this.GetComponent<FollowTarget> ().enabled = false;
			movePos = Camera.main.ScreenToWorldPoint (Input.mousePosition); 
			movePos.z = 0; 
			startPos = transform.position;
			currentTime = timeToPointS; 
			transform.position = movePos;  
			Debug.DrawLine (transform.position, movePos, Color.red);
			//MoveToClick ();
		}
		else if(Input.GetKeyUp(KeyCode.Mouse1)){
			Debug.Log ("shooting flames");
			if(chargesOut < ChargesAllowedOut){
				_currentChargePos = Camera.main.ScreenToWorldPoint (Input.mousePosition); 
				Debug.Log ("adding a flame");
				GameObject spawnFlames = GameObject.Instantiate (flameCharge);
				spawnFlames.GetComponent<ChargeScript>().SetCurrentPos(transform, _currentChargePos);
				chargesOut++;
			}


			//MoveLightToClick ();
		}




		if(currentTime > 0){
			currentTime -= Time.deltaTime; 
			if (currentTime < 0) { currentTime = 0; }
			Vector3 nextPos = Vector3.Lerp(startPos, movePos, (timeToPointS - currentTime)/timeToPointS); 
			this.transform.position = nextPos;
		}
		if (outTimer > 0 ){
			outTimer -= Time.deltaTime; 
			if (outTimer < 0) { 
				ComeHome (); 
			}
		}
	}//end update

	/*void MoveToClick(){
		Debug.Log ("moving to point");
		this.GetComponent<FollowTarget> ().enabled = false;


		//Vector3 dd = Camera.main.ScreenToViewportPoint (Input.mousePosition);
		//transform.position = dd;
		//Debug.Log (dd); 	

	}//end MoveToClick */

	/* void MoveLightToClick(){
		Debug.Log ("shooting to point");
	}//end MoveLightToClick */

	void ComeHome(){
		this.GetComponent<FollowTarget> ().enabled = true;
	}//end ComeHome

	public void SetChargeCount(){
		chargesOut--;
		Debug.Log (chargesOut);
	}//SetChargeCount

}//end class