using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Jammer {

  public class SongBeats {

    /// <summary>
    /// List of beat records for the Drums
    /// </summary>
    public List<BeatRecord> Drums;

    /// <summary>
    /// List of beat records for the Guitar
    /// </summary>
    public List<BeatRecord> Guitar;

    /// <summary>
    /// List of beat records for the Bass
    /// </summary>
    public List<BeatRecord> Bass;

    /// <summary>
    /// List of beat records for the Bass
    /// </summary>
    public List<BeatRecord> Keyboard;

    /// <summary>
    /// Generic Constructor
    /// </summary>
    public SongBeats(){

      // Data looks like this
      //Drums = new List<BeatRecord>();
      //Drums.Add(new BeatRecord() { Seconds = 1.2f, Difficulty = 1 });
      //Drums.Add(new BeatRecord() { Seconds = 2.2f, Difficulty = 1 });
      //Drums.Add(new BeatRecord() { Seconds = 2.8f, Difficulty = 1 });
      //// TODO...

      //Guitar = new List<BeatRecord>();
      //Guitar.Add(new BeatRecord() { Seconds = 1.2f, Difficulty = 1 });
      //Guitar.Add(new BeatRecord() { Seconds = 2.2f, Difficulty = 1 });
      //Guitar.Add(new BeatRecord() { Seconds = 2.8f, Difficulty = 1 });
      //// TODO...
    }

    public void Load(string folder) {
      //Debug.Log(string.Format("SongBeats.Load(folder: {0})", folder));

      string resource;
      string instrument;
      TextAsset textData;

      instrument = "drums";
      resource = string.Format("songs/{0}/{1}", folder, instrument);
      //Debug.Log(string.Format("SongBeats.Load(folder: {0}) resource {1}", folder, resource));
      textData = (TextAsset) Resources.Load(resource, typeof(TextAsset));
      if (textData != null) {
        //Debug.Log(string.Format("SongBeats.Load() loaded json {0}", textData.text));
        Drums = JsonConvert.DeserializeObject<List<BeatRecord>>(textData.text);
      }
      else {
        Debug.LogError(string.Format("SongBeats.Load(folder: {0}) resource {1} failed", folder, resource));
      }

      instrument = "guitar";
      resource = string.Format("songs/{0}/{1}", folder, instrument);
      //Debug.Log(string.Format("SongBeats.Load(folder: {0}) resource {1}", folder, resource));
      textData = (TextAsset) Resources.Load(resource, typeof(TextAsset));
      if (textData != null) {
        //Debug.Log(string.Format("SongBeats.Load() loaded json {0}", textData.text));
        Guitar = JsonConvert.DeserializeObject<List<BeatRecord>>(textData.text);
      }
      else {
        Debug.LogError(string.Format("SongBeats.Load(folder: {0}) resource {1} failed", folder, resource));
      }

      instrument = "bass";
      resource = string.Format("songs/{0}/{1}", folder, instrument);
      //Debug.Log(string.Format("SongBeats.Load(folder: {0}) resource {1}", folder, resource));
      textData = (TextAsset) Resources.Load(resource, typeof(TextAsset));
      if (textData != null) {
        //Debug.Log(string.Format("SongBeats.Load() loaded json {0}", textData.text));
        Bass = JsonConvert.DeserializeObject<List<BeatRecord>>(textData.text);
      }
      else {
        Debug.LogError(string.Format("SongBeats.Load(folder: {0}) resource {1} failed", folder, resource));
      }

      instrument = "keyboard";
      resource = string.Format("songs/{0}/{1}", folder, instrument);
      //Debug.Log(string.Format("SongBeats.Load(folder: {0}) resource {1}", folder, resource));
      textData = (TextAsset) Resources.Load(resource, typeof(TextAsset));
      if (textData != null) {
        //Debug.Log(string.Format("SongBeats.Load() loaded json {0}", textData.text));
        Keyboard = JsonConvert.DeserializeObject<List<BeatRecord>>(textData.text);
      }
      else {
        Debug.LogError(string.Format("SongBeats.Load(folder: {0}) resource {1} failed", folder, resource));
      }
    }

  }
}
