<?php
namespace Fifa\ConnectId\Sdk\Util\ToString;

use DateTime;
use Fifa\ConnectId\Sdk\Api\Model\PersonDuplicateType;
use Fifa\ConnectId\Sdk\Api\Model\PersonDuplicateMatchType;
use Fifa\ConnectId\Sdk\Util\ToString\Exception\ToStringException;

/**
 * Class ToString
 * @package Fifa\ConnectId\Sdk\Util\ToString
 */
class ToString implements ToStringInterface
{
    /**
     * Returns a string representation for given object.
     * Throws an exception in case when object type is not handled in the service.
     *
     * @param $object
     * @return mixed
     * @throws ToStringException
     */
    public static function getValue($object)
    {
        if ($object instanceof PersonDuplicateType) {
            return self::getPersonDuplicateTypeString($object);
        }
        elseif ($object instanceof PersonDuplicateMatchType) {
            return self::getPersonMatchTypeString($object);
        }
        elseif ($object instanceof DateTime) {
            return $object->format('Y-m-d');
        }

        $className = is_null($object) ? 'null' : get_class($object);
        throw new ToStringException('ToString service does not support objects of type: ' . $className);
    }

    /**
     * Returns string representation of PersonDuplicateType object
     *
     * @param PersonDuplicateType $duplicatedPerson
     * @return string
     * @throws ToStringException
     */
    private static function getPersonDuplicateTypeString(PersonDuplicateType $duplicatedPerson): string
    {
        return  $duplicatedPerson->getPersonFifaId() . ' with score ' . $duplicatedPerson->getProximityScore() . '. ' .
                self::getValue($duplicatedPerson->getDuplicateMatchType());
    }

    /**
     * Returns string representation of PersonDuplicateMatchType object
     *
     * @param PersonDuplicateMatchType $personMatch
     * @return string
     */
    private static function getPersonMatchTypeString(PersonDuplicateMatchType $personMatch): string
    {
        $queried = 'Queried: ' . $personMatch->getQueriedHash() . ' / ' . $personMatch->getQueriedDateOfBirth() . '. ';
        $matched = 'Matched: ' . $personMatch->getMatchedHash() . ' / ' . $personMatch->getMatchedDateOfBirth() . '.';
        return $queried . $matched;
    }
}