using UnityEngine;
using System.Collections;
using Jammer;

public class MusicEngine : MonoBehaviour {

    // Use this for initialization
    public GameObject playerRef;
    public AudioSource aud;
    public AudioClip audclip1;
    public AudioClip audclip2;
    public AudioClip audclip3;
    public AudioClip audclip4;
    public AudioClip audclip5;
    public GameObject drumMachine;
    public GameObject drumEffect;
    public GameObject guitar;
    public GameObject[] keytar;
    public GameObject bass;
    public GameObject bassLightning;
    public GameObject bassSpark1;
    public GameObject bassSpark2;
    public GameObject bassSpark3;
    public GameObject bassDropper;
    public dropthatbass dropThatBass;
    public RepeatDrums rd;
    public GameManager gameManager;

    public Animator bassAnimator;
    public Animator drumAnimator;
    public Animator keytarAnimator;
    public Animator guitarAnimator;

    public int drumDamper = 4;
    public int bassDamper = 1;
    public int guitarDamper = 1;
    public int keytarDamper = 1;

    public float timeRemaining;
    public float startTime;

    public void MusicStart(string name) {
        playerRef = GameObject.FindGameObjectWithTag("Player");
        playerRef.transform.position = new Vector3(0.3f, 5, 80);
        CancelInvoke();
        bassAnimator = GameObject.FindGameObjectWithTag("BassPlayer").GetComponent<Animator>();
        drumAnimator = GameObject.FindGameObjectWithTag("Drummer").GetComponent<Animator>();
        keytarAnimator = GameObject.FindGameObjectWithTag("KeytarPlayer").GetComponent<Animator>();
        guitarAnimator = GameObject.FindGameObjectWithTag("GuitarPlayer").GetComponent<Animator>();
        dropThatBass = bassDropper.GetComponent<dropthatbass>();
        keytar = new GameObject[4];
        keytar = GameObject.FindGameObjectsWithTag("Keytar");
        rd = drumMachine.GetComponent<RepeatDrums>();
        aud = gameObject.GetComponent<AudioSource>();
        startTime = Time.time;
        if (name == "song1") {
            aud.clip = audclip1;
            drumDamper = 4;
            bassDamper = 1;
            guitarDamper = 1;
            keytarDamper = 1;
            dropThatBass.difficulty = 1;
        }
        if (name == "song2")
        {
            aud.clip = audclip2;
            drumDamper = 2;
            bassDamper = 1;
            guitarDamper = 1;
            keytarDamper = 1;
            dropThatBass.difficulty = 1;
        }
        if (name == "song3")
        {
            aud.clip = audclip3;
            drumDamper = 1;
            bassDamper = 1;
            guitarDamper = 1;
            keytarDamper = 1;
            dropThatBass.difficulty = 1;
        }
        if (name == "song4")
        {
            aud.clip = audclip4;
            drumDamper = 1;
            bassDamper = 1;
            guitarDamper = 1;
            keytarDamper = 1;
            dropThatBass.difficulty = 50;
        }
        if (name == "song5")
        {
            aud.clip = audclip5;
            drumDamper = 1;
            bassDamper = 1;
            guitarDamper = 1;
            keytarDamper = 1;
            dropThatBass.difficulty = 1;
        }
        StartMusic();
        SongBeats song = new SongBeats();
        song.Load(name);
        int i = 0;
        foreach (BeatRecord beat in song.Drums)
        {
            if (i % drumDamper == 0)
            {
                Invoke("Drum", beat.Seconds);
            }
            i++;
        }

        int j = 0;  
        foreach (BeatRecord pluck in song.Guitar)
        {
            if (j % guitarDamper == 0)
            {
                Invoke("Guitar", pluck.Seconds);
            }
        }

        int k = 0;    
        foreach (BeatRecord slap in song.Bass)
        {
            if (k % bassDamper == 0)
            {
                Invoke("Bass", slap.Seconds);
            }
        }

        int l = 0;
        foreach (BeatRecord tink in song.Keyboard)
        {
            if (l % keytarDamper == 0)
            {
                Invoke("Keytar", tink.Seconds);
            }
        }
    }
	
	// Update is called once per frame
	void Update () {
        if (gameManager.State == GameState.Play) {
            timeRemaining = aud.clip.length - (Time.time - startTime);
            if(timeRemaining < 0){
                gameManager.State = GameState.Lose;
            }
        }

	}

    void StartMusic()
    {
        aud.Play();
    }

    public void Drum()
    {
        rd = drumMachine.GetComponent<RepeatDrums>();
        drumMachine.SendMessage("DrumMakeBeat");
        drumEffect.SendMessage("DrumMakeBeat");
        drumAnimator.SetTrigger("rock");
    }

    public void Guitar()
    {
        guitar.SendMessage("GuitarMakeBeat");
        guitarAnimator.SetTrigger("rock");
    }

    public void Keytar()
    {
        foreach (GameObject key in keytar)
        {
            key.SendMessage("KeytarMakeBeat");
            keytarAnimator.SetTrigger("rock");
        }
    }

    public void Bass()
    {
        bass.SendMessage("BassMakeBeat");
        bassLightning.SendMessage("BassMakeBeat");
        bassSpark1.SendMessage("BassMakeBeat");
        bassSpark2.SendMessage("BassMakeBeat");
        bassSpark3.SendMessage("BassMakeBeat");
        bassAnimator.SetTrigger("rock");

    }
}
