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

describe('Disponibilita e2e test', () => {
  const disponibilitaPageUrl = '/disponibilita';
  const disponibilitaPageUrlPattern = new RegExp('/disponibilita(\\?.*)?$');
  const username = Cypress.env('E2E_USERNAME') ?? 'user';
  const password = Cypress.env('E2E_PASSWORD') ?? 'user';
  const disponibilitaSample = {};

  let disponibilita;

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

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

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

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

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

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

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

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

        cy.visit(disponibilitaPageUrl);

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

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

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

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

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

        disponibilita = undefined;
      });
    });
  });

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

    it('should create an instance of Disponibilita', () => {
      cy.get(`[data-cy="giornoSettimana"]`).select('MARTEDI');

      cy.get(`[data-cy="dataSpecifica"]`).type('2025-12-10');
      cy.get(`[data-cy="dataSpecifica"]`).blur();
      cy.get(`[data-cy="dataSpecifica"]`).should('have.value', '2025-12-10');

      cy.get(`[data-cy="oraInizio"]`).type('2025-12-10T14:03');
      cy.get(`[data-cy="oraInizio"]`).blur();
      cy.get(`[data-cy="oraInizio"]`).should('have.value', '2025-12-10T14:03');

      cy.get(`[data-cy="oraFine"]`).type('2025-12-10T06:53');
      cy.get(`[data-cy="oraFine"]`).blur();
      cy.get(`[data-cy="oraFine"]`).should('have.value', '2025-12-10T06:53');

      cy.get(`[data-cy="orarioInizio"]`).type('wide-eyed in');
      cy.get(`[data-cy="orarioInizio"]`).should('have.value', 'wide-eyed in');

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

      cy.get(`[data-cy="tipo"]`).select('DISPONIBILE');

      cy.get(`[data-cy="note"]`).type('doodle vaguely vacantly');
      cy.get(`[data-cy="note"]`).should('have.value', 'doodle vaguely vacantly');

      cy.get(entityCreateSaveButtonSelector).click();

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