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

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);
      });

  });

  // beforeEach(function() {
  //     browser.sleep(5 * 1000).then(function() {
  //         return browser2.sleep(5 * 1000);
  //     });
  // });
  afterAll(function() {
    return browser2.quit();
  });

  it('when a user updates the brand name of "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var brandName = 'Philips';
    return browser.sleep(user1Waiting)
      .then(function() {
        return user1.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      })
      .then(function() {
        expect(user1.readBrandNameInputValue()).toBe(brandName);
        expect(user2.readBrandNameInputValue()).toBe(brandName);
        return user1.appendToBrandName(' ciaociaociaociao');
      })
      .then(function() {
        console.log('Waiting some seconds...')
        return browser.sleep(user1Waiting);
      })
      .then(function() {
        expect(user1.readBrandNameInputValue()).toBe(brandName + ' ciaociaociaociao');
        return browser2.sleep(user2Waiting);
      })
      .then(function() {
        expect(user2.readBrandNameInputValue()).toBe(brandName + ' ciaociaociaociao');
        return browser.waitForAngular();
      });
  });

  it('when a user updates the product name of "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    browser.sleep(user1Waiting)
      .then(function() {
        return user1.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      })
      //   .then(function(){
      //       return user1.wait(2);
      //   })
      //   .then(function(){
      //       return user2.wait(2);
      //   })
      .then(function() {
        expect(user1.readProductNameInputValue()).toBe(productName);
        expect(user2.readProductNameInputValue()).toBe(productName);
        return user1.appendToProductName(' ciaociaociaociao');

      })
      .then(function() {
        return browser1.sleep(user1Waiting);
      })
      .then(function() {
        expect(user1.readProductNameInputValue()).toBe('AT620/14 ciaociaociaociao');
        return browser2.sleep(user2Waiting);
      })
      .then(function() {
        expect(user2.readProductNameInputValue()).toBe('AT620/14 ciaociaociaociao');
      });



  });

  it('when a user updates the imageUrl of "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var imageUrl = 'https://dashboard.wonderflow.co/static/product-images/philips_at620_14.jpg';
    browser.sleep(user1Waiting)
      .then(function() {
        return user1.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      })
      .then(function() {
        return user1.wait(2);
      })
      .then(function() {
        expect(user1.readImageUrl()).toBe(imageUrl);
        expect(user2.readImageUrl()).toBe(imageUrl);
        return user1.appendToImageUrl(' ciaociaociaociao');
      })
      .then(function() {
        return user1.wait(2);
      })
      .then(function() {
        expect(user1.readImageUrl()).toBe(imageUrl + ' ciaociaociaociao');
        return browser.sleep(user2Waiting);
      }).then(function() {
        expect(user2.readImageUrl()).toBe(imageUrl + ' ciaociaociaociao');
      });



  });

  it('when a user updates number of reviews of "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    return browser.sleep(user1Waiting)
      .then(function() {
        return user1.openProductPanelByProductName(productName);

      })
      .then(function() {
        return user2.openProductPanelByProductName(productName);
      })
      .then(function() {
        expect(user1.readTotalNumberOfReviews()).toBe('365');
        expect(user1.readNumberOfSampledReviews()).toBe('365');
        expect(user1.readNumberOfAnalyzedReviews()).toBe('365 (published=365 reviews, unpublished=0 reviews)');
        expect(user2.readTotalNumberOfReviews()).toBe('365');
        expect(user2.readNumberOfSampledReviews()).toBe('365');
        expect(user2.readNumberOfAnalyzedReviews()).toBe('365 (published=365 reviews, unpublished=0 reviews)');
        return user1.clickToUpdateNumberOfReviews();
      })
      .then(function() {
        return user1.pause();

      })
      .then(function() {
        expect(user1.readTotalNumberOfReviews()).toBe('364');
        expect(user1.readNumberOfSampledReviews()).toBe('364');
        expect(user1.readNumberOfAnalyzedReviews()).toBe('363 (published=362 reviews, unpublished=1 reviews)');
        return browser.sleep(user2Waiting);

      })
      .then(function() {
        expect(user2.readTotalNumberOfReviews()).toBe('364');
        expect(user2.readNumberOfSampledReviews()).toBe('364');
        expect(user2.readNumberOfAnalyzedReviews()).toBe('363 (published=362 reviews, unpublished=1 reviews)');
      })
  });

  it('when a user publishes all the unpublished reviews of "AT620/14", 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() {
        expect(user1.readTotalNumberOfReviews()).toBe('365');
        expect(user1.readNumberOfSampledReviews()).toBe('365');
        expect(user1.readNumberOfAnalyzedReviews()).toBe('365 (published=365 reviews, unpublished=0 reviews)');
        expect(user2.readTotalNumberOfReviews()).toBe('365');
        expect(user2.readNumberOfSampledReviews()).toBe('365');
        expect(user2.readNumberOfAnalyzedReviews()).toBe('365 (published=365 reviews, unpublished=0 reviews)');
        return user1.clickToPublishAnalyzedAndUnpublishedReviews();
      })
      .then(function() {
        return user1.wait(5);
      })
      .then(function() {
        expect(user1.readTotalNumberOfReviews()).toBe('364');
        expect(user1.readNumberOfSampledReviews()).toBe('364');
        expect(user1.readNumberOfAnalyzedReviews()).toBe('363 (published=363 reviews, unpublished=0 reviews)');
        return user2.wait(5);
      })
      .then(function() {
        expect(user2.readTotalNumberOfReviews()).toBe('364');
        expect(user2.readNumberOfSampledReviews()).toBe('364');
        expect(user2.readNumberOfAnalyzedReviews()).toBe('363 (published=363 reviews, unpublished=0 reviews)');
      });
    // user1.clickToUpdateNumberOfReviews();



  });

  it('when a user updates price range of "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    browser.sleep(user1Waiting);
    user1.openProductPanelByProductName(productName);
    return user2.openProductPanelByProductName(productName).then(function() {
        expect(user1.readPriceRangeMin()).toBe('23');
        expect(user1.readPriceRangeMax()).toBe('35');
        expect(user2.readPriceRangeMin()).toBe('23');
        expect(user2.readPriceRangeMax()).toBe('35');
        return user1.appendToPriceRangeMin('000');
      })
      .then(function() {
        return user1.appendToPriceRangeMax('000');
      })
      .then(function() {
        return user1.wait(3);
      })
      .then(function() {
        return user1.clickToUpdatePriceRange();
      })
      .then(function() {
        return user1.wait(5);
      })
      .then(function() {
        expect(user1.readPriceRangeMin()).toBe('23000');
        expect(user1.readPriceRangeMax()).toBe('35000');
        return user2.wait(5);
      })
      .then(function() {
        expect(user2.readPriceRangeMin()).toBe('23000');
        expect(user2.readPriceRangeMax()).toBe('35000');
      });

  });

  it('when a user adds a country to "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    browser.sleep(user1Waiting).then(function() {
        return user1.openProductPanelByProductName(productName);
      }).then(function() {
        return user2.openProductPanelByProductName(productName);
      }).then(function() {
        expect(user1.readCountries()).toEqual(['India']);
        return user1.setCountryToAdd('Italy');
      }).then(function() {
        return user1.clickToAddCountry();
      })
      .then(function() {
        return user1.wait(2);
      })
      .then(function() {
        expect(user1.readCountries()).toEqual(['India', 'Italy']);
        return user2.wait(2);
      }).then(function() {
        expect(user2.readCountries()).toEqual(['India', 'Italy']);
      });
  });

  it('when a user adds a company ID to "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';

    return user1.wait(2)
      .then(function() {
        user1.openProductPanelByProductName(productName);
        return user2.openProductPanelByProductName(productName);

      })
      .then(function() {
        expect(user1.readCompanyIDs()).toEqual(['philips']);
        return user1.setCompanyIdToAdd('me');
      })
      .then(function() {
        return user1.clickToAddCompanyId();
      })
      .then(function() {
        return user1.wait(2);
      })
      .then(function() {
        expect(user1.readCompanyIDs()).toEqual(['philips', 'me']);
        return user2.wait(2);

      })
      .then(function() {
        expect(user2.readCompanyIDs()).toEqual(['philips', 'me']);
      });
  });

  it('when a user adds a reviewTitle to "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var initialReviewTitles = ['Reviews of Philips Aquatouch AT620/14 Shaver For Men', 'Philips AquaTouch AT620/14 Men\'s Shaver (Personal Care)', 'AT620/14'];
    var expectedReviewTitles = ['new', 'Reviews of Philips Aquatouch AT620/14 Shaver For Men', 'Philips AquaTouch AT620/14 Men\'s Shaver (Personal Care)', 'AT620/14'];
    browser.sleep(user1Waiting);
    user1.openProductPanelByProductName(productName);
    user2.openProductPanelByProductName(productName);
    expect(user1.readReviewTitles()).toEqual(initialReviewTitles);
    user1.setReviewTitleToAdd('new');
    user1.clickToAddReviewTitle();
    browser.sleep(user2Waiting);
    expect(user1.readReviewTitles()).toEqual(expectedReviewTitles);
    browser.sleep(user2Waiting);
    expect(user2.readReviewTitles()).toEqual(expectedReviewTitles);
  });

  it('when a user sets Price Category 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.readPriceCategory()).toBe('Low End');
    user1.setPriceCategory('High End');
    browser1.sleep(user1Waiting);
    expect(user1.readPriceCategory()).toBe('High End');
    browser2.sleep(user2Waiting);
    expect(user2.readPriceCategory()).toBe('High End');
  });
  it('when a user sets Product Category to "AT620/14", then another user is notified of the same update', function() {
    var productName = 'AT620/14';
    var newCategory = 'smartphone';

    browser.sleep(user1Waiting);

    user1.openProductPanelByProductName(productName);
    user2.openProductPanelByProductName(productName);

    browser.sleep(user1Waiting);

    expect(user1.readProductCategory()).toBe('male grooming');

    user1.setProductCategory(newCategory);

    browser1.sleep(user1Waiting);
    expect(user1.readProductCategory()).toBe(newCategory);

    browser2.sleep(user2Waiting);
    expect(user2.readProductCategory()).toBe(newCategory);
  });


});