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

public class Tutorial : MonoBehaviour {
	UI ui;
	public GameObject theCheckpoint;
	public GameObject allCheckpoints;
	public AudioClip sndNext;

	void Info(string title, string desc = null, Sprite sprite = null) {
		ui.Play(sndNext, 0.24f);
		ui.Info(title, desc, sprite);
	}

	public float TimeDelay = 10f;
	public float ShortTimeDelay = 2f;

	void OnDestroy() {
		//Hacky
		ui.tutorial = null;
	}

	// Use this for initialization
	IEnumerator Start () {
		ui = Game.instance.ui;
		var plane = Game.instance.plane;
		Info(
			"Welcome to Quartz Airlines"
			, "Our planes rely entirely on karma magic to operate.\nYour job is managing passenger moods to control it."
			);

		yield return new WaitForSeconds(TimeDelay);
		Info(
			"Steering left"
			, "The black seats are used to steer left. Try DRAGGING some passengers' NEGATIVITY to that area."
			);
		yield return new WaitForSeconds(ShortTimeDelay);
		yield return new WaitUntil(() => plane.rollAngle < -2f);
		Info(
			"Steering right"
			, "Great! now steer right with red seats. Cyan and magenta seats change altitude."
			);
		yield return new WaitForSeconds(ShortTimeDelay);
		yield return new WaitUntil(() => plane.rollAngle > 2f);
		Info(
			"Archetypes"
			, "Let me add you a baby, which cries for a while. These archetypical passengers have special behaviors."
			);
		var grid = FindObjectOfType<PlaneGrid>();
		var avail = grid.map
			.OfType<Seat>()
			.Where(s => s && s.usable && s.entity == null)
			.OrderBy(x => Random.value)
			.First();
		grid.AddEntity(Instantiate(grid.prefabEntBaby), avail);
		yield return new WaitForSeconds(TimeDelay);
		Info(
			"Points"
			, "Collect the 'flying magenta squares' for points. Let me add one for you."
			);

		var currPoints = Game.instance.checkpoints;
		theCheckpoint.transform.position = Vector3.one * 1000f;
		while(Game.instance.checkpoints == currPoints) {
			var fdist = Vector3.Dot(theCheckpoint.transform.position - plane.transform.position, plane.transform.forward);
			if(fdist < -5f || fdist > 150f) {
				theCheckpoint.transform.position =
					plane.transform.position
					+ Vector3.ProjectOnPlane(plane.transform.forward, Vector3.up).normalized * 100f
					+ Vector3.ProjectOnPlane(plane.transform.right, Vector3.up).normalized * 5f;
			}


			yield return null;
		}
		Info(
			"Archetypes"
			, "'Bros' tend to be a annoyance. Keep them separated (or join them for chaotic steering!)"
			);
		var avail2 = grid.map
			.OfType<Seat>()
			.Where(s => s && s.usable && s.entity == null)
			.OrderBy(x => Random.value)
			.Take(2);
		foreach(var broSeat in avail2)
			grid.AddEntity(Instantiate(grid.prefabEntBro), broSeat);
		yield return new WaitForSeconds(TimeDelay);

		//Info(
		//	"Landing"
		//	, "After collecting checkpoints, try to land the plane in a horizontal surface."
		//	);
		//yield return new WaitForSeconds(TimeDelay);

		allCheckpoints.SetActive(true);
		Info(
			"Good luck!"
			, ""
			);
		yield return new WaitForSeconds(TimeDelay);


		//yield return new WaitUntil()
		yield return null;
		ui.tutorial = null;
		//Destroy(gameObject);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
