require 'spec_helper'

def valid_claimed_recording
  @name = "hello"
  @description = "description"
  @genre = Genre.first
  @is_public = true
end

def make_claim
  @claimed_recording = @recording.claim(@user, @name, @description, @genre, @is_public)
end

describe ClaimedRecording do

  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

  end

  describe "sucessful save" do
    it "with default case" do
      valid_claimed_recording
      make_claim
      @claimed_recording.errors.any?.should be_false
      @recording.reload
      @recording.recorded_tracks.first.discard.should be_false
    end
  end

  describe "update name" do
    it "with nil" do
      valid_claimed_recording
      @name = nil
      make_claim
      @claimed_recording.errors.any?.should be_true
      @claimed_recording.errors[:name].length.should == 2
      @claimed_recording.errors[:name].select { |value| value.include?("can't be blank") }.length.should == 1
      @claimed_recording.errors[:name].select { |value| value.include?("is too short") }.length.should == 1
      @recording.reload
      @recording.recorded_tracks.first.discard.should be_nil
    end

    it "too short" do
      valid_claimed_recording
      @name = "a"
      make_claim
      @claimed_recording.errors.any?.should be_true
      @claimed_recording.errors[:name].length.should == 1
      @claimed_recording.errors[:name].select { |value| value.include?("is too short") }.length.should == 1
    end
  end

  describe "update description" do
    it "with nil" do
      valid_claimed_recording
      @description = nil
      make_claim
      @claimed_recording.errors.any?.should be_false
    end
  end

  describe "update is_public" do
    it "with nil" do
      valid_claimed_recording
      @is_public = nil
      make_claim
      @claimed_recording.errors.any?.should be_true
      @claimed_recording.errors[:is_public].length.should == 1
      @claimed_recording.errors[:is_public].should == ["is not included in the list"]
    end
  end

  describe "update genre" do
    it "with nil" do
      valid_claimed_recording
      @genre = nil
      make_claim
      @claimed_recording.errors.any?.should be_true
      @claimed_recording.errors[:genre].length.should == 1
      @claimed_recording.errors[:genre].should == ["can't be blank"]
    end
  end

  describe "multiple claims" do
    it "not valid" do
      valid_claimed_recording
      make_claim
      duplicate = @recording.claim(@user, "name", "description", @genre, true)
      duplicate.valid?.should be_false
      duplicate.errors[:user_id].should == ['has already been taken']
    end
  end

  describe "remove_non_alpha_num" do

    let(:instance) { ClaimedRecording.new }

    it "removes hyphen" do
      instance.remove_non_alpha_num("abc-").should == 'abc'
    end

    it "leaves good alone" do
      instance.remove_non_alpha_num("JDnfHsimMQ").should == 'JDnfHsimMQ'
    end
  end

  describe "favorite_index" do

    let(:other_user) { FactoryGirl.create(:user) }

    it "returns nothing" do
      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id)
      favorites.length.should == 0
      start.should be_nil
    end

    it "angry when no user specified" do
      expect { ClaimedRecording.index_favorites(@user, user:other_user ) }.to raise_error "unable to view another user's favorites"
    end

    it "user must be specified" do
      expect { ClaimedRecording.index_favorites(@user) }.to raise_error "user must be specified"
    end

    it "finds favorite claimed_recording if true, not if false" do
      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: @user)

      like = FactoryGirl.create(:recording_like, user: @user, claimed_recording: claimed_recording1, recording: claimed_recording1.recording, favorite: true)

      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id)
      favorites.length.should == 1
      start.should be_nil

      like.favorite = false
      like.save!

      # remove from favorites
      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id)
      favorites.length.should == 0
      start.should be_nil
    end

    it "finds others public claimed recordings" do
      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: other_user)

      like = FactoryGirl.create(:recording_like, user: @user, claimed_recording: claimed_recording1, recording: claimed_recording1.recording, favorite: true)

      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id)
      favorites.length.should == 1
      start.should be_nil
    end

    it "can find others private claimed recordings" do
      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: other_user)

      like = FactoryGirl.create(:recording_like, user: @user, claimed_recording: claimed_recording1, recording: claimed_recording1.recording, favorite: false)

      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id)
      favorites.length.should == 0
      start.should be_nil
    end

    it "can find own private claimed recordings" do
      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: @user)

      like = FactoryGirl.create(:recording_like, user: @user, claimed_recording: claimed_recording1, recording: claimed_recording1.recording, favorite: true)

      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id)
      favorites.length.should == 1
      start.should be_nil
    end

    it "pagination" do
      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: @user)
      claimed_recording2 = FactoryGirl.create(:claimed_recording, user: @user)

      like1 = FactoryGirl.create(:recording_like, user: @user, claimed_recording: claimed_recording1, recording: claimed_recording1.recording, favorite: true)
      like2 = FactoryGirl.create(:recording_like, user: @user, claimed_recording: claimed_recording2, recording: claimed_recording2.recording, favorite: true)

      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id, limit:1)
      favorites.length.should == 1
      start.should_not be_nil

      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id, limit:1, start: start)
      favorites.length.should == 1
      start.should_not be_nil

      favorites, start = ClaimedRecording.index_favorites(@user, user: @user.id, limit:1, start: start)
      favorites.length.should == 0
      start.should be_nil
    end
  end
end