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

public class Reactor : MonoBehaviour {

	public float temperature;
	private float startTemp;
	public float dangerousTemperature;
	public RectTransform tempMeter;
	public GameObject alarmLight;
	public float heatingSpeed;
	public AudioSource alarmAudio;

	public string timeOfDeath;

	public bool meltdown;

	// Use this for initialization
	void Start ()
	{

		alarmLight.SetActive (false);
		meltdown = false;
		startTemp = temperature;
	}
	
	// Update is called once per frame
	void Update ()
	{
		temperature = Mathf.Clamp (temperature, startTemp, Mathf.Infinity);
		tempMeter.rotation = new Quaternion (0, 0, -temperature, 1);

		if (temperature > dangerousTemperature)
		{
			alarmLight.SetActive (true);

			if(alarmAudio.isPlaying == false)
				alarmAudio.PlayOneShot (alarmAudio.clip);
		}
		else if (temperature < dangerousTemperature)
		{
			alarmLight.SetActive (false);
			alarmAudio.Stop ();
		}

		// DEATH!
		if (temperature > 1)
		{
			meltdown = true;
			timeOfDeath = Time.time.ToString ();
			FindObjectOfType<ScoreManager> ().ShowFinalScore ();
		}
	}
}
