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

public class NoteGenerator : MonoBehaviour
{
	public enum Direction
	{
		Up,
		Down,
		Left,
		Right,
		Z,
		A,
		Length
	};

	public enum NoteType
	{
		Eight,
		EightD,
		Quarter,
		Sixteenth,
		Treble,
		Length
	}

	public GameObject noteImage;
    public Transform canvas;
    public float waitTime;
    public float speed;
    public int amount;
    public bool hasFinished = false;
	public Sprite[] sprites;
	public NoteCollider checkNotes;
	[Range(0f,1f)]
	public float winThreshold;

	private GameObject temp;
	private NoteController noteController;
	private ResourceType resourceType;

	public AudioClip createNewItem;

	/*void Start()
    {
        createNotes();
    }
	*/
    IEnumerator GenerateNotes()
    {
        for (int count = 1; count <= amount; count++)
        {
			CreateNewNote();
			AudioManager.Instance.Play(createNewItem);
			yield return new WaitForSeconds(waitTime);
		}
        hasFinished = true;
    }

	private void CreateNewNote() {
		temp = Instantiate(noteImage, transform.position, Quaternion.identity, canvas);
		noteController = temp.GetComponent<NoteController>();
		noteController.noteSpeed = speed;
		noteController.keyDirection = (NoteGenerator.Direction)(Random.Range(0, (int)NoteGenerator.Direction.Length));
		noteController.noteType = (NoteGenerator.NoteType)(Random.Range(0, (int)NoteGenerator.NoteType.Length));
		noteController.GetComponent<Image>().sprite = sprites[(int)noteController.keyDirection];
	}

    public void createNotes(ResourceType type= ResourceType.FireBrigade)
    {
		resourceType = type;
		checkNotes.notesToCheckFor = amount*2;
		checkNotes.StartChecking();
		StartCoroutine(GenerateNotes());
    }

	public void DoneChecked(int score) {
		float temp = ((float) score / (float) amount);
		if (temp >= winThreshold) {
			KangarooGameManager.Instance.CreateNewMaterial(resourceType);
		}
	}
}
