import { CommerceApiIntercepts } from '@commerce-shared-lib/index';

const reportsBookingsPageSelectors = {
  downloadModal: '[qaid="download-vat-summary-modal-button"]',
  downloadText: '[qaid="dowmnload-vat-summary-text"]',
  legalEntityInput: '[qaid="legal-entity-input-select"]',
  legalEntityOption: '[qaid="legal-entity-option"]',
  monthInput: '[qaid="month-input-select"]',
  monthOption: '[qaid="month-option"]',
  yearInput: '[qaid="year-input-select"]',
  yearOption: '[qaid="year-option"]',
  downloadButton: '[qaid="download-button"]'
};
const dayjs = require('dayjs');

export class ReportsBookings {
  static visitReportsBookingsPage() {
    CommerceApiIntercepts.interceptCommerceApis();
    cy.allure().logStep('Visit Reports > Bookings page');
    cy.visit('app/admin2/reports/overview');
    cy.url().should('include', '/overview');
    cy.wait('@waitForUserInfo');
  }

  static openDownloadVatSummaryModal() {
    cy.allure().logStep(`Open download VAT Summary modal :`);
    cy.get(reportsBookingsPageSelectors.downloadModal).click();
    cy.get(reportsBookingsPageSelectors.downloadText).should('be.visible');
  }

  static selectLegalEntity() {
    cy.allure().logStep(`Select legal entity :`);
    cy.get(reportsBookingsPageSelectors.legalEntityInput).click();
    cy.get(reportsBookingsPageSelectors.legalEntityOption)
      .eq(0)
      .then((el) => {
        const todayDate = dayjs().format('YYYY-MM-DD').replace(/-/g, '_');
        cy.task('putDataInCache', {
          key: 'pdfName',
          data:
            todayDate +
            '_vat_summary_' +
            el.text().trim().replace(/ /g, '_').toLowerCase() +
            '.pdf'
        });
      });
    cy.get(reportsBookingsPageSelectors.legalEntityOption).eq(0).click();
  }

  static selectYear() {
    cy.allure().logStep(`Select year :`);
    cy.get(reportsBookingsPageSelectors.yearInput).click();
    cy.get(reportsBookingsPageSelectors.yearOption).eq(0).click();
  }

  static selectMonth() {
    cy.allure().logStep(`Select legal entity :`);
    cy.get(reportsBookingsPageSelectors.monthInput).click();
    cy.get(reportsBookingsPageSelectors.monthOption).eq(2).contains('March').click();
  }

  static downloadPDF() {
    cy.allure().logStep(`Download PDF :`);
    cy.intercept('GET', /api\/v1\/vat-aggregator\/vat-summary\/download/).as(
      'downloadVatSummary'
    );
    cy.get(reportsBookingsPageSelectors.downloadButton).should('not.be.disabled').click();
    cy.wait('@downloadVatSummary')
      .its('response')
      .then((res) => {
        expect(res?.body).to.be.an('ArrayBuffer');
      });
  }

  static verifyContent() {
    cy.task('getDataFromCache', 'pdfName').then((pdfName: any) => {
      cy.readFile(`cypress/downloads/${pdfName}`, 'utf8');
      cy.task('isExistPDF', pdfName).then((exist) => {
        if (exist) {
          cy.task('getPDFContent', pdfName).then((array) => {
            cy.wrap(array).each((el) => {
              expect(el.toString().toLowerCase()).not.to.contain('null');
            });
          });
        }
      });
    });
  }
}
