/*
 * Copyright (C) 2020 Robert Bosch GmbH Copyright (C) 2018 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

describe('Charts', () => {
  before(() => {
    cy.visit('/charts-demo/gauge');
  });

  describe('Site', () => {
    it('should contain title', () => {
      cy.get('h2', { timeout: 10000 }).should('contain', 'Charts');
    });
  });

  describe('Full featured gauge', () => {
    before(() => {
      changeValue(87.2343);
    });

    it('should be visible', () => {
      fullfeaturedGauge().scrollIntoView().should('be.visible');
    });

    it('should display the current value with one decimal point', () => {
      fullfeaturedGauge().contains('[data-test="current-value-digits"]', '87.2').should('be.visible');
    });

    it('should display the unit', () => {
      fullfeaturedGauge().contains('[data-test="current-value-unit"]', '°C').should('be.visible');
    });

    it('should display min and max values', () => {
      fullfeaturedGauge().contains('[data-test="min-value-label"]', '-10').should('be.visible');
      fullfeaturedGauge().contains('[data-test="max-value-label"]', '120').should('be.visible');
    });

    it('should display value name', () => {
      fullfeaturedGauge().contains('[data-test="value-name-label"]', 'Actual Value').should('be.visible');
    });

    function fullfeaturedGauge() {
      return cy.get('[data-test="full-featured-gauge"]');
    }
  });

  describe('Full featured inverted gauge', () => {
    before(() => {
      changeValue(52.3232);
    });

    it('should be visible', () => {
      fullfeaturedInvertedGauge().scrollIntoView().should('be.visible');
    });

    it('should display the current value with one decimal point', () => {
      fullfeaturedInvertedGauge().contains('[data-test="current-value-digits"]', '52.3').should('be.visible');
    });

    it('should display the unit', () => {
      fullfeaturedInvertedGauge().contains('[data-test="current-value-unit"]', '°C').should('be.visible');
    });

    it('should display min and max values', () => {
      fullfeaturedInvertedGauge().contains('[data-test="min-value-label"]', '-10').should('be.visible');
      fullfeaturedInvertedGauge().contains('[data-test="max-value-label"]', '120').should('be.visible');
    });

    it('should display value name', () => {
      fullfeaturedInvertedGauge().contains('[data-test="value-name-label"]', 'Actual Value').should('be.visible');
    });

    function fullfeaturedInvertedGauge() {
      return cy.get('[data-test="full-featured-inverted-gauge"]');
    }
  });

  describe('Simple gauge', () => {
    before(() => {
      changeValue(10.898);
    });

    it('should be visible', () => {
      simpleGauge().scrollIntoView().should('be.visible');
    });

    it('should display the current value without decimal points', () => {
      simpleGauge().contains('[data-test="current-value-digits"]', '11').should('be.visible');
    });

    it('should hide the unit', () => {
      simpleGauge().children('[data-test="current-value-unit"]').should('not.exist');
    });

    it('should hide min and max values', () => {
      simpleGauge().children('[data-test="min-value-label"]').should('not.exist');
      simpleGauge().children('[data-test="max-value-label"]').should('not.exist');
    });

    it('should hide value name', () => {
      simpleGauge().children('[data-test="value-name-label"]').should('not.exist');
    });

    function simpleGauge() {
      return cy.get('[data-test="simple-gauge"]');
    }
  });

  function changeValue(newValue: number) {
    cy.get('[data-test="valueInput"').scrollIntoView();
    cy.get('[data-test="valueInput"').click();
    cy.get('[data-test="valueInput"').clear();
    cy.get('[data-test="valueInput"').type(`${newValue}`);
    cy.get('[data-test="valueInput"').blur();
  }
});
