require 'spec_helper'

describe "Create Session UI", :js => true, :type => :feature, :capybara_feature => true do
  let(:user1) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }

  context "create session flow ui" do
    before(:each) do
      MusicSession.delete_all
      ActiveMusicSession.delete_all

      in_client(user1) do
        page.driver.resize(1500, 800) # makes sure all the elements are visible
        emulate_client
        sign_in_poltergeist user1
        page.find('.createsession').trigger(:click)
      end
    end

    # VRFS-1976
    it "create two sessions; select the second one" do

      # this is tricky because of iCheck and it's custom way of dealing with 'checked' state
      start1 = 15.minutes.ago
      session1 = FactoryGirl.create(:music_session, description: 'My Session 1', creator: user1, scheduled_start: start1)
      session2 = FactoryGirl.create(:music_session, description: 'My Session 2', creator: user1)


      in_client(user1) do
        # should reload the session page, so that we see new sessions
        visit '/client#/createSession'

        # pick the second session
        page.find("#scheduled-session-list input[data-session-id='#{session2.id}'] + ins").trigger(:click)
        page.find('.btn-next').trigger(:click)

        # check if future dialog is showing; if so, accept
        accept = first('#btn-confirm-ok', text: "START SESSION NOW")
        accept.trigger(:click) if accept

        page.find('#session-description-disp', text: 'My Session 2')
      end
    end

    describe "step 1" do
      it "initial status" do
        in_client(user1) do
          find('.session-step-title', text: 'When Is Your Session?')
          find('#session-when-start-scheduled[checked="checked"]')
          find('div#scheduled-session-not-found')
          find('div[info-id="start-scheduled"]')
          page.should have_css(".session-stepnumber", :count => 2)
          page.should have_css(".session-stepnumber.session-stepactive", :count => 1)
          page.should_not have_css(".btn-back")
          page.should have_css(".btn-next.disabled")
          page.should have_css(".btn-help")
        end
      end

      it "select option for scheduling a session" do
        in_client(user1) do
          find('li[create-type="schedule-future"] ins').trigger(:click)
          find('div[info-id="schedule-future"]')
          page.should have_css(".session-stepnumber", :count => 5)
          page.should have_css(".session-stepnumber.session-stepactive", :count => 1)
          find('div#schedule-future-wrapper')
          find('h3', text: 'When will the session happen?')
          find('input#session-start-date')
          find('.start-time-list-holder .easydropdown')
          find('.end-time-list-holder .easydropdown')
          find('.start-time-list-holder .easydropdown')
          find('.timezone-list-holder .easydropdown')
          find('.recurring-mode-list-holder .easydropdown')

          # by selecting this time, the end-time-list should auto-select to 6:30PM
          # Out for the time being; because the contents of the dropdown is based on the current time
          #jk_select('05:30 PM', '#start-time-list')
          #find('.end-time-list-holder .selected', text: '06:30 PM')
        end
      end

      it "select option for scheduling session after others RSVP" do
        in_client(user1) do
          find('li[create-type="rsvp"] ins').trigger(:click)
          find('div[info-id="rsvp"]')
          page.should have_css(".session-stepnumber", :count => 5)
          page.should have_css(".session-stepnumber.session-stepactive", :count => 1)
        end
      end

      it "select option for starting session right now" do
        in_client(user1) do
          find('li[create-type="immediately"] ins').trigger(:click)
          find('div[info-id="immediately"]')
          page.should have_css(".session-stepnumber", :count => 5)
          page.should have_css(".session-stepnumber.session-stepactive", :count => 1)
        end
      end

      it "select option for starting quick session" do
        in_client(user1) do
          find('li[create-type="quick-start"] ins').trigger(:click)
          find('div[info-id="quick-start"]')
          page.should have_css(".session-stepnumber", :count => 2)
          page.should have_css(".session-stepnumber.session-stepactive", :count => 1)
        end
      end
    end

    describe "step 2" do
      before(:each) do
        in_client(user1) do
          find('li[create-type="schedule-future"] ins').trigger(:click)
          find('.btn-next').trigger(:click)
        end
      end

      it "initial status" do
        in_client(user1) do
          find('.session-step-title', text: 'What Are You Playing?')
          page.should have_css(".btn-back")
          page.should have_css(".btn-next")
          page.should have_css(".btn-help")
        end
      end

      it "validates genre, name and description" do
        in_client(user1) do
          find('.btn-next').trigger(:click)
          find('ul.error-text li', text: 'You must select a genre.')
          find('ul.error-text li', text: 'Name is required')
          find('ul.error-text li', text: 'Description is required')
        end
      end
    end

    describe "step 3" do
      before(:each) do
        in_client(user1) do
          find('li[create-type="schedule-future"] ins').trigger(:click)
          find('.btn-next').trigger(:click)

          jk_select("Alternative Rock", '#create-session-form select[name="genres"]')
          fill_in('session-name', :with => "Test Name")
          fill_in('session-description', :with => "Test Description")
          find('.btn-next').trigger(:click)
        end
      end

      it "initial status" do
        find('.session-step-title', text: 'Who Is Playing With You?')
        find('input#session-plus-musicians[type="checkbox"][checked="checked"]')

        expect( find(:css, 'select#session-language-list', :visible => false).value ).to eq('eng')
        page.should have_css(".session-stepnumber", :count => 5)
        page.should have_css(".session-stepnumber.session-stepactive", :count => 1)

        page.should have_css(".btn-back")
        page.should have_css(".btn-next")
        page.should have_css(".btn-help")
      end
    end

    describe "step 4" do
      before(:each) do
        in_client(user1) do
          find('li[create-type="schedule-future"] ins').trigger(:click)
          find('.btn-next').trigger(:click)

          jk_select("Alternative Rock", '#create-session-form select[name="genres"]')
          fill_in('session-name', :with => "Test Name")
          fill_in('session-description', :with => "Test Description")
          find('.btn-next').trigger(:click)

          find('.btn-next').trigger(:click)
        end
      end

      it "initial status" do
        find('.session-step-title', text: 'What Are Your Policies?')
        find('input#session-policy-standard[type="radio"][checked="checked"]')
        find('input#session-policy-confirm[type="checkbox"]')

        expect( find(:css, 'select#session-musician-access', :visible => false).value ).to eq('musicians')
        expect( find(:css, 'select#session-fans-access', :visible => false).value ).to eq('listen-chat-each')
        page.should have_css('span.selected', text: "Musicians may join at will")
        page.should have_css('span.selected', text: "Fans may listen, chat with each other")

        page.should have_css(".btn-back")
        page.should have_css(".btn-next")
        page.should have_css(".btn-help")
      end

      it "validates terms of service checkbox" do
        find('.btn-next').trigger(:click)

        find('ul.error-text li', text: 'You must accept the Session Policy.')
      end
    end

    describe "step 5" do
      before(:each) do
        in_client(user1) do
          find('li[create-type="schedule-future"] ins').trigger(:click)
          find('.btn-next').trigger(:click)

          jk_select("Alternative Rock", '#create-session-form select[name="genres"]')
          fill_in('session-name', :with => "Test Name")
          fill_in('session-description', :with => "Test Description")
          find('.btn-next').trigger(:click)

          find('.btn-next').trigger(:click)

          find('div#divSessionPolicy ins').trigger(:click)
          find('.btn-next').trigger(:click)
        end
      end


      it "initial status" do
        find('.session-step-title', text: 'Review & Confirm')
        find('em#session-name-disp', text: 'Test Name (Alternative Rock)')
        find('div#session-description-disp', text: 'Test Description')
        find('div#session-language-disp', text: 'English')
        find('div#session-invited-disp', text: 'Any interested JamKazam musicians who want to join us')
        find('div#session-instruments-me-disp', text: 'Other')
        find('div#session-instruments-rsvp-disp', text: 'Any Instrument Allowed')
        find('div#session-musician-access-disp', text: 'Musicians: Musicians may join at will')
        find('div#session-fans-access-disp', text: 'Fans: Fans may listen, chat with each other')
        find('div#session-policy-disp', text: 'Standard')

        page.should have_css(".btn-back")
        page.should have_css(".btn-next")
        page.should have_css(".btn-help")
      end
    end
  end
end
