require 'spec_helper'

describe ApiClaimedRecordingsController, type: :controller do
  render_views

  before(:each) do
    @user = FactoryGirl.create(:user)
    @connection = FactoryGirl.create(:connection, :user => @user)
    @instrument = FactoryGirl.create(:instrument, :description => 'a great instrument')
    @track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument)
    @music_session = FactoryGirl.create(:active_music_session, :creator => @user, :musician_access => true)
    # @music_session.connections << @connection
    @music_session.save
    @connection.join_the_session(@music_session, true, nil, @user, 10)
    @recording = Recording.start(@music_session, @user)
    @recording.stop
    @recording.reload
    @genre = FactoryGirl.create(:genre)
    @recording.claim(@user, "name", "description", @genre, true)
    @recording.reload
    @claimed_recording = @recording.claimed_recordings.first!
    @recording.has_mix?.should be false

  end    
  
  describe "show" do
    
    it "should show the right thing when one recording just finished" do
      controller.current_user = @user
      get :show, {format:'json', id: @claimed_recording.id}
      response.should be_success  
      json = JSON.parse(response.body)
      json.should_not be_nil
      json["id"].should == @claimed_recording.id
      json["name"].should == @claimed_recording.name
      json["recording"]["id"].should == @recording.id
      json["recording"]["band"].should be_nil
      json["recording"]["recorded_tracks"].length.should == 1
      json["recording"]["recorded_tracks"].first["id"].should == @recording.recorded_tracks.first.id
      json["recording"]["recorded_tracks"].first["instrument_id"].should == @instrument.id
      json["recording"]["recorded_tracks"].first["user"]["id"].should == @user.id
      json["mix"].should be_nil
    end

    it "should show the right thing when one recording was just uploaded" do
      pending
      @recording.recorded_tracks.first.upload_complete
      controller.current_user = @user
      get :show, :id => @claimed_recording.id
      response.should be_success        
      json = JSON.parse(response.body)
      json.should_not be_nil
      json["recording"]["mixes"].length.should == 1
      json["recording"]["mixes"].first["id"].should == @recording.mixes.first.id
      json["recording"]["mixes"].first["url"].should == @recording.mixes.first.url     
      json["recording"]["mixes"].first["is_completed"].should == false
    end


    it "should show the right thing when the mix was just uploaded" do
      pending
      @recording.recorded_tracks.first.upload_complete
      @mix = Mix.next("server")
      @mix.finish(10000, "md5")
      controller.current_user = @user
      get :show, :id => @claimed_recording.id
      response.should be_success        
      json = JSON.parse(response.body)
      json.should_not be_nil
      json["recording"]["mixes"].length.should == 1
      json["recording"]["mixes"].first["id"].should == @recording.mixes.first.id
      json["recording"]["mixes"].first["is_completed"].should == true
    end
  end
  
  describe "index" do
    it "should generate a single output" do
      controller.current_user = @user
      get :index
      response.should be_success        
      json = JSON.parse(response.body)
      json.should_not be_nil
      json.length.should == 1
      json.first["id"].should == @claimed_recording.id

    end
  end

  describe "download" do
    it "no mix" do
      controller.current_user = @user
      get :download, id: @claimed_recording.id
      response.status.should == 404
    end

    it "quick mix, not completed" do
      quick_mix = FactoryGirl.create(:quick_mix)
      controller.current_user = @user
      get :download, id: quick_mix.recording.candidate_claimed_recording.id
      response.status.should == 404
    end

    it "quick mix, completed" do
      quick_mix = FactoryGirl.create(:quick_mix_completed)
      controller.current_user = @user
      get :download, id: quick_mix.recording.candidate_claimed_recording.id
      response.status.should == 302
      response.headers['Location'].include?('recordings/ogg').should be true

      get :download, id: quick_mix.recording.candidate_claimed_recording.id, type:'mp3'
      response.status.should == 302
      response.headers['Location'].include?('recordings/mp3').should be true
    end

    it "final mix, not completed" do
      mix = FactoryGirl.create(:mix, completed:false)
      controller.current_user = @user
      get :download, id: mix.recording.candidate_claimed_recording.id
      response.status.should == 404
    end

    it "final mix, completed" do
      mix = FactoryGirl.create(:mix, completed:true)
      controller.current_user = @user
      get :download, id: mix.recording.candidate_claimed_recording.id
      response.status.should == 302
      response.headers['Location'].include?('recordings/mixed/ogg').should be true

      get :download, id: mix.recording.candidate_claimed_recording.id, type:'mp3'
      response.status.should == 302
      response.headers['Location'].include?('recordings/mixed/mp3').should be true
    end

    it "both completed final mix and stream mix" do
      # when both quick mix and mix are present, this should let mix win
      mix = FactoryGirl.create(:mix, completed:true)
      quick_mix = FactoryGirl.create(:quick_mix_completed, recording: mix.recording, user: mix.recording.owner)
      controller.current_user = @user
      get :download, id: mix.recording.candidate_claimed_recording.id
      response.status.should == 302
      response.headers['Location'].include?('recordings/mixed/ogg').should be true

      get :download, id: mix.recording.candidate_claimed_recording.id, type:'mp3'
      response.status.should == 302
      response.headers['Location'].include?('recordings/mixed/mp3').should be true
    end

    it "completed stream mix, incomplete final mix" do
      # when both quick mix and mix are present, this should let mix win
      mix = FactoryGirl.create(:mix, completed:false)
      quick_mix = FactoryGirl.create(:quick_mix_completed, recording: mix.recording, user: mix.recording.owner)
      controller.current_user = @user
      get :download, id: mix.recording.candidate_claimed_recording.id
      response.status.should == 302
      response.headers['Location'].include?('recordings/ogg').should be true

      get :download, id: quick_mix.recording.candidate_claimed_recording.id, type:'mp3'
      response.status.should == 302
      response.headers['Location'].include?('recordings/mp3').should be true
    end


  end
  
=begin
 We can't test these because rspec doesn't like that we return 204. It causes rails to return a 406. 
  describe "DELETE 'destroy'" do
    it "should delete properly" do
      controller.current_user = @user
      delete :delete, :id => @claimed_recording.id
      puts response
      puts response.body
      
      response.should be_success
      expect { ClaimedRecording.find(@claimed_recording.id) }.to raise_error
    end
  end
=end  


end