<?php

namespace App\DAO\Pagination;

use App\Utils\Entity;

class Pagination extends Entity {
	private $num_rows;
	private $per_page;
	private $current_page;

	public static function builder(): PaginationBuilder {

		return (new PaginationBuilder());

	}

	public function calculateLastPage(): int {

		return ceil( $this->num_rows / $this->per_page );

	}

	public function calculateOffset(): int {

		return $this->per_page * ($this->current_page - 1);

	}

	public function calculateLimit(): int {

		return $this->per_page;

	}

	public function calculateFirstPage(): ?int {

		return $this->num_rows > 0 ? 1 : null;

	}

	public function calculatePreviousPage(): ?int {

		return $this->current_page > 1 ? $this->current_page - 1 : null;

	}

	public function calculateNextPage(): ?int {

		return $this->current_page < $this->calculateLastPage() ? $this->current_page + 1 : null;

	}

	public function getNumRows(): int {

		return $this->num_rows;

	}

	public function getPerPage(): int {

		return $this->per_page;

	}

	public function getCurrentPage(): int {

		return $this->current_page;

	}

	public function setNumRows(int $num_rows): void {

		$this->num_rows = $num_rows;

	}

	public function setPerPage(int $per_page): void {

		$this->per_page = $per_page;

	}

	public function setCurrentPage(int $current_page): void {

		$this->current_page = $current_page;

	}

	public function toResponseArray(): array {

		return [ 
					'num_rows' => $this->num_rows,
					'current_page' => $this->current_page,
					'first_page' => $this->calculateFirstPage(),
					'last_page' => $this->calculateLastPage(),
					'previous_page' => $this->calculatePreviousPage(),
					'next_page' => $this->calculateNextPage(),
					'per_page' => $this->per_page
		];

	}

}