/* jshint node: true */
/* jshint esnext: true */
/* global describe, beforeAll, beforeEach, afterEach, afterAll, it, expect */
'use strict';

const helper = require('./journeyHelper');
const config = require('config');
const crypto = require('crypto');
const participants = require('../service/participants');
const registration = require('../service/registration');
const participant = require('../domain/participant');
const _ = require('lodash');

describe('admin page', () => {

  let loginUrl = helper.paceUrl + 'login';
  let originalRegistrationStatus;

  beforeAll((done) => {
    registration.isClosed().then(isClosed => {
      originalRegistrationStatus = isClosed;
      done();
    });
  });

  beforeEach((done) => {
    helper.changeOriginalTimeout();
    helper.setupDbConnection(done);
  });

  afterEach((done) => {
    helper.resetToOriginalTimeout();
    helper.closeDbConnection(done);
  });

  afterAll((done) => {
    if (originalRegistrationStatus) {
      registration.close().then(() => {
        done();
      });
    } else {
      registration.reopen().then(() => {
        done();
      });
    }
  });

  function loginAdmin() {
    return helper.setUpClient().url(loginUrl)
      .setValue('input#username', config.get('admin.username'))
      .setValue('input#password', config.get('admin.password'))
      .click('button#submit');
  }

  it('should go to admin page, show statistics and generate start number buttons (registered and on-site numbers)', (done) => {
    loginAdmin().url(helper.paceUrl + 'admin')
      .isVisible('h3#admin_tshirts_count')
      .then(function (isVisible) {
        expect(isVisible).toBe(true);
      })
      .isVisible('button#generate-start-numbers')
      .then(function (isVisible) {
        expect(isVisible).toBe(true);
      })
      .isVisible('button#generate-on-site-start-numbers')
      .then(function (isVisible) {
        expect(isVisible).toBe(true);
      })
      .isVisible('input#amountOnSite')
      .then(function (isVisible) {
        expect(isVisible).toBe(true);
      })
      .isVisible('span#currentNumOfOnSite')
      .then(function (isVisible) {
        expect(isVisible).toBe(true);
      })
      .end(done);
  });

  it('should close and reopen the registration', (done) => {
    loginAdmin().url(helper.paceUrl + 'admin')
      .click('button#close-registration')
      .isVisible('p#registration-closed-message')
      .then((isVisible) => {
        expect(isVisible).toBe(true);
      })
      .click('button#reopen-registration')
      .isVisible('h3#admin_tshirts_count')
      .then((isVisible) => {
        expect(isVisible).toBe(true);
      })
      .end(done);
  });

  it('should redirect to login page if the user is not logged in', (done) => {
    helper.setUpClient().url(helper.paceUrl + 'admin')
      .isVisible('form#loginForm')
      .then(function (isVisible) {
        expect(isVisible).toBe(true);
      })
      .end(done);
  });

  it('should redirect to the start page after logout', (done) => {
    helper.setUpClient().url(helper.paceUrl + 'logout')
      .isVisible('h3*=Online-Anmeldung')
      .then((isVisible) => {
        expect(isVisible).toBe(true);
      })
      .end(done);
  });

  function givenAValidUserExists() {
    let randomString = crypto.randomBytes(8).toString('hex');

    let aParticipant = participant.from({
      firstname: 'Friedrich',
      lastname: 'Schiller',
      email: randomString + '@example.com',
      category: 'f',
      birthyear: 1980,
      team: 'Crazy runners',
      visibility: 'no'
    }).withToken(randomString).withSecureId('secureIdForTheEditLink').withStartNr(10);

    return participants.save(aParticipant);
  }

  it('should go to edit user when clicking edit button (admin is signed in)', (done) => {
    var firstName = 'not set yet';
    var lastName = 'not set yet';

    givenAValidUserExists().then(() => {
      loginAdmin().url(helper.paceUrl + 'admin/participants')
        .getText('.first-name')
        .then((firstNames) => {
          firstName = _.isArray(firstNames) ? firstNames[0] : firstNames;
        })
        .getText('.last-name')
        .then((lastNames) => {
          lastName = _.isArray(lastNames) ? lastNames[0] : lastNames;
        })
        .click('a.edit-button')
        .isVisible('#firstname')
        .then(isVisible => {
          expect(isVisible).toBe(true);
        })
        .getValue('#firstname')
        .then((value) => {
          expect(value).toBe(firstName);
        })
        .getValue('#lastname')
        .then((value) => {
          expect(value).toBe(lastName);
        })
        .end(done);
    });
  });

  it('should redirect to login page when an unauthenticated user requests admin edit page', (done) => {
    givenAValidUserExists().then(() => {
      helper.setUpClient().url(helper.paceUrl + 'admin/editparticipant/secureIdForTheEditLink')
        .isVisible('form#loginForm')
        .then(function (isVisible) {
          expect(isVisible).toBe(true);
        })
        .end(done);
    });
  });

  it('should be able to define the start time of 2 blocks', (done) => {
    loginAdmin().url(helper.paceUrl + 'admin')
      .click('a#after')
      .isVisible('div#new_block')
      .then((isVisible) => {
        expect(isVisible).toBe(true);
      })
      .setValue('input#name0', 'Startblock Rot')
      .setValue('input#hours0', '10')
      .setValue('input#minutes0', '15')
      .setValue('input#seconds0', '10')
      .click('button#set_race_starttime')
      .getValue('input#hours1')
      .then((value) => {
        expect(value).toBe('10');
      })
      .getValue('input#minutes1')
      .then((value) => {
        expect(value).toBe('15');
      })
      .getValue('input#seconds1')
      .then((value) => {
        expect(value).toBe('10');
      }).end(done);
  });


});

@@ -5165,143 +5165,15 @@
 dmin
-')%0A      .click('a#after')%0A      .isVisible('div#new_block')%0A      .then((isVisible) =%3E %7B%0A        expect(isVisible).toBe(true);%0A      %7D
+/after'
 )%0A  

make functional tests less flaky