<?php
namespace Fifa\ConnectId\Command\Helpers;

use Fifa\ConnectId\Sdk\Exception\XmlSerializationException;
use Fifa\ConnectId\Sdk\Model\DataStandard\PersonLocal;
use Fifa\ConnectId\Sdk\Model\DataStandard\PlayerRegistration;
use Fifa\ConnectId\Sdk\Model\PersonDetailsRequest;
use Fifa\ConnectId\Sdk\Registration\PersonDetailsProviderInterface;
use Fifa\ConnectId\Sdk\Util\Data\ClubTrainingCategory;
use Fifa\ConnectId\Sdk\Util\Data\Discipline;
use Fifa\ConnectId\Sdk\Util\Data\Gender;
use Fifa\ConnectId\Sdk\Util\Data\Person\Level;
use Fifa\ConnectId\Sdk\Util\Data\RegistrationNature;
use Fifa\ConnectId\Sdk\Util\Data\Status;
use Fifa\ConnectId\Sdk\Util\PersonLocalXmlSerializer\PersonLocalXmlSerializer;
use Psr\Log\LoggerInterface;

/**
 * Class ListenerTestPersonDetailsProvider
 */
class ListenerTestPersonDetailsProvider implements PersonDetailsProviderInterface
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * ListenerTestPersonDetailsProvider constructor.
     * @param LoggerInterface $logger
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * Gets detailed information about person with given request.
     *
     * @param PersonDetailsRequest $request
     * @return string|null - the Person Local type from the FIFA Data Standard.
     * @throws \Fifa\ConnectId\Sdk\Util\ToString\Exception\ToStringException
     */
    public function getPersonDetails(PersonDetailsRequest $request): ?string
    {
        // create PersonLocal object with required fields
        $localLastName = 'Novak';
        $gender = Gender::MALE;
        $nationality = 'IN';
        $dateOfBirth = new \DateTime('2000-03-29', new \DateTimeZone('UTC'));
        $countryOfBirth = 'PL';
        $placeOfBirth = 'Kraków';
        $localLanguage = 'eng';
        $localCountry = 'IN';
        $personFIFAId = $request->getUniqueFifaId();

        $personLocal = new PersonLocal($localLastName, $gender, $nationality, $dateOfBirth, $countryOfBirth, $placeOfBirth, $localLanguage, $localCountry, $personFIFAId);
        $personLocal->LocalFirstName = 'John'; // optional field
        $personLocal->PopularName = 'J Novak'; // optional field
        $personLocal->SecondNationality = 'PL'; // optional field

        // add player registration
        $personFIFAId = $request->getUniqueFifaId();
        $status = Status::ACTIVE;
        $organisationFIFAId = '103C3AB';
        $registrationValidFrom = new \DateTime('2017-06-01', new \DateTimeZone('UTC'));
        $level = strtolower(Level::PRO);
        $discipline = Discipline::BEACH_SOCCER;
        $registrationNature = RegistrationNature::LOAN;

        $playerRegistration = new PlayerRegistration($personFIFAId, $status, $organisationFIFAId, $registrationValidFrom, $level, $discipline, $registrationNature);
        $validToDate = new \DateTime('2018-05-31', new \DateTimeZone('UTC'));
        $playerRegistration->setRegistrationValidTo($validToDate); // optional field
        $playerRegistration->setClubTrainingCategory(ClubTrainingCategory::CATEGORY_4);
        $personLocal->addPlayerRegistration($playerRegistration);

        // serialize object into XML
        $xmlSerializer = new PersonLocalXmlSerializer();
        $xmlContent = null;

        try {
            $xmlContent = $xmlSerializer->serialize($personLocal);
        }
        catch (XmlSerializationException $e) {
            // handle serialization exception
            $this->logger->error(sprintf('Unable to serialize XML for person: %s. Details: %s', $personFIFAId, $e->__toString()));
        }

        return $xmlContent;
    }
}