require 'spec_helper'
require 'webmock/rspec'

describe "Find session latency badge", js: true, type: :feature, capybara_feature: true do
	let(:creator_user){ FactoryGirl.create(:user) }
	let(:finder_user){ FactoryGirl.create(:user) }
	let(:user1){ FactoryGirl.create(:user) }
	let(:user2){ FactoryGirl.create(:user) }
	let(:user3){ FactoryGirl.create(:user) }
	let(:user4){ FactoryGirl.create(:user) }
	let(:latency_data_uri) { /\S+\/user_latencies/ }

	def create_session(creator_user)
		in_client(creator_user) do
			fast_signin(creator_user, "/client#/createSession")
			wait_until_curtain_gone
			find("h1", text: "session")
			find(".quick-start-open").click
		end
		
	end

	def join_session(joiner_user)
		in_client(joiner_user) do
			fast_signin(joiner_user, "/client#/findSession")
			wait_until_curtain_gone
			find("h1", text: "find a session")
			find("a", text: "Open Jams").click
			find("#sessions-active .found-session .join-link").click
			find("#session-terms-conditions")
			within("#session-terms-conditions") do
				find("#btn-accept-terms").click
			end
			find("h1", text: "session")
		end
	end

	before(:all) do
		Capybara.default_max_wait_time = 10
	end

	describe "In public session" do

		before(:each) do     		
			emulate_client
			ActiveMusicSession.delete_all
			create_session(creator_user)
		end

		it "show GOOD" do
			in_client(finder_user) do
				creator_response_body = mock_latency_response([{ user: creator_user, ars_total_latency: 1.0, ars_internet_latency: 0.5, audio_latency: 0.5 }])
					
				stub_request(:post, latency_data_uri)
				.with(
					:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}
				)
				.to_return( body: creator_response_body, status: 200)
				
				fast_signin(finder_user, "/client#/findSession")
				wait_until_curtain_gone
				find("h1", text: "find a session")
				find("a", text: "Open Jams").click
				expect(page).to have_selector("#sessions-active .found-session table.musicians-category tr", count: 1)

				expect(page).to have_selector("div.latency-badge[data-user-id=\"#{creator_user.id}\"]", text: "GOOD")
				#expect(page).to have_selector("div.latency-badge[data-user-id=\"#{joiner_user.id}\"]", text: "GOOD")
				
				# selector = "#sessions-active a[data-user-id=\"#{creator_user.id}\"][data-hoveraction=\"musician\"]"
				# find(selector, text: creator_user.name).hover_intent
				# find('h3', text: creator_user.name)
				# find("#musician-latency-badge .latency", text: 'GOOD')
				# page.execute_script("$('#{selector}').mouseleave();")
				# sleep(1)

				# selector = "#sessions-active a[data-user-id=\"#{joiner_user.id}\"][data-hoveraction=\"musician\"]"
				# find(selector, text: joiner_user.name).hover_intent
				# find('h3', text: joiner_user.name)
				# find("#musician-latency-badge .latency", text: 'GOOD')
				# page.execute_script("$('#{selector}').mouseleave();")
				# sleep(1)
			end
		end

		it "show FAIR" do
			in_client(finder_user) do
				creator_response_body = mock_latency_response([{ user: creator_user, ars_total_latency: 41.0, ars_internet_latency: 21.0, audio_latency: 20.0 }]) 
		
				stub_request(:post, latency_data_uri)
				.with(
					:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}
				)
				.to_return( body: creator_response_body, status: 200)

				fast_signin(finder_user, "/client#/findSession")
				wait_until_curtain_gone
				find("h1", text: "find a session")
				find("a", text: "Open Jams").click
				expect(page).to have_selector("#sessions-active .found-session table.musicians-category tr", count: 1)

				expect(page).to have_selector("div.latency-badge[data-user-id=\"#{creator_user.id}\"]", text: "FAIR")
				#expect(page).to have_selector("div.latency-badge[data-user-id=\"#{joiner_user.id}\"]", text: "GOOD")
				
			end
		end

		it "show HIGH if latency > 70", focus: true do
			in_client(finder_user) do
				creator_response_body = mock_latency_response([{ user: creator_user, ars_total_latency: 71.0, ars_internet_latency: 41.0, audio_latency: 30.0 }]) 
		
				stub_request(:post, latency_data_uri)
				.with(
					:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}
				)
				.to_return( body: creator_response_body, status: 200)

				fast_signin(finder_user, "/client#/findSession")
				wait_until_curtain_gone
				find("h1", text: "find a session")
				find("a", text: "Open Jams").click
				expect(page).to have_selector("#sessions-active .found-session table.musicians-category tr", count: 1)

				expect(page).to have_selector("div.latency-badge[data-user-id=\"#{creator_user.id}\"]", text: "HIGH")
				#expect(page).to have_selector("div.latency-badge[data-user-id=\"#{joiner_user.id}\"]", text: "GOOD")
				
			end
		end

		it "show HIGH if latency > 100", focus: true do
			in_client(finder_user) do
				creator_response_body = mock_latency_response([{ user: creator_user, ars_total_latency: 101.0, ars_internet_latency: 51.0, audio_latency: 50.0 }]) 
		
				stub_request(:post, latency_data_uri)
				.with(
					:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}
				)
				.to_return( body: creator_response_body, status: 200)

				fast_signin(finder_user, "/client#/findSession")
				wait_until_curtain_gone
				find("h1", text: "find a session")
				find("a", text: "Open Jams").click
				expect(page).to have_selector("#sessions-active .found-session table.musicians-category tr", count: 1)

				expect(page).to have_selector("div.latency-badge[data-user-id=\"#{creator_user.id}\"]", text: "HIGH")
				#expect(page).to have_selector("div.latency-badge[data-user-id=\"#{joiner_user.id}\"]", text: "GOOD")
				
			end
		end

		it "show UNKNOWN" do
			in_client(finder_user) do
				creator_response_body = mock_latency_response([{ user: creator_user, ars_total_latency: -2, ars_internet_latency: 0, audio_latency: 0 }]) 
		
				stub_request(:post, latency_data_uri)
				.with(
					:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}
				)
				.to_return( body: creator_response_body, status: 200)

				fast_signin(finder_user, "/client#/findSession")
				wait_until_curtain_gone
				find("h1", text: "find a session")
				find("a", text: "Open Jams").click
				expect(page).to have_selector("#sessions-active .found-session table.musicians-category tr", count: 1)

				expect(page).to have_selector("div.latency-badge[data-user-id=\"#{creator_user.id}\"]", text: "UNKNOWN")
				#expect(page).to have_selector("div.latency-badge[data-user-id=\"#{joiner_user.id}\"]", text: "GOOD")
				
			end
		end

		it "show FAILED" do
			in_client(finder_user) do
				creator_response_body = mock_latency_response([{ user: creator_user, ars_total_latency: -3, ars_internet_latency: 0, audio_latency: 0 }]) 
		
				stub_request(:post, latency_data_uri)
				.to_raise("some error")

				fast_signin(finder_user, "/client#/findSession")
				wait_until_curtain_gone
				find("h1", text: "find a session")
				find("a", text: "Open Jams").click
				expect(page).to have_selector("#sessions-active .found-session table.musicians-category tr", count: 1)

				expect(page).to have_selector("div.latency-badge[data-user-id=\"#{creator_user.id}\"]", text: "FAILED")
				#expect(page).to have_selector("div.latency-badge[data-user-id=\"#{joiner_user.id}\"]", text: "GOOD")
				
			end
		end

		it "shows latency of joiners of the session" do
			response_body = mock_latency_response([
				{ user: creator_user, ars_total_latency: 1.0, ars_internet_latency: 0.4, audio_latency: 0.6 }, #GOOD
        { user: user1, ars_total_latency: 1.0, ars_internet_latency: 0.4, audio_latency: 0.6 }, #GOOD
        { user: user2, ars_total_latency: 71.0, ars_internet_latency: 35, audio_latency: 36 }, #HIGH
        { user: user3, ars_total_latency: 41.0, ars_internet_latency: 15, audio_latency: 26.0 }, #FAIR
        { user: user4, ars_total_latency: 101.0, ars_internet_latency: 31.75, audio_latency: 70.25 } #HIGH
        ])
      
      stub_request(:post, latency_data_uri)
      .with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'})
      .to_return( body: response_body, status: 200)
			
			[user1, user2, user3, user4].each do |user|
				join_session(user)
			end

			fast_signin(finder_user, "/client#/findSession")
			wait_until_curtain_gone
			find("h1", text: "find a session")
			find("a", text: "Open Jams").click
			expect(page).to have_selector("#sessions-active .found-session table.musicians-category tr", count: 5)

			expect(page).to have_selector("div.latency-badge[data-user-id=\"#{creator_user.id}\"]", text: "GOOD")
			expect(page).to have_selector("div.latency-badge[data-user-id=\"#{user1.id}\"]", text: "GOOD")
			expect(page).to have_selector("div.latency-badge[data-user-id=\"#{user2.id}\"]", text: "HIGH")
			expect(page).to have_selector("div.latency-badge[data-user-id=\"#{user3.id}\"]", text: "FAIR")
			expect(page).to have_selector("div.latency-badge[data-user-id=\"#{user4.id}\"]", text: "HIGH")
			save_screenshot("findSessionLatency.png")
		end

	end

end