require 'spec_helper'

describe ApiFeedsController, type: :controller do
  render_views

  let(:user) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }
  let(:band) { FactoryGirl.create(:band) }
  let(:music_session) {FactoryGirl.create(:active_music_session, creator: user) }
  let(:claimed_recording) {FactoryGirl.create(:claimed_recording) }

  before(:each) do
    ActiveMusicSession.delete_all
    MusicSessionUserHistory.delete_all
    MusicSession.delete_all
    Recording.delete_all
  end

  it "returns nothing" do
    get :index
    json = JSON.parse(response.body, :symbolize_names => true)
    json[:entries].length.should == 0
  end


  it "returns a recording" do
    claimed_recording.touch
    # artifact of factory of :claimed_recording that this gets created
    MusicSessionUserHistory.delete_all
    MusicSession.delete_all

    get :index
    json = JSON.parse(response.body, :symbolize_names => true)
    json[:entries].length.should == 1

    claimed_recording = json[:entries][0]
    claimed_recording[:type].should == 'recording'
  end

  it "returns a music session" do
    music_session.touch
    # artifact of factory of :claimed_recording that this gets created

    get :index
    json = JSON.parse(response.body, :symbolize_names => true)
    json[:entries].length.should == 1

    music_session = json[:entries][0]
    music_session[:type].should ==  'music_session'
  end

  describe "time range" do

    it "today and month find different results" do
      music_session.music_session.touch
      music_session.music_session.feed.created_at = 3.days.ago
      music_session.music_session.feed.save!

      get :index
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1

      get :index, { time_range:'today' }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 0
    end
  end

  describe "type filter" do

    it "can filter by type" do
      claimed_recording.touch

      get :index
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 2

      get :index, { type: 'recording'}
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1
      json[:entries][0][:type].should == 'recording'

      get :index, { type: 'music_session'}
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1
      json[:entries][0][:type].should == 'music_session'
    end
  end

  describe "pagination" do

    it "since parameter"  do
      claimed_recording.touch
      claimed_recording.recording.created_at = 3.days.ago
      claimed_recording.recording.save!

      ams = ActiveMusicSession.find(claimed_recording.recording.music_session.music_session.id)
      ams.before_destroy

      get :index, { limit: 1 }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1
      _next = json[:next_page]
      _next.should_not be_nil

      get :index, { limit: 1, next_page: _next }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1
      _next = json[:next_page]
      _next.should_not be_nil

      get :index, { limit: 1, next_page: _next }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 0
      _next = json[:next_page]
      _next.should be_nil
    end
  end

  describe "user targetting" do

    it "user viewing own profile" do
      music_session.music_session.fan_access = false
      music_session.music_session.save!
      controller.current_user = music_session.creator

      get :index, { user: music_session.creator.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1

      json[:entries][0][:helpers][:name].should == music_session.music_session.name
      json[:entries][0][:helpers][:description].should == music_session.music_session.description
    end

    it "user viewing someone else's profile" do
      music_session.music_session.fan_access = false
      music_session.music_session.save!
      controller.current_user = user2
      music_session.music_session.reload
      music_session.music_session.fan_access.should be false

      get :index,  { user: music_session.creator.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1

      json[:entries][0][:helpers][:name].should == "Private"
      json[:entries][0][:helpers][:description].should == "Private"
    end
  end

  describe "band targetting" do

    it "user viewing own band" do
      user.bands << band
      user.save!
      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: user)
      claimed_recording1.is_public = false
      claimed_recording1.recording.band = band
      claimed_recording1.recording.save!
      claimed_recording1.save!

      controller.current_user = user

      get :index, { band: band.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1

      json[:entries][0][:helpers][:name].should == claimed_recording1.name
      json[:entries][0][:helpers][:description].should == claimed_recording1.description
    end

    it "user viewing someone else's band" do
      user.bands << band
      user.save!

      claimed_recording1 = FactoryGirl.create(:claimed_recording, user: user)
      claimed_recording1.is_public = false
      claimed_recording1.recording.band = band
      claimed_recording1.recording.save!
      claimed_recording1.save!

      controller.current_user = user2

      get :index, { band: band.id }
      json = JSON.parse(response.body, :symbolize_names => true)
      json[:entries].length.should == 1

      json[:entries][0][:helpers][:name].should == "Private"
      json[:entries][0][:helpers][:description].should == "Private"
    end
  end
end
