<?php

namespace Fifa\ConnectId\Sdk\Tms\Validators;

use Fifa\ConnectId\Sdk\Model\Transfer\FirstProRegistration;
use Fifa\ConnectId\Sdk\Registration\ValidationException;
use Fifa\ConnectId\Sdk\Util\Data\Person\Level;
use InvalidArgumentException;

class FirstProRegistrationValidator
{
    /**
     * @param FirstProRegistration $firstProRegistration
     *
     * @throws ValidationException
     */
    public static function validate(FirstProRegistration $firstProRegistration)
    {
        if ($firstProRegistration->Player == null) {
            throw new ValidationException('Player must be provided.');
        }

        $playerRegistrations = $firstProRegistration->Player->PlayerRegistration;
        if (empty($playerRegistrations) || count($playerRegistrations) != 1) {
            throw new ValidationException('Exactly one player registration must be provided');
        }

        $registration = reset($playerRegistrations);
        if ($registration->Level != strtolower(Level::PRO)) {
            throw new ValidationException('Only professional registration is accepted');
        }
        if (empty($registration->ClubTrainingCategory)) {
            throw new ValidationException('Club training category must be provided');
        }

        $organisationOfficialRegistrations = $firstProRegistration->Player->OrganisationOfficialRegistration;
        if (!empty($organisationOfficialRegistrations)) {
            throw new ValidationException('There should be no organisation official registrations');
        }

        $matchOfficialRegistrations = $firstProRegistration->Player->MatchOfficialRegistration;
        if (!empty($matchOfficialRegistrations)) {
            throw new ValidationException('There should be no match official registrations');
        }

        $teamOfficialRegistrations = $firstProRegistration->Player->TeamOfficialRegistration;
        if (!empty($teamOfficialRegistrations)) {
            throw new ValidationException('There should be no team official registrations');
        }
    }
}