<?php

include("GnutecaWebServices.class.php");

class gnuteca3WebServicesProfile
{
    private $webService;
    private $MIOLO;

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

    /**
     * WebService que lista os dados do usuário
     * @param int $clientId
     * @param string $clientPassword
     * @param int $personId
     * @return bool|string
     */
    public function getProfile($clientId, $clientPassword, $personId)
    {
        $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("getProfile", false)) 
        {
            $retorno->message = $this->webService->__getErrorStr();
            return json_encode($retorno);
        }

        $busPerson = $this->MIOLO->getBusiness('gnuteca3', 'BusPerson');
        $busPerson->personIdS = $personId;
        $profile = $busPerson->searchPerson(TRUE);
        !empty($profile) && $profile = $profile[0];

        $profile = [
            "Nome" => $profile->personName,
            "Endereço e Número" => $profile->location . ', ' . $profile->number ?? '-',
            "Complemento" => $profile->complement ?? '-',
            "Cidade" => $profile->name ?? '-',
            "CEP" => $profile->zipCode ?? '-',
            "E-mail" => $profile->email ?? '-',
            "Telefone" => $profile->phones ?? '-',
        ];

        $retorno->profile = $profile;

        // Obtém a imagem do usuário
        $busFile = $this->MIOLO->getBusiness("gnuteca3", "BusFile");
        $imageName = $busFile->fileExists("person", $personId) ? $personId : "default";
        $retorno->profilePicture = $this->MIOLO->getAbsoluteURL(null) . "miolo25/html/file.php?folder=person&file=$imageName";

        $retorno->politics = $this->listPolicy($personId);
        return json_encode($retorno);
    }

    /**
     * Webservice que lista as políticas da unidade
     * @param int $clientId
     * @param string $clientPassword
     * @param int $personId
     * @param int $libraryUnitId
     * @return bool|string
     */
    private function listPolicy($personId)
    {
        $busOperationLoan = $this->MIOLO->getBusiness('gnuteca3', 'BusOperationLoan');
        $session = $this->MIOLO->getSession();
        // $libraryUnitId = $session->getValue('MyLibraryLogged');
        $busOperationLoan->setLibraryUnit(1); // OBTER A LIBRARYUNITID LOGADA
        $busOperationLoan->setPerson($personId);
        
        $person = $busOperationLoan->getPerson();
        $politicas = $person->policy;
        
        $policy = [];
        foreach ($politicas as $politica) 
        {
            $policy[] = [
                "materialGender" => $politica->materialGenderDescription,
                "loanDays" => $politica->loanDays,
                "loanLimit" => $politica->loanLimit,
                "openLoans" => $politica->loanOpenOfAssociation,
                "delayedLoans" => $politica->delayLoan,
                "reserves" => $politica->reserves,
                "answeredReserves" => $politica->answeredReserves
            ];
        }

        return $policy;
    }
}