require 'spec_helper'

describe "Account", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user) { FactoryGirl.create(:user, traditional_band: true,paid_sessions: true, paid_sessions_hourly_rate: 1,  paid_sessions_daily_rate:1 ) }
  let(:jam_track) {FactoryGirl.create(:jam_track)}

  before(:each) do
    UserMailer.deliveries.clear
    emulate_client
    sign_in_poltergeist user
    visit "/client#/account"
    
    find('div.account-mid.identity')
  end

  it { should have_selector('h1', text: 'my account') }

  describe "identity" do
    before(:each) do
      find("#account-edit-identity-link").trigger(:click)
    end

    it {
      find('#account-identity h2', text: 'identity:')
      should have_selector('form#account-edit-email-form h4', text: 'Update your email address:')
      should have_selector('form#account-edit-password-form h4', text: 'Update your password:')
    }

    describe "update email" do

      before(:each) do
        find('form#account-edit-email-form h4', text: 'Update your email address:')
        fill_in "account_update_email", with: "junk@jamkazam.com"
        #sleep 1
        find("#account-edit-email-submit").trigger(:click)
        fill_in "update-email-confirm-password", with: user.password
        find("#account-edit-email-confirm-password-submit").trigger(:click)

      end

      it {
          find('#notification h2', text: 'Confirmation Email Sent')
      }
    end

    describe "update password" do

      describe "successfully" do

        before(:each) do
          fill_in "current_password", with: user.password
          fill_in "password", with: user.password
          fill_in "password_confirmation", with: user.password
          find("#account-edit-password-submit").trigger(:click)
        end

        it { should have_selector('h1',    text: 'my account') }
      end

      describe "unsuccessfully" do

        before(:each) do
          find('#account-identity h2',    text: 'identity:')
          find("#account-edit-password-submit").trigger(:click)
        end

        it {
          find('#account-identity h2',    text: 'identity:')
          find('#account-identity div.field.error input[name=current_password] ~ ul li',    text: "can't be blank")
          find('#account-identity div.field.error input[name=password] ~ ul li',    text: "is too short (minimum is 6 characters)")
          find('#account-identity div.field.error input[name=password_confirmation] ~ ul li',    text: "can't be blank")
        }
      end
    end

    describe "profile" do

      before(:each) do
        find(".account-edit-profile-link").trigger(:click)
        find('a.small', text: 'Change Avatar')
      end

      describe "successfully" do

        before(:each) do
          fill_in "first_name", with: "Bobby"
          fill_in "last_name", with: "Toes"
          uncheck('subscribe_email')
          find(".account-edit-profile-submit").trigger(:click)
        end

        it {
          user.subscribe_email.should be true # we haven't user.reload yet
          find('h2',    text: 'edit profile: musical experience')
          user.reload
          user.subscribe_email.should be false
          user.first_name.should == "Bobby"
          user.last_name.should == "Toes"
          should have_selector('#profile #user', text: 'Bobby Toes')


          # Update birth date and check updated birth date 
          
          jk_select("Jan", '#account-edit-profile-form #user_birth_date_2i')
          jk_select("12", '#account-edit-profile-form #user_birth_date_3i')
          jk_select("1960", '#account-edit-profile-form #user_birth_date_1i')
          find("#account-edit-profile-form .account-edit-profile-submit").trigger(:click)
          find('label', text: 'Concert Gigs Played')
          find("#account-edit-profile-experience-form .account-edit-profile-submit").trigger(:click)
          find('.interest-options.traditional-band .yes-option ins.iCheck-helper').trigger(:click)
          find('label', text: 'Touring Option')
          find('.interest-options.paid-sessions .yes-option ins.iCheck-helper').trigger(:click)
          fill_in "paid_sessions_hourly_rate", with: "1.00"
          fill_in "paid_sessions_daily_rate", with: "1.00"

          find("#account-edit-profile-interests-form .account-edit-profile-submit").trigger(:click)
          find('label', text: 'Website (URL):')
          find("#account-edit-profile-samples-form .account-edit-profile-submit").trigger(:click)
          
          user.reload
          user.birth_date == "1960-01-12"

          #should have_selector('#account-edit-profile-form .birth_date li.active', text: "Jan")
          #should have_selector('#account-edit-profile-form .birth_date li.active', text: "12")
          #should have_selector('#account-edit-profile-form .birth_date li.active', text: "1960")

        }

      end

      describe "unsuccessfully" do

        before(:each) do
          fill_in "first_name", with: ""
          fill_in "last_name", with: ""
          find(".account-edit-profile-submit").trigger(:click)
        end

        it {
          should have_selector('h2',    text: 'profile:')
          should have_selector('div.field.error input[name=first_name] ~ ul li',    text: "can't be blank")
          should have_selector('div.field.error input[name=last_name] ~ ul li',    text: "can't be blank")
        }
      end
    end
  end

  describe "sessions" do

    before(:each) do
      @creator, @name, @desc = schedule_session

      visit "/client#/account"

      find("#account-scheduled-sessions-link").trigger(:click)
      find('h2', text: 'scheduled sessions:')
    end

    it "list scheduled sessions" do
      should have_selector('div.session-right', text: @name)
    end

    it "shows detail of scheduled session" do
      find("a.session-detail-button").trigger(:click)

      find('.session-properties-right', text: @name)
      find('td', text: @creator.name)
    end

    it "should update scheduled session" do
      find("a.session-detail-button").trigger(:click)
      find('a.button-orange.update-session', text: "UPDATE").trigger(:click)

      find('strong', text: "Date & Time")
      find('strong', text: "Other Properties")

      find('#session-prop-name').value.should eq @name

      # update session properties with new values
      date = Time.now.strftime "%a %e %B %Y"
      fill_in('session-prop-start-date', with: date)
      find('a.ui-state-default', text: '11').trigger(:click)
      jk_select('11:30 PM', '#account-session-properties-div #start-time-prop-list')
      jk_select('12:00 AM', '#account-session-properties-div #end-time-prop-list')
      jk_select('(GMT+00:00) Etc/UTC', '#account-session-properties-div #timezone-prop-list')
      jk_select('African', '#account-session-properties-div select[name="genres"]')
      fill_in('session-prop-name', with: "Updated Name")
      fill_in('session-prop-desc', with: "Updated Description")
      jk_select('German', '#account-session-properties-div #session-prop-lang-list')
      jk_select('Only RSVP musicians may join', '#account-session-properties-div #session-prop-musician-access')
      jk_select('Fans may not listen to session', '#account-session-properties-div #session-prop-fans-access')
      find('a#session-update').trigger(:click)

      should have_selector('h2', text: 'Session is successfully updated.')
      find('a.button-orange', text: "OKAY").trigger(:click)

      # check updated properties
      current_date = Time.now
      date_value = Time.new(current_date.year, current_date.mon, 11)
      date_string = date_value.strftime "%a %e %B %Y"

      find('#account-session-properties-div #session-prop-start-date').value.should eq date_string
      find('#account-session-properties-div .selected', text: '11:30 PM')
      find('#account-session-properties-div .selected', text: '12:30 AM')
      find('#account-session-properties-div .selected', text: '(GMT+00:00) Etc/UTC')
      find('#account-session-properties-div #session-prop-genre .selected', text: 'African')
      find('#account-session-properties-div #session-prop-name').value.should eq "Updated Name"
      find('#account-session-properties-div #session-prop-desc').value.should eq "Updated Description"
      find('#account-session-properties-div .selected', text: 'German')
      find('#account-session-properties-div .selected', text: 'Only RSVP musicians may join')
      find('#account-session-properties-div .selected', text: 'Fans may not listen to session')

      # try to update without changing the fields
      find('a#session-update').trigger(:click)

      should have_selector('h2', text: 'Session is successfully updated.')
      find('a.button-orange', text: "OKAY").trigger(:click)

      # check fields with original values
      find('#account-session-properties-div #session-prop-start-date').value.should eq date_string
      find('#account-session-properties-div .selected', text: '11:30 PM')
      find('#account-session-properties-div .selected', text: '12:30 AM')
      find('#account-session-properties-div .selected', text: '(GMT+00:00) Etc/UTC')
      find('#account-session-properties-div #session-prop-genre .selected', text: 'African')
      find('#account-session-properties-div #session-prop-name').value.should eq "Updated Name"
      find('#account-session-properties-div #session-prop-desc').value.should eq "Updated Description"
      find('#account-session-properties-div .selected', text: 'German')
      find('#account-session-properties-div .selected', text: 'Only RSVP musicians may join')
      find('#account-session-properties-div .selected', text: 'Fans may not listen to session')

    end

    it "cancel a scheduled sessions" do
      find("a.session-cancel-button").trigger(:click)

      should have_selector('h1', 'cancel session')
      find("a#btnCancelSession", text: "CANCEL SESSION").trigger(:click)

      should_not have_selector('tr')
    end

  end
end
