-Browser = require 'zombie'
+wd = require 'wd'
 should = require 'should'
 request = require 'request'
+browser = wd.remote()
+wd40 = require('../wd40')(browser)
 
 BASE_URL = 'http://localhost:3001'
+LOGIN_URL = "#{BASE_URL}/login"
 
 # Overview
 # Login as teststaff, create a profile called ickletest, attempt to login.
 #
 # Switching logs in as user A, adds a dataset using the API.
-# Then we switch to zombie to switch the context.
+# Then we switch to a browser to switch the context.
 #
 # TODO: move Switching out into its own test file
 
 login  = (username, password, callback) ->
-  request.get "#{BASE_URL}/login", ->
+  request.get LOGIN_URL, ->
     request.post
-      uri: "#{BASE_URL}/login"
+      uri: LOGIN_URL
       form:
         username: username
         password: password

          form: form
        , (err, resp, body) =>
          obj = JSON.parse body
          @browser = new Browser()
          @browser.visit "#{BASE_URL}/set-password/#{obj.token}", =>
            @browser.wait done

    it 'shows a page with a password field', ->
      should.exist @browser.query('#password')

    context 'when I fill in my new password', ->
      before (done) ->
        @browser.fill '#password', newPass
        @browser.pressButton '#content .btn-primary', =>
          @browser.wait done

      it 'sets my password', ->
        should.exist @browser.query('.alert-success')


describe 'Switch', ->


      , done

  before (done) ->
    browser.deleteAllCookies done

  before (done) ->
    # Log in as B via zombie
    browser.get LOGIN_URL, ->
      wd40.fill '#username', staff_user, ->
        wd40.fill '#password', staff_pass, ->
          wd40.pressButton '#login', done

  context 'when a staff member switches context', ->
    before (done) ->
      browser.get "#{BASE_URL}/switch/#{nonstaff_user}", done

    it 'redirected to home page', (done) ->
      wd40.trueURL (err, url) ->
        url.should.equal "#{BASE_URL}/"
        done()

    it 'shows me datasets of the profile into which I have switched', (done) ->
      wd40.text '.dataset-list', (err, text) ->
        text.should.include dataset_name
        done()

    it "has the switched to profile's name", (done) ->
      wd40.text 'h1', (err, text) ->
        text.should.include 'Mr Ickle Test'
        done()

    it 'shows a gravatar', (done) ->
      browser.elementByCssIfExists 'h1 img', (err, img) ->
        img.src.should.include 'gravatar'
        done()

    it 'shows the context search box', (done) ->
      browser.waitForVisibleByCss '.context-switch', 4000, done

  context 'when a non-staff member attempts to switch context', ->
    before (done) ->
      browser.deleteAllCookies done

    before (done) ->
      browser.get LOGIN_URL, ->
        wd40.fill '#username', nonstaff_user, ->
          wd40.fill '#password', nonstaff_pass, ->
            wd40.click '#login', ->
              wd40.get "#{BASE_URL}/switch/#{staff_user}", ->
                wd40.get BASE_URL, done

    it "hasn't changed who I am", (done) ->
      wd40.text 'h1', (err, text) ->
        text.should.include 'Mr Ickle Test'
        wd40.text 'h1', (err, text) ->
          text.should.not.include 'Staff Test'
          done()

    it 'still shows me my datasets', (done) ->
      wd40.text '.dataset-list', (err, text) ->
        text.should.include dataset_name
        done()

    it "doesn't show the context switching popup", (done) ->
      browser.isVisible '.context-switch', (err, visible) ->
        visible.should.be.true
        done()

describe 'Whitelabel', ->

