import {by, element, protractor} from 'protractor';

export class DashboardPo {

  async clickToAccordionTab(): Promise<unknown> {
    return element(by.id('p-accordiontab-0')).click();
  }

  async clickButtonByText(text: string): Promise<unknown> {
    return element(by.buttonText(text)).click();
  }

  async xpathIsVisible(xPath: string): Promise<boolean> {
    return element(by.xpath(xPath)).isDisplayed();
  }

  async buttonByTextIsVisible(buttonText: string): Promise<boolean> {
    return element(by.buttonText(buttonText)).isDisplayed();
  }

  async clickManipulationUnitButton(id: string): Promise<unknown> {
    return element(by.id('manipulation_'+id)).click();
  }

  async clickByXpath(xPath: string): Promise<unknown> {
    return element(by.xpath(xPath)).click();
  }

  async setUnitDescription(xPath: string, text: string): Promise<unknown> {
    element(by.xpath(xPath)).clear()
    return element(by.xpath(xPath)).sendKeys(text);
  }

  async fillPosition(): Promise<unknown> {
    element(by.id('lat')).click()
    element(by.id('lat')).sendKeys(protractor.Key.UP, '10');
    element(by.id('lat')).sendKeys(protractor.Key.UP);
    element(by.id('lat')).sendKeys(protractor.Key.UP);
    element(by.id('lon')).click()
    element(by.id('lon')).sendKeys(protractor.Key.UP);
    element(by.id('lon')).sendKeys(protractor.Key.UP);
    element(by.id('lon')).sendKeys(protractor.Key.UP);
    element(by.id('alt')).sendKeys('10');
    element(by.id('speed')).sendKeys('10');
    return element(by.id('dop')).sendKeys('10');
  }

  async numberOfxPaths(xPath: string): Promise<unknown> {
    return element.all(by.xpath(xPath)).count();
  }

  async fillSensorForm(text: string): Promise<unknown> {
    element(by.id('sensorId')).clear();
    element(by.id('sensorId')).sendKeys(text);
    element(by.id('sensorName')).clear()
    element(by.id('sensorName')).sendKeys(text)
    element(by.cssContainingText('option', 'test')).click();
    return element(by.cssContainingText('option', 'Strikes (  )')).click();
  }

  async fillEditSensorForm(text: string): Promise<unknown> {
    element(by.id('sensorName')).clear()
    return element(by.id('sensorName')).sendKeys(text)
  }

  async clickToAccordionTabByXpath(xPath: string): Promise<unknown> {
    return element(by.xpath(xPath)).click();
  }
}
