import { PageElement, IPageElement, TextCompareStrategy } from './page-element';
import { PageElementAny } from './page-element-any';
import { ButtonType, PageElementButton } from './page-element-button';
import { FormFieldMatSelect } from '../form-field/form-field-mat-select';


export class PageElementPaginator extends PageElement {

  constructor(ipe: IPageElement) {
    super(ipe);
  }

  get paginatorPageLabel() {
    return new PageElementAny({
      id: this.id + ' mat-form-field:first-child mat-label',
      text: Cypress.env('PAGINATOR_PAGE_TEXT')
    });
  }

  get paginatorPageSelect() {
    return new FormFieldMatSelect({
      id: this.id + ' mat-form-field:first-child mat-select'
    });
  }

  get paginatorElementsPerPageLabel() {
    return new PageElementAny({
      id: this.id + ' mat-form-field:nth-child(2) mat-label',
      text: Cypress.env('PAGINATOR_ITEMS_PER_PAGE_TEXT')
    });
  }

  get paginatorElementsPerPageSelect() {
    return new FormFieldMatSelect({
      id: this.id + ' mat-form-field:nth-child(2) mat-select',
      options: ['5', '10', '15', '20', '50']
    });
  }

  get positionIndicator() {
    return new PageElementAny({
      id: this.id + ' mat-card > div > span',
      text: /\d+ a \d+ de \d+/, textCompareStrategy: TextCompareStrategy.REGEX
    });
  }

  get firstPageButton() {
    return new PageElementButton({
      id: this.id + ' mat-card > div > span > div > button:first-child',
      iconName: 'fa-step-backward'
    }, ButtonType.MATICON_FA);
  }

  get previousPageButton() {
    return new PageElementButton({
      id: this.id + ' mat-card > div > span > div > button:nth-child(2)',
      iconName: 'fa-chevron-left'
    }, ButtonType.MATICON_FA);
  }

  get nextPageButton() {
    return new PageElementButton({
      id: this.id + ' mat-card > div > span > div > button:nth-child(4)',
      iconName: 'fa-chevron-right'
    }, ButtonType.MATICON_FA);
  }
  get lastPageButton() {
    return new PageElementButton({
      id: this.id + ' mat-card > div > span > div > button:last-child',
      iconName: 'fa-step-forward'
    }, ButtonType.MATICON_FA);
  }

  goToFirstPage() {
    if (!this.isFirstPage) {
      this.firstPageButton.click();
      this.currentPage = 1;
    }
  }

  goToPreviousPage() {
    if (!this.isFirstPage) {
      this.previousPageButton.click();
      this.currentPage--;
    }
  }

  get isFirstPage() {
    return 1 == this.currentPage;
  }

  goToNextPage() {
    if (!this.isLastPage) {
      this.nextPageButton.click();
      this.currentPage++;
    }
  }

  get isLastPage() {
    return this.totalPages == this.currentPage;
  }

  goToLastPage() {
    if (!this.isLastPage) {
      this.lastPageButton.click();
      this.currentPage = this.totalPages!;
    }
  }

  totalItems?: number;
  totalPages?: number;
  currentPage: number = 1;
  private _itemsPerPage: number = 10;

  get itemsPerPage() {
    return this._itemsPerPage || Cypress.env('PAGINATOR_INITIAL_ITEMS_PER_PAGE');
  }

  set itemsPerPage(v: number) {
    this.paginatorElementsPerPageSelect.selectByValue(v.toString());
    this._itemsPerPage = v;
    this.currentPage = 1;
    this.totalPages = Math.ceil((this.totalItems || 0) / this._itemsPerPage);
  }

  override validateDefaultFieldValues(): void {
    this.firstPageButton.validateDefaultFieldValues();
    this.previousPageButton.validateDefaultFieldValues();
    this.nextPageButton.validateDefaultFieldValues();
    this.lastPageButton.validateDefaultFieldValues();
    this.paginatorElementsPerPageSelect.validateDefaultFieldValues();
    this.paginatorPageSelect.validateDefaultFieldValues();
    this.paginatorElementsPerPageLabel.validateDefaultFieldValues();
    // this.validateInFirstPage();
  }

  validatePositionText(start: number | string, end: number | string, total: number | string) {
    this.positionIndicator.getText().should('contain', `${start} ${Cypress.env('PAGINATOR_POSTIION_TO_TEXT')} ${end} ${Cypress.env('PAGINATOR_POSTIION_OF_TEXT')} ${total}`);
  }

  validateNavigateButtons() {
    this.positionIndicator.getText().then((posText: string) => {
      let text = posText.replace(Cypress.env('PAGINATOR_POSTIION_TO_TEXT'), '').replace(Cypress.env('PAGINATOR_POSTIION_OF_TEXT'), '');
      let numbers = text.split(' ').filter(f => f).map(m => +m);

      if (numbers[0] == 1 && numbers[1] >= numbers[2]) {
        this.validatePaginatorWithSinglePage();
      } else if (numbers[0] == 1) {
        this.validateInFirstPage();
      } else {
        this.paginatorPageSelect.getText().then((pageNumber: string) => {
          if (+pageNumber > 1 && numbers[1] < numbers[2]) {
            this.validateInMidPage();
          } else {
            this.validateInLastPage();
          }
        })
      }
    });
  }

  private validateInFirstPage() {
    this.firstPageButton.pageElement.should('be.disabled');
    this.previousPageButton.pageElement.should('be.disabled');
    this.lastPageButton.pageElement.should('be.enabled');
    this.nextPageButton.pageElement.should('be.enabled');
  }

  private validateInLastPage() {
    this.firstPageButton.pageElement.should('be.enabled');
    this.previousPageButton.pageElement.should('be.enabled');
    this.lastPageButton.pageElement.should('be.disabled');
    this.nextPageButton.pageElement.should('be.disabled');
  }

  private validateInMidPage() {
    this.firstPageButton.pageElement.should('be.enabled');
    this.previousPageButton.pageElement.should('be.enabled');
    this.lastPageButton.pageElement.should('be.enabled');
    this.nextPageButton.pageElement.should('be.enabled');
  }

  private validatePaginatorWithSinglePage() {
    this.firstPageButton.pageElement.should('be.disabled');
    this.previousPageButton.pageElement.should('be.disabled');
    this.lastPageButton.pageElement.should('be.disabled');
    this.nextPageButton.pageElement.should('be.disabled');
  }

}
