import {DashboardPo} from './dashboard.po';
import {LoginPo} from '../login/login.po';
import {browser, by, element} from 'protractor';
import * as moment from 'moment-timezone';
import {NavBarPo} from '../nav-bar/nav-bar.po';


describe('Dashboard Module', () => {
  let dashboardPo: DashboardPo;
  let loginPo: LoginPo
  let navBarPo: NavBarPo
  let today = moment().format('YYYYMMDDHHMMSS').toString();

  beforeEach(async () => {
    dashboardPo = new DashboardPo();
    loginPo = new LoginPo();
    navBarPo = new NavBarPo();
    await loginPo.navigateTo();
    await loginPo.processForm();
  });

  afterEach(async () => {
    await loginPo.logout();
  });

  it('Show test unit sensors!', async () => {
    expect(await dashboardPo.xpathIsVisible('//h5[contains(text(), \'test sensor Temp\')]')).toEqual(false);
    await dashboardPo.clickToAccordionTab();
    await browser.sleep(500);
    expect(await dashboardPo.xpathIsVisible('//h5[contains(text(), \'test sensor Temp\')]')).toEqual(true);
  });

  it('Add unit ' + today + ' !', async () => {
    await navBarPo.showUnitInsert();
    expect(await navBarPo.popupShown()).toEqual(true);
    await navBarPo.sendUnitForm(today);
    expect(await navBarPo.success()).toEqual(true);
  });

  it('Added unit ' + today + ' shown!', async () => {
    expect(await dashboardPo.xpathIsVisible('//h4[contains(text(), \''+ today +'\')]')).toEqual(true);
  });

  it('Unit ' + today + ' manipulation buttons test!', async () => {
    expect(await dashboardPo.buttonByTextIsVisible('Sensors graph')).toEqual(true);
    await dashboardPo.clickManipulationUnitButton(today);
    expect(await dashboardPo.xpathIsVisible('//span[contains(text(), \'Edit unit\')]')).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//span[contains(text(), \'Insert position\')]')).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//span[contains(text(), \'Delete unit\')]')).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//span[contains(text(), \'Add sensor\')]')).toEqual(true);
  });

  it('Edit unit!', async () => {
    await dashboardPo.clickManipulationUnitButton(today);
    await dashboardPo.clickByXpath('//span[contains(text(), \'Edit unit\')]');
    expect(await dashboardPo.xpathIsVisible('//div[contains(@class, \'p-dialog\')]')).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//span[contains(text(), \'Edit unit\')]')).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//span[contains(@class, \'p-dialog-title\')]')).toEqual(true);
    today = today+'edit';
    await dashboardPo.setUnitDescription('//input', today);
    await dashboardPo.clickByXpath('//span[contains(text(), \'Save\')]');
    expect(await navBarPo.success()).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//h4[contains(text(), \''+ today +'\')]')).toEqual(true);
  });

  it('Insert position!', async () => {
    await dashboardPo.clickManipulationUnitButton(today.substr(0, today.length-4));
    await dashboardPo.clickByXpath('//span[contains(text(), \'Insert position\')]');
    await dashboardPo.fillPosition();
    await dashboardPo.clickByXpath('//span[contains(text(), \'Save\')]');
    expect(await navBarPo.success()).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//h4[contains(text(), \''+ today +'\')]')).toEqual(true);
  });

  it('Add sensor!', async () => {
    const sensor = today.substr(0, today.length-4)
    await dashboardPo.clickManipulationUnitButton(today.substr(0, today.length-4));
    await dashboardPo.clickByXpath('//span[contains(text(), \'Add sensor\')]');
    await browser.sleep(1000);
    await dashboardPo.clickButtonByText('Add Sensor');
    await browser.sleep(1000);
    await dashboardPo.clickButtonByText('Add Sensor');
    expect(await dashboardPo.numberOfxPaths('//input[contains(@id, \'sensorId\')]')).toEqual(3);
    await dashboardPo.clickButtonByText('Remove last sensor');
    await dashboardPo.clickButtonByText('Remove last sensor');
    await dashboardPo.fillSensorForm(sensor);
    await dashboardPo.clickByXpath('//span[contains(text(), \'Save\')]');
    await browser.sleep(1000);
    expect(await navBarPo.success()).toEqual(true);
    await browser.sleep(5000);
    expect(await dashboardPo.xpathIsVisible('//h4[contains(text(), \''+ sensor +'\')]')).toEqual(true);
    await dashboardPo.clickToAccordionTabByXpath('(.//a[contains(@class, \'p-accordion-header-link\')])[last()]');
    await browser.sleep(500);
    expect(await dashboardPo.xpathIsVisible('//h5[contains(text(), \''+ sensor +'\')]')).toEqual(true);
  });

  it('Edit sensor!', async () => {
    const sensor = today.substr(0, today.length-4)
    await dashboardPo.clickToAccordionTabByXpath('(.//a[contains(@class, \'p-accordion-header-link\')])[last()]');
    await browser.sleep(500);
    await dashboardPo.clickManipulationUnitButton('sensor_'+sensor);
    await browser.sleep(500);
    await dashboardPo.clickByXpath('//span[contains(text(), \'Edit sensor\')]');
    await dashboardPo.fillEditSensorForm(sensor+'_test');
    await dashboardPo.clickByXpath('//span[contains(text(), \'Save\')]');
    await browser.sleep(1000);
    expect(await navBarPo.success()).toEqual(true);
    expect(await dashboardPo.xpathIsVisible('//h5[contains(text(), \''+ sensor +'_test\')]')).toEqual(true);
  });

  it('Delete sensor!', async () => {
    const sensor = today.substr(0, today.length-4)
    await dashboardPo.clickToAccordionTabByXpath('(.//a[contains(@class, \'p-accordion-header-link\')])[last()]');
    await browser.sleep(500);
    await dashboardPo.clickManipulationUnitButton('sensor_'+sensor);
    await browser.sleep(500);
    await dashboardPo.clickByXpath('//span[contains(text(), \'Delete sensor\')]');
    await dashboardPo.clickByXpath('//span[contains(text(), \'Yes\')]');
    await browser.sleep(1000);
    expect(await navBarPo.success()).toEqual(true);
    expect(await element(by.xpath('//h5[contains(text(), \''+ sensor +'\')]')).isPresent()).toEqual(false);
  });

  it('Delete unit!', async () => {
    await dashboardPo.clickManipulationUnitButton(today.substr(0, today.length-4));
    await browser.sleep(500);
    await dashboardPo.clickByXpath('//span[contains(text(), \'Delete unit\')]');
    await dashboardPo.clickByXpath('//span[contains(text(), \'Yes\')]');
    await browser.sleep(1000);
    expect(await navBarPo.success()).toEqual(true);
    expect(await element(by.xpath('//h4[contains(text(), \''+ today +'\')]')).isPresent()).toEqual(false);
  });
});
