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

public class UI : MonoBehaviour {

	public CanvasGroup beatGroup;
	public Plane plane;
	public Scrollbar barPitch;
	public Scrollbar barRoll;
	public Text lblAltitude;
	public Text lblAngle;

	public Text lblTitle;
	public Text lblDescription;
	public Image imgHelp;

	public AudioClip sndCheckpoint;
	public AudioClip sndBeat;

	public Tutorial tutorial;

	public void Info(string title, string desc = null, Sprite sprite = null) {
		if(title != null) lblTitle.text = title;
		if(desc != null) lblDescription.text = desc;
		if(sprite != null) imgHelp.sprite = sprite;
	}

	IEnumerator WaitSfx(AudioSource asrc) {
		yield return new WaitUntil(() => !asrc.isPlaying);
		Destroy(asrc);
	}

	public void Play(AudioClip clip, float volume = 1f) {
		if(clip == null)
			return;
		var asrc = gameObject.AddComponent<AudioSource>();
		asrc.clip = clip;
		asrc.playOnAwake = true;
		asrc.volume = volume;
		asrc.Play();
		StartCoroutine(WaitSfx(asrc));
	}

	// Use this for initialization
	void Start () {
		tutorial = FindObjectOfType<Tutorial>();
	}

	// Update is called once per frame
	void Update () {
		var t = 1f-Mathf.Clamp01((Game.instance.beatPeriod-Game.instance.nextBeatTime) / 0.5f);
		beatGroup.alpha = t;//Mathf.Clamp01(Game.instance.nextBeatTime / Game.instance.beatPeriod);
		barRoll.value = plane.steerRoll * 0.5f + 0.5f;
		barPitch.value = plane.steerAltitude * 0.5f + 0.5f;
		lblAltitude.text = "Altitude: " + (plane.transform.position.y * 100).ToString("0.") + "m";
		lblAngle.text = "Roll: " + plane.rollAngle.ToString("0.") + "°";
	}
}
