import {
  entityConfirmDeleteButtonSelector,
  entityCreateButtonSelector,
  entityCreateCancelButtonSelector,
  entityCreateSaveButtonSelector,
  entityDeleteButtonSelector,
  entityDetailsBackButtonSelector,
  entityDetailsButtonSelector,
  entityEditButtonSelector,
  entityTableSelector,
} from '../../support/entity';

describe('Conferma e2e test', () => {
  const confermaPageUrl = '/conferma';
  const confermaPageUrlPattern = new RegExp('/conferma(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const confermaSample = {};

  let conferma;

  beforeEach(() => {
    cy.login(username, password);
  });

  beforeEach(() => {
    cy.intercept('GET', '/api/confermas+(?*|)').as('entitiesRequest');
    cy.intercept('POST', '/api/confermas').as('postEntityRequest');
    cy.intercept('DELETE', '/api/confermas/*').as('deleteEntityRequest');
  });

  afterEach(() => {
    if (conferma) {
      cy.authenticatedRequest({
        method: 'DELETE',
        url: `/api/confermas/${conferma.id}`,
      }).then(() => {
        conferma = undefined;
      });
    }
  });

  it('Confermas menu should load Confermas page', () => {
    cy.visit('/');
    cy.clickOnEntityMenuItem('conferma');
    cy.wait('@entitiesRequest').then(({ response }) => {
      if (response?.body.length === 0) {
        cy.get(entityTableSelector).should('not.exist');
      } else {
        cy.get(entityTableSelector).should('exist');
      }
    });
    cy.getEntityHeading('Conferma').should('exist');
    cy.url().should('match', confermaPageUrlPattern);
  });

  describe('Conferma page', () => {
    describe('create button click', () => {
      beforeEach(() => {
        cy.visit(confermaPageUrl);
        cy.wait('@entitiesRequest');
      });

      it('should load create Conferma page', () => {
        cy.get(entityCreateButtonSelector).click();
        cy.url().should('match', new RegExp('/conferma/new$'));
        cy.getEntityCreateUpdateHeading('Conferma');
        cy.get(entityCreateSaveButtonSelector).should('exist');
        cy.get(entityCreateCancelButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', confermaPageUrlPattern);
      });
    });

    describe('with existing value', () => {
      beforeEach(() => {
        cy.authenticatedRequest({
          method: 'POST',
          url: '/api/confermas',
          body: confermaSample,
        }).then(({ body }) => {
          conferma = body;

          cy.intercept(
            {
              method: 'GET',
              url: '/api/confermas+(?*|)',
              times: 1,
            },
            {
              statusCode: 200,
              headers: {
                link: '<http://localhost/api/confermas?page=0&size=20>; rel="last",<http://localhost/api/confermas?page=0&size=20>; rel="first"',
              },
              body: [conferma],
            },
          ).as('entitiesRequestInternal');
        });

        cy.visit(confermaPageUrl);

        cy.wait('@entitiesRequestInternal');
      });

      it('detail button click should load details Conferma page', () => {
        cy.get(entityDetailsButtonSelector).first().click();
        cy.getEntityDetailsHeading('conferma');
        cy.get(entityDetailsBackButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', confermaPageUrlPattern);
      });

      it('edit button click should load edit Conferma page and go back', () => {
        cy.get(entityEditButtonSelector).first().click();
        cy.getEntityCreateUpdateHeading('Conferma');
        cy.get(entityCreateSaveButtonSelector).should('exist');
        cy.get(entityCreateCancelButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', confermaPageUrlPattern);
      });

      it('edit button click should load edit Conferma page and save', () => {
        cy.get(entityEditButtonSelector).first().click();
        cy.getEntityCreateUpdateHeading('Conferma');
        cy.get(entityCreateSaveButtonSelector).click();
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', confermaPageUrlPattern);
      });

      it('last delete button click should delete instance of Conferma', () => {
        cy.get(entityDeleteButtonSelector).last().click();
        cy.getEntityDeleteDialogHeading('conferma').should('exist');
        cy.get(entityConfirmDeleteButtonSelector).click();
        cy.wait('@deleteEntityRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(204);
        });
        cy.wait('@entitiesRequest').then(({ response }) => {
          expect(response?.statusCode).to.equal(200);
        });
        cy.url().should('match', confermaPageUrlPattern);

        conferma = undefined;
      });
    });
  });

  describe('new Conferma page', () => {
    beforeEach(() => {
      cy.visit(confermaPageUrl);
      cy.get(entityCreateButtonSelector).click();
      cy.getEntityCreateUpdateHeading('Conferma');
    });

    it('should create an instance of Conferma', () => {
      cy.get(`[data-cy="motivoConferma"]`).type('decent');
      cy.get(`[data-cy="motivoConferma"]`).should('have.value', 'decent');

      cy.get(`[data-cy="tipoConferma"]`).select('RIFIUTATA');

      cy.get(entityCreateSaveButtonSelector).click();

      cy.wait('@postEntityRequest').then(({ response }) => {
        expect(response?.statusCode).to.equal(201);
        conferma = response.body;
      });
      cy.wait('@entitiesRequest').then(({ response }) => {
        expect(response?.statusCode).to.equal(200);
      });
      cy.url().should('match', confermaPageUrlPattern);
    });
  });
});
