'use strict';
var utils = require('../../lib/utils');
var User = require('./product-catalog.user');
var EC = protractor.ExpectedConditions;


describe('AdminApp - Admin Product Catalog - Concurrent Editing', function() {
  var browser1,
    browser2;
  var helper;
  var user1,
    user2;
  var user1Waiting,
    user2Waiting;
  beforeAll(function() {
    browser1 = browser;
    browser2 = browser1.forkNewDriverInstance();
    return utils.login(null, null, browser2);
  });

  beforeEach(function() {
    helper = require('../../lib/css-selector-builder');
    user1 = new User({
      browser: browser1
    });
    user2 = new User({
      browser: browser2
    });
    user1Waiting = 5 * 1000;
    user2Waiting = 5 * 1000;
  });
  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);
      })
      .then(function() {
        return user2.takeScreenshot('after opening catalog')
      });

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



  xit('when a user assigns sku  of "AT620/14" to a review title, then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    return user1.wait(5)
      .then(function() {
        return user1.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      })
      .then(function() {
        // label-user-language-assignment
        expect(user1.readFirstAssignmentSkuToReviewTitle()).toEqual('AT620/14');
        return user1.selectSkuToAssign('generic-sku');

      })
      .then(function() {
        return user1.wait(5);
      })
      .then(function() {
        expect(user1.readFirstAssignmentSkuToReviewTitle()).toEqual('generic-sku');
        return user2.wait(5);

      })
      .then(function() {
        console.log('Waiting for 5 secs...')
        return user2.browser.wait(EC.textToBePresentInElement(element.all(by.xpath(helper.getFirstAssignmentSkuToReviewTitleSelector(productName))).get(0).$('option:checked'), 'generic-sku'), 5000);
      })
      .then(function() {
        return user2.scrollToElement($('.sku-select'));
      })
      .then(function() {
        return user2.takeScreenshot('changing-skus');
      })
      .then(function() {
        console.log('Reading sku...')
        expect(user2.readFirstAssignmentSkuToReviewTitle()).toEqual('generic-sku');

      });


  }).pend('IMPORTANT, BUT IT DOES NOT WORK AND IT IS NOT EASY TO DETERMINE THE CAUSE ');



  it('when a user updates channels of "AT620/14" to an analyst, then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var initialChannels = ['amazon.in', 'flipkart.com'];
    var expectedChannels = ['philips.co.in', 'flipkart.com', 'amazon.in'];
    return browser.sleep(user1Waiting)
      .then(function() {
        return user1.openProductPanelByProductName(productName);

      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user1.scrollTo($('.label-channel'));
      })
      // .then(function(){
      //   return user1.waitForVisibleElement($('.label-channel'));
      // })
      .then(function() {
        return user1.takeScreenshot('channels.1')
      })
      .then(function() {
        return user1.readChannels();
      })
      .then(function(channels) {
        expect(channels).toEqual(initialChannels);
        return browser.sleep(user1Waiting);
      })
      .then(function() {
        return user1.updateChannels();
      })
      .then(function() {
        return browser.sleep(2*1000);
      })
      .then(function() {
        return user1.takeScreenshot('channels.2')
      })
      .then(function() {
        expect(user1.readChannels()).toEqual(expectedChannels);
        return browser.sleep(user2Waiting);
      })
      .then(function() {
        expect(user2.readChannels()).toEqual(expectedChannels);
      });
  });
  it('when a user updates min and max date of "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var maxDate = 'Max date:Sep 15, 2013',
      minDate = 'Min date:Aug 8, 2013';
    var newMaxDate = 'Max date:Sep 15, 2014',
      newMinDate = 'Min date:Aug 7, 2013';
    browser.sleep(user1Waiting);
    user1.openProductPanelByProductName(productName);
    user2.openProductPanelByProductName(productName);
    expect(user1.readMinDate()).toBe(minDate);
    expect(user1.readMaxDate()).toBe(maxDate);
    expect(user2.readMinDate()).toBe(minDate);
    expect(user2.readMaxDate()).toBe(maxDate);
    user1.clickToUpdateMinAndMaxDate();
    browser.sleep(user1Waiting);
    expect(user1.readMinDate()).toBe(newMinDate);
    expect(user1.readMaxDate()).toBe(newMaxDate);
    browser.sleep(user2Waiting);
    expect(user2.readMinDate()).toBe(newMinDate);
    expect(user2.readMaxDate()).toBe(newMaxDate);
  });

  it('when a user updates languages of "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var initialChannels = ['gb'];
    var expectedChannels = ['gb', 'ge'];
    browser.sleep(user1Waiting);
    user1.openProductPanelByProductName(productName);
    user2.openProductPanelByProductName(productName);
    expect(user1.readLanguages()).toEqual(initialChannels);
    user1.clickToUpdateLanguages();
    browser.sleep(user1Waiting);
    expect(user1.readLanguages()).toEqual(expectedChannels);
    browser.sleep(user2Waiting);
    expect(user2.readLanguages()).toEqual(expectedChannels);
  });
  it('when a user updates avg rating of "AT620/14" to an analyst, then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var initialRating = '4';
    var expectedRating = '4.115384615384615';
    return browser.sleep(user1Waiting)
      .then(function() {
        return user1.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);

      })
      .then(function() {
        expect(user1.readAvgRating()).toEqual(initialRating);
        return user1.clickToUpdateRating();

      })
      .then(function() {
        return browser.sleep(user1Waiting);
      })
      .then(function() {
        expect(user1.readAvgRating()).toEqual(expectedRating);
        return browser2.sleep(user2Waiting);
      })
      .then(function() {
        expect(user2.readAvgRating()).toEqual(expectedRating);
      });
  });
  it('when a user set Wonderboard visibility for "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    return user1.wait(1)
      .then(function() {
        return user1.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      })
      .then(function() {
        expect(user1.readWonderboardVisibility()).toBeTruthy();
        return user1.clickToUpdateWonderboardVisibility();
      })
      .then(function() {
        return user1.wait(1);
      })
      .then(function() {
        expect(user1.readWonderboardVisibility()).toBeFalsy();
        return user2.wait(2);
      })
      .then(function() {
        expect(user2.readWonderboardVisibility()).toBeFalsy();
      });



  });
  it('when a user set AnnotationApp visibility for "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    browser.sleep(user1Waiting);
    user1.openProductPanelByProductName(productName);
    user2.openProductPanelByProductName(productName);
    expect(user1.readAnnotationAppVisibility()).toBeTruthy();
    user1.clickToUpdateAnnotationAppVisibility();
    expect(user1.readAnnotationAppVisibility()).toBeFalsy();
    browser.sleep(user2Waiting + 5000);
    browser2.sleep(user2Waiting + 5000);
    expect(user2.readAnnotationAppVisibility()).toBeFalsy();
  });
  it('when a user adds an SKU to "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    browser.sleep(user1Waiting);
    user1.openProductPanelByProductName(productName);
    user2.openProductPanelByProductName(productName);
    expect(user1.readSkus()).toEqual(['AT620/14  X', 'generic-sku  X']);
    user1.setSkuToAdd('new');
    user1.clickToAddSku();
    browser.sleep(user1Waiting);
    expect(user1.readSkus()).toEqual(['AT620/14  X', 'generic-sku  X', 'new  X']);
    browser.sleep(user2Waiting);
    expect(user2.readSkus()).toEqual(['AT620/14  X', 'generic-sku  X', 'new  X']);

  });
});