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

public class CharacterOne : MonoBehaviour {

    [Range(1, 16)]
    public int currLocation = 1;
    [Range(0, 4)]
    public float pRadioFrequency = 1;
    public bool alive = true;

    public float waitForNextMove = 6f;
    public float frequencyVariance = .3f;
    public float noiseFallRate = .08f;

    bool canMoveDown = false;
    bool canMoveUp = false;
    bool canMoveRight = false;
    bool canMoveLeft = false;

    public AudioManager audMgr;
    public GameObject voodoo;

    public static bool radioClicked = false;

    // Use this for initialization
    void Start () {
        currLocation = Mathf.Clamp(currLocation, 1, 16);
        pRadioFrequency = Mathf.Clamp(pRadioFrequency, 0, 4);
        StartCoroutine("characterMovement");
    }
	
	// Update is called once per frame
	void Update ()
    {

        //if (RadioControls.radioFrequency > pRadioFrequency - frequencyVariance &&
        //    RadioControls.radioFrequency < pRadioFrequency + frequencyVariance)
        //{
        //    audMgr.noiseSource.volume -= noiseFallRate * Time.deltaTime;
        //}
        //else if (audMgr.noiseSource.volume < .2f && !radioClicked)
        //    audMgr.noiseSource.volume += noiseFallRate * Time.deltaTime;

        if (!alive)
        {
            
            this.gameObject.SetActive(false);
        }	
	}


    IEnumerator characterMovement()
    {
        
        while (true)
        {
            switch (currLocation)
            {
                case 1:
                    setPossibleDirections(false, true, false, true);
                    
                    break;
                case 2:
                    setPossibleDirections(true, true, false, true);
                    
                    break;
                case 3:
                    setPossibleDirections(true, true, false, true);
                    
                    break;
                case 4:
                    setPossibleDirections(true, false, false, true);
                    break;
                case 5:
                    setPossibleDirections(false, true, true, false);
                    
                    break;
                case 6:
                    setPossibleDirections(false, true, true, true);
                   
                    break;
                case 7:
                    setPossibleDirections(true, false, true, true);
                    
                    break;
                case 8:
                    setPossibleDirections(false, false, true, true);
                    
                    break;
                case 9:
                    setPossibleDirections(false, true, false, true);
                   
                    break;
                case 10:
                    setPossibleDirections(true, true, true, true);
                    
                    break;
                case 11:
                    setPossibleDirections(true, true, true, false);
   
                    break;
                case 12:
                    setPossibleDirections(true, false, true, true);

                    break;
                case 13:
                    setPossibleDirections(false, true, true, false);

                    break;
                case 14:
                    setPossibleDirections(true, true, true, false);
                    break;
                case 15:
                    setPossibleDirections(true, true, false, false);
                    break;
                case 16:
                    setPossibleDirections(true, false, true, false);
                    break;

            }
            RandomLocation(canMoveLeft, canMoveRight, canMoveUp, canMoveDown);
            yield return new WaitForSecondsRealtime(waitForNextMove);

        }
    }

    public void setPossibleDirections(bool left, bool right, bool up, bool down)
    {
        canMoveLeft = left;
        canMoveRight = right;
        canMoveUp = up;
        canMoveDown = down;
    }

    void RandomLocation(bool left, bool right, bool up, bool down)
    {
        List<int> directions = new List<int>();
        if(left)
        {
            directions.Add(-1);
            
        }   
        if(right)
        {
            directions.Add(1);
        }   
        if(up)
        {
            directions.Add(-4);
        }   
        if(down)
        {
            directions.Add(4);
        }

        int movement = directions[(int)Random.Range(0, directions.Count)];

        currLocation += movement;
        if (!radioClicked)
        {
            playDirectionAudio(movement);
        }

    }

    void playDirectionAudio(int moved)
    {
        if (RadioControls.radioFrequency > pRadioFrequency - frequencyVariance &&
            RadioControls.radioFrequency < pRadioFrequency + frequencyVariance)
        {
            //audMgr.StopNoise();

            switch (moved)
            {
                case -1:
                    audMgr.RandomizeSFX(audMgr.moveWest);
                    break;
                case 1:
                    audMgr.RandomizeSFX(audMgr.moveEast);
                    break;
                case -4:
                    audMgr.RandomizeSFX(audMgr.moveNorth);
                    break;
                case 4:
                    audMgr.RandomizeSFX(audMgr.moveSouth);
                    break;

            }

        }
        else if (!audMgr.radioSounds.isPlaying)
        {
            //audMgr.PlayNoise(audMgr.noiseSFX);
        }
    }
}
