import { browser, by, element } from 'protractor';
import * as moment from 'moment-timezone';

export class NavBarPo {
  async navigateTo(): Promise<unknown> {
    return browser.get(browser.baseUrl);
  }

  async showUserInsert(): Promise<unknown> {
    return element(by.id('addUser')).click();
  }

  async popupShown(): Promise<boolean> {
    return element(by.className('p-dialog-resizable')).isPresent();
  }

  async sendUserForm(): Promise<unknown> {
    const today = moment().format('YYYYMMDDHHMMSS').toString();
    element(by.id('username')).sendKeys(today);
    element(by.id('password')).sendKeys('testuser');
    element(by.id('userRealName')).sendKeys('testuser');
    element(by.cssContainingText('option', 'testing units')).click();
    element(by.cssContainingText('option', 'General user')).click();
    return element(by.buttonText('Save')).click();
  }

  async success(): Promise<boolean> {
    return element(by.cssContainingText('.p-toast-summary', 'Success')).isPresent();
  }

  async showUnitInsert(): Promise<unknown> {
    return element(by.id('addUnit')).click();
  }

  async sendUnitForm(today: string) {
    element(by.id('unitId')).sendKeys(today);
    element(by.id('unitDescription')).sendKeys(today);
    element(by.id('lat')).sendKeys(10);
    element(by.id('lon')).sendKeys(10);
    element(by.buttonText('Add sensor')).click();
    await browser.sleep(3000);
    element(by.id('sensorId')).sendKeys(today);
    element(by.id('sensorName')).sendKeys(today)
    element(by.cssContainingText('option', 'test')).click();
    element(by.cssContainingText('option', 'Strikes (  )')).click();
    await browser.sleep(3000);
    return element(by.buttonText('Save')).click();
  }

  async unitInList(today: string) {
    return element(by.cssContainingText('#p-accordiontab-0 .unitName', today));
  }
}
