import { Component, Input, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core';
import { NumberUtils } from '../../utilities/number-utils';

@Component({
  selector: 'pager',
  templateUrl: './pager.component.html',
  styleUrls: ['./pager.component.scss']
})
export class PagerComponent implements OnChanges {

  @Input() public alignJustify: boolean = true;
  @Input() public page: number;
  @Input() public pages: number[];
  @Input() public toolTips: string[];
  @Input() public minPage: number = 1;
  @Input() public maxPage: number = Number.POSITIVE_INFINITY; // default no limit
  @Input() public prefix: string = "Page";
  @Input() public numberOfPages: number = 5; // max number of pages to show at a time between "Previous" and "Next" buttons
  @Output() public pageChange = new EventEmitter<number>();
  @Output() public addingNewPage = new EventEmitter<number>();

  pagesStart: number;
  pagesEnd: number;

  constructor() { }

  public get lastPage(): number {
    return this.pages[this.pages.length - 1];
  }

  ngOnChanges(change: SimpleChanges) {
    if (false
      || (change.pages && change.pages.currentValue !== change.pages.previousValue)
      || (change.maxPage && change.maxPage.currentValue !== change.maxPage.previousValue)
    ) {
      if (this.pages.length > 0 && (this.page > Math.min(this.pages.length + 1, this.maxPage) || this.page < this.minPage)) {

        this.page = NumberUtils.Clamp(this.page, this.minPage, Math.min(this.pages.length + 1, this.maxPage));
        this.gotoPage(this.page);
        // this.calculatePages();
      }
    }
    if (false
      || (change.page && change.page.currentValue !== change.page.previousValue)
      || (change.maxPage && change.maxPage.currentValue !== change.maxPage.previousValue)
    ) {
      if (this.pages.length > 0) {

        this.page = NumberUtils.Clamp(this.page, this.minPage, Math.min(this.pages.length + 1, this.maxPage));
        // 
        this.calculatePages();
      }
    }
  }

  gotoPage(p: number) {
    // 
    this.pageChange.emit(p);
  }

  getPageToolTip(page: number): string {
    if (this.toolTips && this.toolTips.length > (page - 1)) {
      return this.toolTips[(page - 1)];
    }
    return `Page ${page}`;
  }

  getNewPageToolTip(): string {
    return this.page === this.pages[this.pages.length - 1] ? `New ${this.prefix}` : `Next ${this.prefix}`;
  }

  calculatePages() {
    // 
    if (this.pages.length > 0) {

      if (!this.pages.includes(this.page)) {
        this.pages.push(this.page);
        this.pages = this.pages.sort(NumberUtils.SortFunction);
      }
      let currentPage = this.page;
      let lastPage = this.pages[this.pages.length - 1];
      let pagesAfter = Math.floor(this.numberOfPages / 2);
      let pagesBefore = this.numberOfPages - pagesAfter;
      if (currentPage - pagesBefore < 0) {
        pagesAfter += pagesBefore - currentPage;
      }
      if (currentPage + pagesAfter > lastPage) {
        pagesBefore += currentPage + pagesAfter - lastPage;
      }
      this.pagesStart = Math.max(0, currentPage - pagesBefore);
      this.pagesEnd = Math.min(lastPage, currentPage + pagesAfter);
    }
  }
  addNewPage() {
    if (this.page < this.maxPage)
      this.addingNewPage.emit(this.page + 1);
  }
}
