'use strict';

var User = require('../../lib/user');
var helper = require('../../lib/css-selector-builder');
var Logger=require('logb').getLogger(module.filename);


function InsightListUser(opts) {
  opts = opts || {};
  User.call(this, opts);
}
InsightListUser.prototype = new User();
// insights-list, merge mode
InsightListUser.prototype.enableMergeMode = function() {
  return this.clickByCssSelector('.w-enable-merge-mode-button');
};
InsightListUser.prototype.selectFirst2Insights = function() {
  var self = this;
  return this.browser.element.all(by.css('.w-insight-item .w-insight-to-merge-checkbox'))
    .filter(function(el, index) {
      return index < 2;
    })
    .each(function(el) {
      return self.scrollToElement(el, -200)
        .then(function() {
          return el.click();
        });
    });
};
InsightListUser.prototype.selectInsightToMergeByIndex = function(index) {
  return this.browser.element.all(by.css('.w-insight-item .w-insight-to-merge-checkbox')).filter(function(el, i) {
    return index === i;
  }).each(function(el) {
    return el.click();
  });
};
InsightListUser.prototype.clickOnMergeButton = function() {
  return this.clickByCssSelector('.w-merge-insights-button');
};
InsightListUser.prototype.readInsightsToMergeOnModalWindow = function() {
  return this.browser.element.all(by.css('.modal-body .w-insight-to-merge-text')).filter(function(el, index) {
    return index < 2;
  }).map(function(el) {
    return el.getText();
  });
};
InsightListUser.prototype.readFirstTwoInsights = function() {
  return this.browser.element.all(by.css('.w-insight-item .w-insight-text-input')).map(function(el) {
    return el.getAttribute('value');
  });
};
InsightListUser.prototype.setTextOfNewInsight = function(text) {
  return this.browser.element(by.css('.w-text-of-new-insight')).sendKeys(text).then(function() {
    return browser.waitForAngular();
  });
};
InsightListUser.prototype.clickToApplyMerge = function() {
  return this.browser.element(by.css('.w-apply-merge')).click();
};

// /insights-list
InsightListUser.prototype.readTextOfFirstInsight = function() {
  return this.browser.element(by.xpath(helper.getInsightTextInput())).getAttribute('value');
};
InsightListUser.prototype.setTextOfFirstInsight = function(textToAppend) {
  return this.browser.element(by.xpath(helper.getInsightTextInput())).sendKeys(textToAppend);
};
InsightListUser.prototype.clickToSaveFirstInsight = function() {
  return this.browser.element(by.xpath(helper.getFirstInsightSaveButton())).click();
};
//
InsightListUser.prototype.readBrandName = function() {
  return this.browser.element(by.xpath(helper.getBrandNameSelector())).getText();
};
InsightListUser.prototype.readProductName = function() {
  return this.browser.element(by.xpath(helper.getProductNameSelector())).getText();
};
InsightListUser.prototype.readNumberOfPublishedInsights = function() {
  return this.browser.element(by.xpath(helper.getNumberOfPublishedInsightSelector())).getText();
};
InsightListUser.prototype.countNumberOfInsights = function() {
  return this.browser.element.all(by.xpath(helper.getInsightContainersSelector())).count();
};
InsightListUser.prototype.readFirst2Insights = function() {
  return this.browser.element.all(by.xpath(helper.getFirst2InsightsSelector())).map(function(el) {
    return el.getAttribute('value');
  });
};
InsightListUser.prototype.readNumberOfLinkedReviewsOfFirstInsight = function() {
  return this.browser.element(by.xpath(helper.getNumberOfLinkedReviewsOfFirstInsight())).getText();
};
InsightListUser.prototype.clickOnLinkedReviewsOfFirstInsight = function() {
  return this.browser.element(by.xpath(helper.getLinkOfLinkedReviewsOfFirstInsightSelector())).click();
};

InsightListUser.prototype.clickOnLinkedCallCenterDataByIndexOfInsightInTheList = function(index) {
  return this.browser.element.all(by.css('.w-number-of-linked-call-center-data'))
    .filter(function(el, i) {
      return index === i;
    }).get(0).click();
};
InsightListUser.prototype.readNumbersOfLinkedReviews = function() {
  return element.all(by.css('.w-number-of-linked-reviews'))
    .map(function(el) {
      return el.getText();
    });
};
InsightListUser.prototype.readNumberOfLinkedCallCenterData = function() {
  return element.all(by.css('.w-number-of-linked-call-center-data'))
    .map(function(el) {
      return el.getText();
    });
};
InsightListUser.prototype.readAuthorOfInsightByIndex = function(index) {
  var selector;
  selector = '.w-insight-author';
  // selector = '.w-insight-author:nth-child(' + index + ')';
  return this.browser.element.all(by.css(selector)).filter(function(el, i) {
    return index === i;
  }).first().getText();
};

// insight-list - publish/unpublish
InsightListUser.prototype.readStateOfFirstInsight = function() {
  Logger.info('readStateOfFirstInsight()');
  return this.browser.element(by.css('.w-insight-item:nth-of-type(1) .w-insight-state')).getText();
};
InsightListUser.prototype.readStateOfSecondInsight = function() {
  Logger.info('readStateOfSecondInsight()');

  return this.browser.element(by.css('.w-insight-item:nth-of-type(2) .w-insight-state')).getText();
};
InsightListUser.prototype.clickToPublishFirstInsight = function() {
  Logger.info('clickToPublishFirstInsight()');
  return this.browser.element(by.css('.w-insight-item:nth-of-type(1) .w-publish-insight')).click();
};
InsightListUser.prototype.clickToUnPublishSecondInsight = function() {  
  Logger.info('clickToUnPublishSecondInsight()');
  return this.browser.element(by.css('.w-insight-item:nth-of-type(2) .w-unpublish-insight')).click();
};

module.exports = InsightListUser;