<?php

namespace Fifa\ConnectId\Sdk\Registration\Model;

use Fifa\ConnectId\Sdk\Api\Model\PersonRegistrationContextType;
use Fifa\ConnectId\Sdk\Api\Model\PersonType;
use Fifa\ConnectId\Sdk\Model\PersonDuplicateWithDetails;
use Fifa\ConnectServiceBus\Sdk\Utils\ToStringConverter;

/**
 * Class UpdateResult
 * @package Fifa\ConnectId\Sdk\Registration\Model
 */
class UpdateResult
{
    /**
     * @var PersonType
     */
    private $personType;

    /**
     * @var bool
     */
    private $isSuccessful;

    /**
     * @var array|PersonDuplicateWithDetails[]|null
     */
    private $personDuplicateWithDetails;

    /**
     * @var null|PersonRegistrationContextType
     */
    private $registrationContext;

    /**
     * Constructor
     *
     * @param bool $isSuccessful
     * @param PersonType|null $personType
     * @param PersonDuplicateWithDetails[]|null $personDuplicateWithDetails
     * @param null|PersonRegistrationContextType $registrationContext
     */
    private function __construct(bool $isSuccessful, ?PersonType $personType, ?array $personDuplicateWithDetails, ?PersonRegistrationContextType $registrationContext)
    {
        $this->isSuccessful = $isSuccessful;
        $this->personType = $personType;
        $this->personDuplicateWithDetails = $personDuplicateWithDetails;
        $this->registrationContext = $registrationContext;
    }

    /**
     * String representation of the object
     *
     * @return string
     */
    public function __toString()
    {
        return sprintf('IsSuccessful: %s, Result: %s, Duplicates: %s, Registration context: %s',
            $this->isSuccessful() ? 'true': 'false',
            $this->personType,
            ToStringConverter::arrayToString($this->getPersonDuplicateWithDetails() ?? []),
            $this->getRegistrationContext()
        );
    }

    /**
     * Create an instance of UpdateResult in case of successful update
     *
     * @param PersonType $personType
     * @return UpdateResult
     */
    public static function success(PersonType $personType): UpdateResult
    {
        return new self(true,  $personType, null, null);
    }

    /**
     * Create an instance of UpdateResult in case of duplicates found
     *
     * @param DuplicatesOfRegisteredPerson $duplicates
     * @return UpdateResult
     */
    public static function duplicatesFound(DuplicatesOfRegisteredPerson $duplicates): UpdateResult
    {
        return new self(false, null, $duplicates->getDuplicatesWithDetails(), $duplicates->getPersonRegistrationContext());
    }

    /**
     * @return PersonType|null
     */
    public function getPersonType(): ?PersonType
    {
        return $this->personType;
    }

    /**
     * @return boolean
     */
    public function isSuccessful(): bool
    {
        return $this->isSuccessful;
    }

    /**
     * @return array|PersonDuplicateWithDetails[]|null
     */
    public function getPersonDuplicateWithDetails(): ?array
    {
        return $this->personDuplicateWithDetails;
    }

    /**
     * @return null|PersonRegistrationContextType
     */
    public function getRegistrationContext(): ?PersonRegistrationContextType
    {
        return $this->registrationContext;
    }
}