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

public class MusicPlayerScript : MonoBehaviour
{
    public int status;
    AudioSource aso;

    int toplay;
    float lastplayed;
    List<float> waits;

    public string[] notes;
    public string[] phrasings;
    public string instrument;

    public int waittype;

    int lastnoteidx;
    int lastphrasingidx;

    public float volume;

    public void Awake()
    {
        //wait = 0.25f;
        waits = new List<float>();
        aso = GetComponent<AudioSource>();
        lastnoteidx = -1;
        lastphrasingidx = -1;
    }

    public void PlayThisMany(int many)
    {
        // many is 3.
        //toplay = (int)Mathf.Max(many, toplay);
        
        if (waittype == 0)
        {
            int r = Random.Range(0, 3);
            if (r == 0) waits = new List<float>(new float[] { 0, 0.5f, 0.5f });
            else if (r == 1) waits = new List<float>(new float[] { 0, 0.5f, 0.25f, 0.25f });
            else waits = new List<float>(new float[] { 0, 0.25f, 0.25f, 0.25f, 0.25f });
        }

        if (waittype == 1)
        { // ****..*.*.......
            //0,0.125f,
            //r = 2;
            int r = Random.Range(0, 3);
            if (r == 0) waits = new List<float>(new float[] { 0, 0.125f,0.125f,0.125f,0.125f});
            //else if (r == 1) waits = new List<float>(new float[] { 0, 0.166f, 0.166f, 0.166f, 0.166f, 0.166f, 0.166f });
            else if (r == 1) waits = new List<float>(new float[] { 0, 0.25f, 0.0833f,0.0833f, 0.0833f,0.25f });
            else if (r == 2) waits = new List<float>(new float[] { 0,0.0833f, 0.0833f, 0.25f });
            for (int i = 0; i < waits.Count; i++)
            {
                waits[i] = waits[i] * 0.75f;
            }
        }
        toplay = waits.Count;
        //toplay = waits.Count;
    }

    string NoteToWord(int note)
    {
        return notes[note]; // lel
        /*if (note == 0) return "as4";
        if (note == 1) return "cs5";
        if (note == 2) return "ds5";
        if (note == 3) return "fs5";
        if (note == 4) return "gs5";
        if (note == 5) return "as5";
        return "";*/
    }

    AudioClip GetClip(string basse, string notte, string typpe)
    {
        //print("sounds" + "/" + basse + "/" + notte + "/" + typpe);
        return (AudioClip)Resources.Load<AudioClip>("sounds" + "/" + basse + "/" + notte + "/" + typpe);
            
    }

    public void Update()
    {
        if (waits.Count > 0 && Time.time - lastplayed >= waits[0]*0.75f)
        {
            waits.RemoveAt(0);
            lastplayed = Time.time;
            int note = Random.Range(0, notes.Length);
            if (lastnoteidx != -1 && Random.Range(0, 3) != 0)
            {
                while (Mathf.Abs(note - lastnoteidx) != 1)
                    note = Random.Range(0, notes.Length);
            }
            else {
                while (Mathf.Abs(note - lastnoteidx) != 2)
                    note = Random.Range(0, notes.Length);
            }
            lastnoteidx = note;

            int typpe = Random.Range(0, phrasings.Length);
            if (lastphrasingidx != -1 && Random.Range(0, 3) != 0)
            {
                typpe = lastphrasingidx;
            }

            lastphrasingidx = typpe;
            //int r = Random.Range(0, 10);
            //float length = 0;
            //if (r < 7) length = 0.5f;
            //else length = 1;
            

            AudioClip clip = GetClip(instrument, NoteToWord(note), phrasings[typpe]);
            //print(length);
            //print(clip.length);
            //clip = MakeSubclip(clip, 0, (clip.length * Random.Range(0,1f)));
            aso.PlayOneShot(clip, 1);
        }
    }

    // supa slow
    private AudioClip MakeSubclip(AudioClip clip, float start, float stop)
    {
        /* Create a new audio clip */
        int frequency = clip.frequency;
        float timeLength = stop - start;
        int samplesLength = (int)(frequency * timeLength);
        AudioClip newClip = AudioClip.Create(clip.name + "-sub", samplesLength, 1, frequency, false);
        /* Create a temporary buffer for the samples */
        float[] data = new float[samplesLength];
        /* Get the data from the original clip */
        clip.GetData(data, (int)(frequency * start));
        /* Transfer the data to the new clip */
        newClip.SetData(data, 0);
        /* Return the sub clip */
        return newClip;
    }
}
