'use strict';

var utils = require('../../lib/utils');
var User = require('./product-catalog.user');

describe('Admin app - product analysis metadata', function() {

  var browser1,
    browser2;

  var productName;

  var user1,
    user2;

  beforeAll(function() {
    browser1 = browser;
    browser2 = browser1.forkNewDriverInstance();

    user1 = new User({
      browser: browser1
    });

    user2 = new User({
      browser: browser2
    });

        return utils.login(null, null, browser2);
  });

  beforeEach(function() {

    productName = 'AT610/14';

    return user1.gotToCatalog()
      .then(function() {
        return user1.showPageContainingProductByProductName(productName);
      })
      .then(function() {
        return user2.gotToCatalog();
      })
      .then(function() {
        return user2.showPageContainingProductByProductName(productName);
      })
      .then(function() {
        return user1.openProductPanelByProductName(productName)
      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      });

  });

  describe('When user A and B access product catalog and open the same product', function() {

    it('Then should see a form with a select and a datepicker', function() {

      return user1.wait(2).then(function() {
        expect(browser1.element(by.css('.w-product-analysis-metadata')).isPresent()).toEqual(true);
        expect(browser2.element(by.css('.w-product-analysis-metadata')).isPresent()).toEqual(true);
        expect(browser1.element(by.css('.w-product-analysis-metadata .w-type-of-analysis-select')).isPresent()).toEqual(true);
        expect(browser2.element(by.css('.w-product-analysis-metadata .w-type-of-analysis-select')).isPresent()).toEqual(true);
        expect(browser1.element(by.css('.w-product-analysis-metadata .w-update-on-datepicker')).isPresent()).toEqual(true);
        expect(browser2.element(by.css('.w-product-analysis-metadata .w-update-on-datepicker')).isPresent()).toEqual(true);
      });


    });

    describe('When user A select an option from “typeOfAnalysis” select', function() {

      beforeEach(function() {
        return user1.selectTypeOfAnalysis(0, 1);
      });

      it('Then user B should be updated on the modification of “typeOfAnalysis” field', function() {
        return user2.readTypeOfAnalysis(0)

          .then(function(typeOfAnalysis) {
            expect(typeOfAnalysis).toEqual('Planned One-time deep dives');
          });
      });
    });

    describe('When user A pick a date from “updatedOn” datepicker', function() {

      beforeEach(function() {
        return user1.selectUpdatedOnDate(0);
      });

      it('Then user B should be updated on the modification of “updatedOn” field', function() {
        return user2.readUpdatedOnDate(0)

          .then(function(dateLabel) {
            var regex = /[A-Z][a-z]+, [A-Z][a-z]+ [0-9]{1,2}, [0-9]{4}/gi;
            expect(dateLabel).toMatch(regex); // To be changed according to the current month. Current month is April
          });
      });
    });
  });
});