/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
import {Fragment} from 'react';
import {
  PaginationWrapper,
  ItemRange,
  PaginationContainer,
  PageButton
} from './style';

interface IPagination {
  currentPageNumber: number;
  numberOfItems: number;
  itemsPerPage: number;
  setCurrentPage: (page: number) => void;
  itemText?: string;
}

export const Pagination = (props: IPagination): JSX.Element => {
  const maxNumberOfButtonsToShow = 3;
  const LowerBoundLimit = 2;
  const higherBoundLimit = 1;
  const {currentPageNumber, numberOfItems, itemsPerPage, setCurrentPage, itemText} = props;
  const totalNumberOfPages = Math.ceil(numberOfItems / itemsPerPage);
  const setPage = (page: number): void => {
    setCurrentPage(page);
  };

  const minimumItemBoundPerPage = ((currentPageNumber - 1) * itemsPerPage) + 1;
  const maximumItemBoundPerPage = (currentPageNumber) * itemsPerPage;
  const generateButtons = (bounds = 1): JSX.Element[] => {
    const buttonToShow = maxNumberOfButtonsToShow > totalNumberOfPages ? totalNumberOfPages : maxNumberOfButtonsToShow;
    const buttons = [];
    let count: number;

    if (currentPageNumber <= bounds) {
      count = 1;
    } else if ((currentPageNumber + bounds) > totalNumberOfPages) {
      count = currentPageNumber - (buttonToShow - (totalNumberOfPages - currentPageNumber + 1));
    } else {
      count = currentPageNumber - bounds;
    }

    for (let y = 0; y < buttonToShow; y++) {
      buttons.push(
        <PageButton
          key={`page-button-${y}`}
          onClick={() => setPage(count + y)}
          active={currentPageNumber === count + y}>
          {count + y}
        </PageButton>
      );
    }

    return buttons;
  };

  return (
    <PaginationContainer className="pagination-container">
      {numberOfItems ? (
        <PaginationWrapper>
          {(currentPageNumber >= LowerBoundLimit) && (
            <Fragment>
              {currentPageNumber > 2 && totalNumberOfPages !== 3 && <PageButton onClick={() => setPage(1)}>1</PageButton>}
              {(currentPageNumber > LowerBoundLimit) && currentPageNumber !== 3 && <p>...</p>}
            </Fragment>
          )}
          {generateButtons()}
          {(currentPageNumber <= totalNumberOfPages - higherBoundLimit) && (
            <Fragment>
              {(currentPageNumber < totalNumberOfPages - higherBoundLimit) && currentPageNumber + 2 !== totalNumberOfPages && <p>...</p>}
              {currentPageNumber + 1 !== totalNumberOfPages && totalNumberOfPages !== 3 && <PageButton onClick={() => setPage(totalNumberOfPages)}>
                {totalNumberOfPages}
              </PageButton>}
            </Fragment>
          )}
        </PaginationWrapper>
      ) : null}
      <ItemRange>
        {numberOfItems > 0 ? minimumItemBoundPerPage : 0} - {maximumItemBoundPerPage > numberOfItems ? numberOfItems : maximumItemBoundPerPage} of {numberOfItems} {itemText ? itemText : `channels`}
      </ItemRange>
    </PaginationContainer>
  );
};

export default Pagination;