'use strict';

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

// class ProductCatalogUser extends User {
//   constructor(opts) {
//     opts = opts || {};
//     User.call(this, opts);
//   }
//   gotToCatalog() {
//     return this.goTo('/catalog')
//       .then(() => {
//         var el = this.browser.element(by.css('.panel'));
//         return this.waitForVisibleElement($('.panel'));
//       })
//       .then(() => {
//         return this.takeScreenshot('just-went-to-catalog');
//       });
//   }
//   openProductPanelByProductName(productName) {
//   var el;
//   this.currentProductName = productName;
//   el = this.browser.element(by.xpath(helper.getPanelTitleSelectorByProductName(this.currentProductName)));
//   return self.waitForClickableElement(el)
//     .then(()=> {
//       return el.click();
//     })
//     .then(()=> {
//       return self.waitForVisibleElement($('.input-brand-name'),'Waiting for brand name after opened product panel');
//       //return self.browser.sleep(3*1000);
//     });
// }
// appendToImageUrl (str) {
//   Logger.info('Appending to image URL input...')
//   return this.browser.element(by.xpath(helper.getInputImageUrlSelectorByProductName(this.currentProductName))).sendKeys(str);
// }
// readImageUrl() {
//   Logger.info('Reading image URL...');
//   return this.browser.element(by.xpath(helper.getInputImageUrlSelectorByProductName(this.currentProductName))).getAttribute('value');
// }
// readCountries() {
//   return this.browser.element.all(by.xpath(helper.getCountriesSelector(this.currentProductName)))
//   .map((el)=> {
//     return el.getText();
//   })
// }
// clickToAddCountry () {
//   Logger.info('Clicking to add a country...')
//   return this.browser.element(by.xpath(helper.getCountryAddButton(this.currentProductName))).click();
// }
// selectTypeOfAnalysis (productIndex, optionIndex) {

//   var selectElement;

//   return this.browser.element.all(by.css('.w-product-analysis-metadata .w-type-of-analysis-select'))

//   .then((selectElements) =>{
//     selectElement = selectElements[productIndex];
//     return this.scrollToElement(selectElement);
//   })

//   .then(()=> {
//     return selectElement.all(by.tagName('option'));
//   })

//   .then((options)=> {
//     options[optionIndex].click();
//   })


//   .then(()=> {
//     return this.wait(5);
//   })
// }
// selectUpdatedOnDate (productIndex) {
//   var datePicker;

//   return this.browser.element.all(by.css('.w-product-analysis-metadata .w-update-on-datepicker'))

//   .then((datePickers)=> {
//     datePicker = datePickers[productIndex];
//     return this.scrollToElement(datePicker);
//   })

//   .then(()=> {
//     return datePicker.all(by.css('table tbody button')).get(1).click();
//   })

//   .then(()=> {
//     this.wait(10);
//   });
// }
// }

function ProductCatalogUser(opts) {
  opts = opts || {};
  User.call(this, opts);
}
ProductCatalogUser.prototype = new User();


ProductCatalogUser.prototype.gotToCatalog = function() {
  var self = this;
  return this.goTo('/catalog')
    .then(function() {
      var el = self.browser.element(by.css('.panel'));
      return self.waitForVisibleElement($('.panel'));
    })
    .then(function() {
      return self.takeScreenshot('just-went-to-catalog');
    });
};
ProductCatalogUser.prototype.openProductPanelByProductName = function(productName) {
  var el;
  var self = this;
  this.currentProductName = productName;
  el = this.browser.element(by.xpath(helper.getPanelTitleSelectorByProductName(this.currentProductName)));
  return self.waitForClickableElement(el)
    .then(function() {
      Logger.info('Clicking on product panel to open it...');
      return el.click();
    })
    .then(function() {
      Logger.info('Waiting for 5 seconds...');
      // return self.waitForVisibleElement($('.input-brand-name'),'Waiting for brand name after opened product panel');
      return self.browser.sleep(5 * 1000);
    });
};

ProductCatalogUser.prototype.appendToImageUrl = function(str) {
  Logger.info('Appending to image URL input...')
  return this.browser.element(by.xpath(helper.getInputImageUrlSelectorByProductName(this.currentProductName))).sendKeys(str);
};
ProductCatalogUser.prototype.readImageUrl = function() {
  Logger.info('Reading image URL...');
  return this.browser.element(by.xpath(helper.getInputImageUrlSelectorByProductName(this.currentProductName))).getAttribute('value');
};
// countries
ProductCatalogUser.prototype.readCountries = function() {
  return this.browser.element.all(by.xpath(helper.getCountriesSelector(this.currentProductName))).map(function(el) {
    return el.getText();
  });
};

ProductCatalogUser.prototype.clickToAddCountry = function() {
  return this.browser.element(by.xpath(helper.getCountryAddButton(this.currentProductName))).click();
};

ProductCatalogUser.prototype.selectTypeOfAnalysis = function(productIndex, optionIndex) {

  var selectElement;
  var self = this;

  return self.browser.element.all(by.css('.w-product-analysis-metadata .w-type-of-analysis-select'))

  .then(function(selectElements) {
    selectElement = selectElements[productIndex];
    return self.scrollToElement(selectElement);
  })

  .then(function() {
    return selectElement.all(by.tagName('option'));
  })

  .then(function(options) {
    options[optionIndex].click();
  })


  .then(function() {
    return self.wait(5);
  });
};

ProductCatalogUser.prototype.selectUpdatedOnDate = function(productIndex) {
  var datePicker;
  var self = this;

  return self.browser.element.all(by.css('.w-product-analysis-metadata .w-update-on-datepicker'))

  .then(function(datePickers) {
    datePicker = datePickers[productIndex];
    return self.scrollToElement(datePicker);
  })

  .then(function() {
    return datePicker.all(by.css('table tbody button')).get(1).click();
  })

  .then(function() {
    self.wait(10);
  });
};

ProductCatalogUser.prototype.readTypeOfAnalysis = function(productIndex) {
  var self = this;
  return self.browser.element.all(by.css('.w-product-analysis-metadata .w-type-of-analysis-select'))

  .then(function(selectElements) {
    return selectElements[productIndex].$('option:checked').getText();
  });

};


ProductCatalogUser.prototype.readUpdatedOnDate = function(productIndex) {
  var self = this;
  return self.browser.element.all(by.css('.w-product-analysis-metadata .w-datepicker-value-label em'))

  .then(function(labels) {
    return labels[productIndex].getText();
  });
};

ProductCatalogUser.prototype.showPageContainingProductByProductName = function(productName) {
  var self = this;

  return self.browser.element(by.xpath(helper.getPanelTitleSelectorByProductName(productName)))
    .isPresent()
    .then(function(isDisplayed) {
      var el;
      if (!isDisplayed) {
        el = self.browser.element(by.xpath('//button/span[contains(text(),"More products")]'));
        return self.waitForVisibleElement(el)
          .then(function() {
            return self.scrollToElement(el);
          })
          .then(function() {
            return el.click();
          })
          .then(function() {
            return self.showPageContainingProductByProductName(productName);
          });
      }

      return self.wait(2);
    });
  //return self.browser.element(by.css(''))

}

ProductCatalogUser.prototype.readProductNameInputValue = function() {
  Logger.info('Reading product name input value...');
  var el;
  el = element(by.xpath(helper.getInputProductNameSelectorByProductName(this.currentProductName)));
  return this.scrollTo(el)
    .then(function() {
      return el.getAttribute('value');
    });
};
ProductCatalogUser.prototype.appendToBrandName = function(str) {
  Logger.info('Appending to brand name input, the value', str);
  var el;
  var self = this;
  el = element(by.xpath(helper.getInputBrandNameSelectorByProductName(this.currentProductName)));
  return this.scrollTo(el)
    .then(function() {
      return self.takeScreenshot('appending to brand name');
    })
    .then(function() {
      return self.waitForVisibleElement(el, 'Waiting for input brand name being visible...');
    })
    .then(function() {
      return el.sendKeys(str);
    });
};
ProductCatalogUser.prototype.readBrandNameInputValue = function() {
  Logger.info('Reading brand name input value...');
  return this.browser.element(by.xpath(helper.getInputBrandNameSelectorByProductName(this.currentProductName))).getAttribute('value');
};



module.exports = ProductCatalogUser;