using UnityEngine;
using System.Collections;

//public class WaveGenerator : MonoBehaviour
//{
//    public Wave wave;

//    public int frameCounter = 0;
//    public int intervalTime = 25;
//    public int curInterval = 0;
//    public int maxCount = 8;

//    public bool hitInterval = false;

//    new Renderer renderer;

//    void Start ()
//    {
//        renderer = GetComponent<Renderer>();
//	}
	
//	void FixedUpdate ()
//    {
//        hitInterval = false;
//        for(int i = 1; i < maxCount; i++)
//        {
//            if (frameCounter == intervalTime * i)
//            {
//                curInterval++;
//                hitInterval = true;

//                if (renderer)
//                {
//                    if (i % 2 != 0)
//                    {
//                        renderer.material.color = Color.red;
//                    }
//                    else
//                    {
//                        renderer.material.color = Color.white;
//                    }
//                }
//                break;
//            }
//        }
//        if(frameCounter >= intervalTime * maxCount)
//        {
//            GenerateWave();
//            hitInterval = true;

//            if (renderer)
//                renderer.material.color = Color.white;

//            frameCounter = 0;
//            curInterval = 0;
//        }
        
//        frameCounter++;
//	}

//    void GenerateWave()
//    {
//        print("Output Wave");
//        wave.Reset();
//        wave.transform.position = transform.position;
//        wave.gameObject.SetActive(true);
//        wave.circle.gameObject.SetActive(true);
//    }
//}

public class WaveGenerator : MonoBehaviour
{
    public GameObject waveObj;

    public Wave wave;

    public bool hitInterval = false;

    public float secs = 0;
    public float secsOverall = 0;

    public int currentInterval = 0;
    public int numOfIntervals = 8;
    public int bpm = 100;
    float intervalLength = 0.5f;

    [Space]
    public AudioSource sfxSource;
    public AudioClip sfx_pulse;
    public AudioClip sfx_beat;

    void Start()
    {
        wave = Instantiate(waveObj).GetComponent<Wave>();
    }

    void FixedUpdate()
    {
        intervalLength = (60.0f / (float)bpm);

        if(secs >= intervalLength)
        {
            secs = secs - intervalLength;
            hitInterval = true;
            currentInterval++;

            if (currentInterval == numOfIntervals)
            {
                currentInterval = 0;
                wave.Reset();

                wave.transform.position = transform.position;
                wave.gameObject.SetActive(true);
                wave.circle.gameObject.SetActive(true);
                if(sfxSource)
                    sfxSource.PlayOneShot(sfx_pulse);
            }
            else
            {
                if (sfxSource)
                    sfxSource.PlayOneShot(sfx_beat, 0.35f);
            }
        }
        else
        {
            hitInterval = false;
        }


        secs += Time.fixedDeltaTime;
        secsOverall += Time.fixedDeltaTime;
    }
}
