'use strict';

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

// ignored because refering to an old "failed" feature
xdescribe('AdminApp - Product catalog', function() {

  var browser1,
    browser2;

  var productName = 'AT610/14';

  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, browser1)
      .then(function() {
        return utils.login(null, null, browser2);
      });

  });

  beforeEach(function() {
    var productName = 'AT620/14';

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

  });

  afterAll(function() {
      return browser2.quit();
  });

  describe('Add sku to product', function() {

    beforeAll(function() {
      return user1.openProductPanelByProductName(productName)
        .then(function() {
          return user2.openProductPanelByProductName(productName);
        })
        .then(function() {
          return user1.addNewSkuToProduct(2, 'pluto');

        });
    });

    it('User A should see the new sku in the skus table', function() {

      return browser1.element.all(by.css('.w-product-sku-section:nth-child(2) .w-sku-table-entry'))
        .then(function(tableEntries) {
          expect(tableEntries.length).toEqual(2);
          return browser1.element.all(by.css('.w-product-sku-section:nth-child(2) .w-sku-table-entry:nth-child(1) td')).getInnerHtml();
        })
        .then(function(innerHtml) {
          expect(innerHtml[0]).toEqual('pluto');
        });
    });

    it('User B should see the new sku in the skus table', function() {
      return browser2.element.all(by.css('.w-product-sku-section'))

        .then(function(skuSections) {
          return skuSections[2].all(by.css('.w-sku-table-entry'));
        })

        .then(function(tableEntries) {
          expect(tableEntries.length).toEqual(2);
          return tableEntries[1].all(by.css('td')).getInnerHtml();
        })

        .then(function(innerHtml) {
          expect(innerHtml[0]).toEqual('pluto');
        });
    });

  });

  describe('Edit min price range of sku', function() {

    it('User A and B should see the updated value of min price range of the selected sku', function() {

      return user1.editMinPriceRangeOfSku(2, 0, 20)

        .then(function() {
          return browser1.element.all(by.css('.w-product-sku-section'));
        })

        .then(function(skuSections) {
          return skuSections[2].all(by.css('.w-sku-table-entry'));
        })

        .then(function(tableEntries) {
          return tableEntries[0].element(by.css('.w-sku-min-price-input')).getAttribute('value');
        })

        .then(function(inputValue) {
          expect(inputValue).toEqual('20');
        })

        .then(function() {
          return browser2.element.all(by.css('.w-product-sku-section'));
        })

        .then(function(skuSections) {
          return skuSections[2].all(by.css('.w-sku-table-entry'));
        })

        .then(function(tableEntries) {
          return tableEntries[0].element(by.css('.w-sku-min-price-input')).getAttribute('value');
        })

        .then(function(inputValue) {
          expect(inputValue).toEqual('20');
        });
    });

  });

  describe('Edit max price range of sku', function() {

    it('User A and B should see the updated value of max price range of the selected sku', function() {

      return user1.editMaxPriceRangeOfSku(2, 0, 30)

        .then(function() {
          return browser1.element.all(by.css('.w-product-sku-section'));
        })

        .then(function(skuSections) {
          return skuSections[2].all(by.css('.w-sku-table-entry'));
        })

        .then(function(tableEntries) {
          return tableEntries[0].element(by.css('.w-sku-max-price-input')).getAttribute('value');
        })

        .then(function(inputValue) {
          expect(inputValue).toEqual('30');
        })

        .then(function() {
          return browser2.element.all(by.css('.w-product-sku-section'));
        })

        .then(function(skuSections) {
          return skuSections[2].all(by.css('.w-sku-table-entry'));
        })

        .then(function(tableEntries) {
          return tableEntries[0].element(by.css('.w-sku-max-price-input')).getAttribute('value');
        })

        .then(function(inputValue) {
          expect(inputValue).toEqual('30');
        });
    });

  });

  describe.skip('Edit price category of sku', function() {

    it('User A and B should see the updated value of price category of the selected sku', function() {

      return user1.editPriceCategoryOfSku(2, 0, 1)

        .then(function() {
          return browser1.element.all(by.css('.w-product-sku-section'));
        })

        .then(function(skuSections) {
          expect(skuSections[2].element(by.css('.w-sku-price-category-select')).$('option:checked').getText()).toEqual('Mid End');
        })

        .then(function() {
          return browser1.element.all(by.css('.w-product-sku-section'));
        })

        .then(function(skuSections) {
          expect(skuSections[2].element(by.css('.w-sku-price-category-select')).$('option:checked').getText()).toEqual('Mid End');
        });
    });
  });
});