<?php

namespace App\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 * class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */
use App\DAO\Filter\QueryFilter;
use App\DAO\Pagination\Pagination;
use App\Services\ServiceResponse\ServiceResponse;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Exceptions\ValidationException;
use Config\Services;
use Psr\Log\LoggerInterface;
use App\Services\LogsysService;

class BaseController extends Controller {

	/**
	 * An array of helpers to be loaded automatically upon
	 * class instantiation.
	 * These helpers will be available
	 * to all other controllers that extend BaseController.
	 *
	 * @var array
	 */
	protected $http_error_messages = [ 
		HTTP_CODE_OK => HTTP_REASON_OK,
		HTTP_CODE_NO_CONTENT => HTTP_REASON_NO_CONTENT,
		HTTP_CODE_BAD_REQUEST => HTTP_REASON_BAD_REQUEST,
		HTTP_CODE_UNAUTHORIZED => HTTP_REASON_UNAUTHORIZED,
		HTTP_CODE_FORBIDDEN => HTTP_REASON_FORBIDDEN,
		HTTP_CODE_NOT_FOUND => HTTP_REASON_NOT_FOUND,
		HTTP_CODE_METHOD_NOT_ALLOWED => HTTP_REASON_METHOD_NOT_ALLOWED,
		HTTP_CODE_INTERNAL_SERVER_ERROR => HTTP_REASON_INTERNAL_SERVER_ERROR
	];
	protected $helpers = [ ];
	/**
	 *
	 * @var \CodeIgniter\Validation\Validation
	 */
	protected $validation;

	/**
	 *
	 * @var \App\Services\LogsysService
	 */
	protected $logsysService;
	/**
	 * server variable request_method
	 */
	protected $request_method;
	/**
	 * server variable path_info
	 */
	protected $path_into;

	public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) {

		$this->logsysService = new LogsysService();
		$this->path_into = $_SERVER ['PATH_INFO'];
		$this->request_method = $_SERVER ['REQUEST_METHOD'];

		parent::initController( $request, $response, $logger );
		$this->validation = Services::validation();

	}

	public function getResponse(ServiceResponse $service_response, array $response_messages) {

		$responseArray = [ ];
		$status_service_response = $service_response->getStatus();
		$status_service_data = $service_response->getData();

		if ($status_service_data) {
			$responseArray ['data'] = $status_service_data;
		}
		$responseArray ['message'] = $response_messages [$status_service_response] ['message'];
		if ($service_response->getErrors()) {
			$responseArray ['errors'] = $service_response->getErrors();
		}
		$code = $response_messages [$status_service_response] ['code'];
		$resp = $this->response->setStatusCode( $code )
			->setJSON( $responseArray );

		return $this->response->setStatusCode( $code )
			->setJSON( $responseArray );

	}

	public function getDataPost(): array {		

		return count( $_POST ) === 0 ? json_decode( file_get_contents( 'php://input' ), true ) : $_POST;

	}

	public function getPaginationFromRequest(): ?Pagination {

		$current_page = ( int ) $_GET ['page'];
		$per_page = ( int ) $_GET ['per_page'];
		$pagination_is_defined = isset( $current_page ) && $current_page > 0 && isset( $per_page ) && $per_page > 0;
		if ($pagination_is_defined) {
			return Pagination::builder()->setCurrentPage( $current_page )
				->setPerPage( $per_page )
				->build();
		} else {
			return null;
		}

	}

}
