'use strict';

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

describe('Admin app - Use case: Assign reviews', function() {

  var browser1,
    browser2;
  var user1,
    user2;

  var productName;

  beforeAll(function() {
    browser1 = browser;
    browser2 = browser1.forkNewDriverInstance();
    return utils.login(null, null, browser2);
  });



  beforeEach(function() {
    user1 = new User({
      browser: browser1
    });
    user2 = new User({
      browser: 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);
      });

  });

  afterAll(function() {
    return browser1.get(utils.getEndPoint() + '/#/').then(function() {
      return browser2.quit();
    });
  });

  it('Language selector should not be displayed', function() {

    return browser1.element.all(by.css('.w-assign-reviews-form')).get(0)
      .element(by.css('.w-assign-reviews-language-select')).isDisplayed().then(function(result) {
      expect(result).toBe(false);
    });



  });



  describe('When admin A select a data source', function() {

    it('Then language selector should be present and show the number of reviews for that language', function() {

      return user1.selectDataSourceFromReviewsAssignement(0, 1)
        .then(function(){
          return user1.wait(4);
        })
        .then(function(){
          return user1.takeScreenshot('lang-select');
        })
        .then(function() {
          return browser1.element.all(by.css('.w-assign-reviews-form')).get(0).element(by.css('.w-assign-reviews-language-select')).isDisplayed();
        })
        .then(function(isDisplayed) {
          expect(isDisplayed).toBe(true);
          return browser1
                  .element.all(by.css('.w-assign-reviews-form .w-assign-reviews-language-select option'))
                  .get(1)
                  .getText();
        })
        .then(function(text) {
          expect(text.replace(/\n+/gi,'').trim()).toEqual('gb (4)');
        });
    });
  });

  describe('When admin A assign a review', function() {

    //var assignReviewsSection, assignReviewsSection1;

    it('Then admin A and B should see the new assignement in the list', function() {

      var initialAssignments;
      var finalAssingments;
      //var productName = 'AT610/14';

      initialAssignments = [];
      finalAssingments = ['chiara.lagana@wonderflow.co - gb - reviews X'];


      return user1.selectAnalystToAssign('chiara.lagana@wonderflow.co')
        .then(function() {
          return user1.selectDataSourceToAssign('reviews');
        })
        .then(function() {
          return user1.wait(1);
        })
        .then(function() {
          return user1.selectLanguageToAssign('gb (4)');
        })
        .then(function() {
          return user1.clickToAssignAnalyastToLanguage();
        })
        .then(function() {
          return user1.wait(1);
        })
        .then(function() {
          return user2.wait(1);
        })
        .then(function() {
          expect(user1.readAssignmentsAnalystToLanguage()).toEqual(finalAssingments);
          expect(user2.readAssignmentsAnalystToLanguage()).toEqual(finalAssingments);
        });



    });

    it('when a user deassigns reviews of "AT620/14" to an analyst, then another user is notified of the same update', function() {
      var initialAssignmenets,
        finalAssingments;
     
      initialAssignmenets = ['chiara.lagana@wonderflow.co - gb - reviews X'];
      finalAssingments = [];

      return user1.selectAnalystToAssign('chiara.lagana@wonderflow.co')
        .then(function() {
          return user1.selectDataSourceToAssign('reviews');
        })
        .then(function() {
          return user1.wait(1);
        })
        .then(function() {
          return user1.selectLanguageToAssign('gb (4)');
        })
        .then(function() {
          return user1.clickToAssignAnalyastToLanguage();
        })
        .then(function() {
          return user1.wait(1);
        })
        .then(function() {
          return user1.readAssignmentsAnalystToLanguage();
        })
        .then(function(assignmentsAnalystToLangugage) {
          expect(assignmentsAnalystToLangugage).toEqual(initialAssignmenets);
        })
        .then(function() {
          return user1.deAssignByAnalystAndLanguage('chiara.lagana@wonderflow.co', 'gb');
        })
        .then(function() {
          return user1.wait(1);
        })
        .then(function() {
          return user1.readAssignmentsAnalystToLanguage();
        })
        .then(function(assignmentsAnalystToLangugage) {
          expect(assignmentsAnalystToLangugage).toEqual(finalAssingments);
        })
        .then(function() {
          return user1.wait(5);
        })
        .then(function() {
          return user2.wait(5);
        })
        .then(function() {
          return user2.readAssignmentsAnalystToLanguage();
        })
        .then(function(assignmentsAnalystToLangugage) {
          expect(assignmentsAnalystToLangugage).toEqual(finalAssingments);

        });
    });

  });

});