<?php

namespace Fifa\ConnectId\Sdk\ApiModelValidation;

use Fifa\ConnectId\Sdk\Api\Model\PictureEmbeddedType;
use Fifa\ConnectId\Sdk\Model\Validation\DocumentValidator;
use Fifa\ConnectId\Sdk\Model\Validation\PictureValidator;
use Fifa\ConnectId\Sdk\Registration\ValidationException;

class PictureEmbeddedTypeValidator
{
    /**
     * Method that validates object provided and throws exception when object's properties are invalid
     *
     * @param PictureEmbeddedType|null $validationSubject
     * @param string $validationSubjectName
     * @return void
     * @throws ValidationException
     */
    public static function validate(?PictureEmbeddedType $validationSubject, string $validationSubjectName)
    {
        $validationSubjectName = ApiModelValidator::resolveValidationSubjectName($validationSubject, $validationSubjectName);

        ApiModelValidator::cannotBeNull($validationSubject, $validationSubjectName);
        ApiModelValidator::cannotBeNullOrWhitespace($validationSubject->getContentType(), $validationSubjectName.".contentType");

        $valuePropertyName = $validationSubjectName.".value";
        ApiModelValidator::cannotBeNull($validationSubject->getValue(), $valuePropertyName);

        self::validateImage($validationSubject->getValue(), $valuePropertyName);
    }

    /**
     * @param string $content
     * @param string $validationSubjectName
     * @throws ValidationException
     */
    private static function validateImage(string $content, string $validationSubjectName) {
        $result = (new DocumentValidator(new PictureValidator()))->validate($content);

        if(!$result->isValid()) {
            throw new ValidationException(sprintf("%s must be a valid image.", $validationSubjectName));
        }
    }
}