<?php

include("GnutecaWebServices.class.php");

class gnuteca3WebServicesPenalties
{
    private $webService;
    private $MIOLO;

    public function __construct() 
    {
        $this->webService = new GWebServices();
        $this->MIOLO = MIOLO::getInstance();
    }

    /**
     * WebService que lista as penalidades do usuário
     * @param int $clientId
     * @param string $clientPassword
     * @param int $personId
     * @param int $pageNumber
     * @return string|null JSON com as penalidades do usuário
     */
    public function listPenalties(
        $clientId,
        $clientPassword,
        $personId,
        $pageNumber = 1
        ) {

        $retorno = new stdClass();

        // Define acesso ao método e define a classe do Webservice
        $this->webService->__setClient($clientId, $clientPassword);
        $this->webService->setClassName(get_class($this));

        // Verifica se usuário possui acesso ao WebService.
        if (!$this->webService->__checkMethod("listPenalties", false)) 
        {
            $retorno->message = $this->webService->__getErrorStr();
            return json_encode($retorno);
        }

        $busPenalty = $this->MIOLO->getBusiness('gnuteca3', 'BusPenalty');

        $busPenalty->personIdS = $personId;
        $penalties = array_chunk($busPenalty->searchPenalty(true), LISTING_NREGS);

        $retorno->penalties = array_map(function($penalty) 
        {
            return 
            [
                "penaltyId" => $penalty->penaltyId,
                "observation" => $penalty->observation ?? '-',
                "penaltyDate" => $penalty->penaltyDate ?? '-',
                "penaltyEndDate" => $penalty->penaltyEndDate ?? '-',
                "libraryUnit" => $penalty->libraryName ?? '-'
            ];
        }, $penalties[$pageNumber - 1]);

        $retorno->numberOfPages = count($penalties);

        return json_encode($retorno);
    }
}