require 'spec_helper'

describe JamTrackImporter do

  before {
    pending "Much of JamTracks was put into long-term storage (S3 Glacier). Must bring some back for this to work"
  }

  let(:s3_manager) { S3Manager.new(app_config.aws_bucket_jamtracks, app_config.aws_access_key_id, app_config.aws_secret_access_key) }

  let(:sample_yml)  {
    {
        "name" => "Back in Black",
        "plan_code" => "jamtrack-acdc-backinblack",
        "recording_type" => 'Cover',
        "pro" => 'ASCAP',
        "genre" => 'rock'

    }
  }
  describe "load_metalocation" do

    # NOTE: uncomment if unmark pending!

    #include UsesTempFiles
    #metafile = 'meta.yml'
    #in_directory_with_file(metafile)

    before(:each) do
      before {
        pending "Much of JamTracks was put into long-term storage (S3 Glacier). Must bring some back for this to work"
      }
      JamTrackImporter.storage_format = 'default'
      content_for_file(YAML.dump(sample_yml))
    end

    it "no meta" do
      s3_metalocation = 'audio/Artist 1/Bogus Place/meta.yml'
      JamTrackImporter.storage_format = 'default'
      JamTrackImporter.load_metalocation(s3_metalocation).should be_nil
    end

    it "successfully" do
      s3_metalocation = 'audio/Artist 1/Song 1/meta.yml'
      s3_manager.upload(s3_metalocation, metafile)

      JamTrackImporter.load_metalocation(s3_metalocation).should eq(sample_yml)
    end
  end

  describe "sort_tracks" do
    let(:jam_track) { FactoryGirl.create(:jam_track) }
    let(:importer) { JamTrackImporter.new() }
    let(:vocal) {Instrument.find('voice')}
    let(:drums) {Instrument.find('drums')}
    let(:bass_guitar) {Instrument.find('bass guitar')}
    let(:piano) {Instrument.find('piano')}
    let(:keyboard) {Instrument.find('keyboard')}
    let(:acoustic_guitar) {Instrument.find('acoustic guitar')}
    let(:electric_guitar) {Instrument.find('electric guitar')}
    let(:other) {Instrument.find('other')}

    it "the big sort" do
      # specified in https://jamkazam.atlassian.net/browse/VRFS-3296
      vocal_lead = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: vocal, part: 'Lead')
      vocal_lead_female = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: vocal, part: 'Lead Female')
      vocal_lead_male = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: vocal, part: 'Lead Male')
      vocal_backing = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: vocal, part: 'Backing')
      vocal_random = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: vocal, part: 'Random')
      drums_drums = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: drums, part: 'Drums')
      drums_percussion = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: drums, part: 'Percussion')
      drums_random_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: drums, part: 'A')
      drums_random_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: drums, part: 'C')
      bass_guitar_bass = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: bass_guitar, part: 'Bass')
      bass_guitar_random_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: bass_guitar, part: 'some bass')
      bass_guitar_random_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: bass_guitar, part: 'zome bass')
      piano_piano = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: piano, part: 'Piano')
      keyboard_synth_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: keyboard, part: 'Synth 1')
      keyboard_synth_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: keyboard, part: 'Synth 2')
      keyboard_pads = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: keyboard, part: 'Pads')
      keyboard_random_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: keyboard, part: 'A')
      keyboard_random_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: keyboard, part: 'Z')
      acoust_guitar_lead = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: acoustic_guitar, part: 'Lead')
      acoust_guitar_lead_x = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: acoustic_guitar, part: 'Lead X')
      acoust_guitar_solo_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: acoustic_guitar, part: 'Solo 1')
      acoust_guitar_solo_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: acoustic_guitar, part: 'Solo 2')
      acoust_guitar_rhythm = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: acoustic_guitar, part: 'Rhythm')
      acoust_guitar_random_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: acoustic_guitar, part: 'A')
      acoust_guitar_random_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: acoustic_guitar, part: 'Z')
      elect_guitar_lead = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: electric_guitar, part: 'Lead')
      elect_guitar_lead_x = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: electric_guitar, part: 'Lead X')
      elect_guitar_solo_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: electric_guitar, part: 'Solo 1')
      elect_guitar_solo_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: electric_guitar, part: 'Solo 2')
      elect_guitar_rhythm = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: electric_guitar, part: 'Rhythm')
      elect_guitar_random_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: electric_guitar, part: 'A')
      elect_guitar_random_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: electric_guitar, part: 'Z')
      other_1 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: other, part: 'Other 1')
      other_2 = FactoryGirl.build(:jam_track_track, jam_track: jam_track, instrument: other, part: 'Other 2')

      expected = [
          vocal_lead,
          vocal_lead_female,
          vocal_lead_male,
          vocal_backing,
          vocal_random,
          drums_drums,
          drums_percussion,
          drums_random_1,
          drums_random_2,
          bass_guitar_bass,
          piano_piano,
          keyboard_synth_1,
          keyboard_synth_2,
          keyboard_pads,
          keyboard_random_1,
          keyboard_random_2,
          acoust_guitar_lead,
          acoust_guitar_lead_x,
          acoust_guitar_rhythm,
          acoust_guitar_random_1,
          acoust_guitar_solo_1,
          acoust_guitar_solo_2,
          acoust_guitar_random_2,
          elect_guitar_lead,
          elect_guitar_lead_x,
          elect_guitar_solo_1,
          elect_guitar_solo_2,
          elect_guitar_rhythm,
          elect_guitar_random_1,
          elect_guitar_random_2,
          bass_guitar_random_1,
          bass_guitar_random_2,
          other_1,
          other_2
      ]
      shuffled = expected.shuffle
      sorted_tracks = importer.sort_tracks(shuffled)

      importer.set_custom_weight(vocal_lead).should eq(100)

      expected.each_with_index do |expected_track, i|
        sorted_tracks[i].should eq(expected_track)
      end
    end
  end

  describe "synchronize" do
    let(:jam_track) { jt = JamTrack.new; jt.id = '1'; jt }
    let(:importer) { JamTrackImporter.new() }
    let(:minimum_meta) { nil }
    let(:metalocation) { 'audio/Artist 1/Song 1/meta.yml' }
    let(:options) {{ skip_audio_upload:true }}

    it "bare minimum specification" do
      importer.synchronize_metadata(jam_track, minimum_meta, metalocation, 'Artist 1', 'Song 1', options)

      jam_track.plan_code.should eq('jamtrack-artist1-song1')
      jam_track.name.should eq("Song 1")
      jam_track.description.should == "This is a JamTrack audio file for use exclusively with the JamKazam service. This JamTrack is a high quality cover of the Artist 1 song \"Song 1\"."
      jam_track.time_signature.should be_nil
      jam_track.status.should eq('Staging')
      jam_track.recording_type.should eq('Cover')
      jam_track.original_artist.should eq('Artist 1')
      jam_track.songwriter.should be_nil
      jam_track.publisher.should be_nil
      jam_track.sales_region.should eq('Worldwide')
      jam_track.price.should eq(1.99)
    end
  end

  describe "parse_wav" do
    it "Guitar" do
      result = JamTrackImporter.new.parse_file('blah/Ready for Love Stem - Guitar - Main.wav')
      result[:instrument].should eq('electric guitar')
      result[:part].should eq('Main')
    end
  end
end