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

describe('UtenteApp e2e test', () => {
  const utenteAppPageUrl = '/utente-app';
  const utenteAppPageUrlPattern = new RegExp('/utente-app(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const utenteAppSample = { username: 'bell for windy', email: 'Oronzo88@libero.it', ruolo: 'ADMIN', attivo: false };

  let utenteApp;

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

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

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

  it('UtenteApps menu should load UtenteApps page', () => {
    cy.visit('/');
    cy.clickOnEntityMenuItem('utente-app');
    cy.wait('@entitiesRequest').then(({ response }) => {
      if (response?.body.length === 0) {
        cy.get(entityTableSelector).should('not.exist');
      } else {
        cy.get(entityTableSelector).should('exist');
      }
    });
    cy.getEntityHeading('UtenteApp').should('exist');
    cy.url().should('match', utenteAppPageUrlPattern);
  });

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

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

    describe('with existing value', () => {
      beforeEach(() => {
        cy.authenticatedRequest({
          method: 'POST',
          url: '/api/utente-apps',
          body: utenteAppSample,
        }).then(({ body }) => {
          utenteApp = body;

          cy.intercept(
            {
              method: 'GET',
              url: '/api/utente-apps+(?*|)',
              times: 1,
            },
            {
              statusCode: 200,
              body: [utenteApp],
            },
          ).as('entitiesRequestInternal');
        });

        cy.visit(utenteAppPageUrl);

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

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

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

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

      it('last delete button click should delete instance of UtenteApp', () => {
        cy.get(entityDeleteButtonSelector).last().click();
        cy.getEntityDeleteDialogHeading('utenteApp').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', utenteAppPageUrlPattern);

        utenteApp = undefined;
      });
    });
  });

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

    it('should create an instance of UtenteApp', () => {
      cy.get(`[data-cy="username"]`).type('since woeful');
      cy.get(`[data-cy="username"]`).should('have.value', 'since woeful');

      cy.get(`[data-cy="email"]`).type('Richelmo81@hotmail.com');
      cy.get(`[data-cy="email"]`).should('have.value', 'Richelmo81@hotmail.com');

      cy.get(`[data-cy="telefono"]`).type('bathrobe westernise');
      cy.get(`[data-cy="telefono"]`).should('have.value', 'bathrobe westernise');

      cy.get(`[data-cy="ruolo"]`).select('INCARICATO');

      cy.get(`[data-cy="attivo"]`).should('not.be.checked');
      cy.get(`[data-cy="attivo"]`).click();
      cy.get(`[data-cy="attivo"]`).should('be.checked');

      cy.get(`[data-cy="nome"]`).type('apud hydrant thankfully');
      cy.get(`[data-cy="nome"]`).should('have.value', 'apud hydrant thankfully');

      cy.get(`[data-cy="cognome"]`).type('uproot who spirited');
      cy.get(`[data-cy="cognome"]`).should('have.value', 'uproot who spirited');

      cy.get(`[data-cy="luogoNascita"]`).type('a gadzooks until');
      cy.get(`[data-cy="luogoNascita"]`).should('have.value', 'a gadzooks until');

      cy.get(`[data-cy="dataNascita"]`).type('gad past');
      cy.get(`[data-cy="dataNascita"]`).should('have.value', 'gad past');

      cy.get(`[data-cy="residente"]`).type('object');
      cy.get(`[data-cy="residente"]`).should('have.value', 'object');

      cy.get(`[data-cy="societa"]`).type('polyester forage');
      cy.get(`[data-cy="societa"]`).should('have.value', 'polyester forage');

      cy.get(`[data-cy="sede"]`).type('once');
      cy.get(`[data-cy="sede"]`).should('have.value', 'once');

      cy.get(`[data-cy="codfiscale"]`).type('ha');
      cy.get(`[data-cy="codfiscale"]`).should('have.value', 'ha');

      cy.get(`[data-cy="telefonoSoc"]`).type('serenade rationale meh');
      cy.get(`[data-cy="telefonoSoc"]`).should('have.value', 'serenade rationale meh');

      cy.get(`[data-cy="emailSoc"]`).type('oh hungrily wonderfully');
      cy.get(`[data-cy="emailSoc"]`).should('have.value', 'oh hungrily wonderfully');

      cy.get(entityCreateSaveButtonSelector).click();

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