require 'spec_helper'

describe ApiUserSyncsController, type: :controller do
  render_views


  let(:user1) {FactoryGirl.create(:user)}
  let(:user2) {FactoryGirl.create(:user)}

  before(:each) {
    controller.current_user = user1
  }

  it "requires logged in" do
    controller.current_user = nil
    get :index, {format: 'json', id: ''}
    json = JSON.parse(response.body, :symbolize_names => true)
    json.should == {message: "not logged in"}
  end

  it "can return empty results" do
    get :index, { :format => 'json', :id => user1.id }
    json = JSON.parse(response.body, :symbolize_names => true)
    json[:next].should be_nil
    json[:entries].length.should == 0
  end

  it "deletables" do
    post :deletables, { :format => 'json', :id => user1.id, recording_ids: ['1'] }
    json = JSON.parse(response.body, :symbolize_names => true)
    json[:recording_ids].should eq(['1'])
  end

  describe "one recording with two users" do
    let!(:recording1) {
      recording = FactoryGirl.create(:recording, owner: user1, band: nil, duration:1)
      recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: recording.owner)
      recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: user2)
      recording.save!
      recording.reload
      recording
    }

    let(:sorted_tracks) {
      Array.new(recording1.recorded_tracks).sort_by! {|rt| -rt.id}
    }

    it "no claimed_recordings" do

      # if there are no claims, then no one wants the recording. no usersyncs
      get :index, { :format => 'json', :id => user1.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:next].should be_nil
      json[:entries].length.should == 0
    end

    it "recording isn't over" do
      recording1.duration = nil
      recording1.save!

      get :index, { :format => 'json', :id => user1.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:next].should be_nil
      json[:entries].length.should == 0
    end

    it "one user decides to keep the recording" do

      FactoryGirl.create(:claimed_recording, user: user1, recording: recording1, discarded:false)

      recording1.recorded_tracks[0].fully_uploaded = false
      recording1.recorded_tracks[0].save!

      get :index, { :format => 'json', :id => user1.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:next].should be_nil
      json[:entries].length.should == 2

      recorded_track1 = json[:entries][0]
      recorded_track1[:upload][:should_upload].should be true
      recorded_track1[:upload][:too_many_upload_failures].should be false
      recorded_track1[:download][:should_download].should be true
      recorded_track1[:download][:too_many_downloads].should be false

      recorded_track2 = json[:entries][1]
      recorded_track2[:upload][:should_upload].should be true
      recorded_track2[:upload][:too_many_upload_failures].should be false
      recorded_track2[:download][:should_download].should be true
      recorded_track2[:download][:too_many_downloads].should be false

      controller.current_user = user2
      get :index, { :format => 'json', :id => user2.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:next].should be_nil
      json[:entries].length.should == 1

      recorded_track1 = json[:entries][0]
      recorded_track1[:upload][:should_upload].should be true
      recorded_track1[:upload][:too_many_upload_failures].should be false
      recorded_track1[:download][:should_download].should be false
      recorded_track1[:download][:too_many_downloads].should be false

    end

    it "one user decides to discard the recording" do
      FactoryGirl.create(:claimed_recording, user: user1, recording: recording1, discarded:true)
      FactoryGirl.create(:claimed_recording, user: user2, recording: recording1, discarded:false)

      # it's a length of zero because recorded_tracks default to 'fully_uploaded = true'. so nothing to do for this user
      get :index, { :format => 'json', :id => user1.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:next].should be_nil
      json[:entries].length.should == 0

      controller.current_user = user2
      get :index, { :format => 'json', :id => user2.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:next].should be_nil
      json[:entries].length.should == 2


      recorded_track1 = json[:entries][0]
      recorded_track1[:upload][:should_upload].should be true
      recorded_track1[:upload][:too_many_upload_failures].should be false
      recorded_track1[:download][:should_download].should be true
      recorded_track1[:download][:too_many_downloads].should be false

      recorded_track2 = json[:entries][1]
      recorded_track2[:upload][:should_upload].should be true
      recorded_track2[:upload][:too_many_upload_failures].should be false
      recorded_track2[:download][:should_download].should be true
      recorded_track2[:download][:too_many_downloads].should be false
    end

    it "both users decide to discard the recording" do
      recording1.all_discarded = true
      recording1.save!

      get :index, { :format => 'json', :id => user1.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:next].should be_nil
      json[:entries].length.should == 0

    end
  end

end
