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

public class NoiseVolumeControl : MonoBehaviour {

    public AudioManager audMgr;

    public List<CharacterOne> enemies;
    public List<float> diff = new List<float>();
    public float frequencyVariance = .25f;
    public float noiseFallRate = .05f;
    public int isTrue = 0;

	// Use this for initialization
	void Start () {

        noiseFallRate = enemies[0].noiseFallRate;
        StartCoroutine(FindDifference());
	}
	
	// Update is called once per frame
	void Update () {

    }

    IEnumerator FindDifference()
    {
        int removeLocation = 0;
        bool takeOut = false;
        while (true)
        {

            for (int i = 0; i < enemies.Count; i++)
            {
                if (RadioControls.radioFrequency > enemies[i].pRadioFrequency - frequencyVariance &&
                    RadioControls.radioFrequency < enemies[i].pRadioFrequency + frequencyVariance)
                {
                    isTrue++;
                }

                if(!enemies[i].alive)
                {
                    takeOut = true;
                    removeLocation = i;
                }

            }

            if (takeOut)
            {
                //enemies.RemoveAt(removeLocation);
                takeOut = false;
            }

            if (isTrue > 0)
                audMgr.noiseSource.volume -= noiseFallRate * Time.deltaTime;
            else if (isTrue == 0 && !CharacterOne.radioClicked && audMgr.noiseSource.volume < .12f)
            {
                audMgr.noiseSource.volume += noiseFallRate * Time.deltaTime;
            }

            isTrue = 0;

            yield return new WaitForSeconds(1 / 60f);
        }
    }

}
