<?php

declare(strict_types=1);

namespace Itdatex\Rechnungseingang\Vendor\JMS\Serializer\Metadata\Driver;

use Itdatex\Rechnungseingang\Vendor\JMS\Serializer\Metadata\ClassMetadata as SerializerClassMetadata;
use Itdatex\Rechnungseingang\Vendor\JMS\Serializer\Metadata\ExpressionPropertyMetadata;
use Itdatex\Rechnungseingang\Vendor\JMS\Serializer\Metadata\PropertyMetadata;
use Itdatex\Rechnungseingang\Vendor\JMS\Serializer\Metadata\StaticPropertyMetadata;
use Itdatex\Rechnungseingang\Vendor\JMS\Serializer\Metadata\VirtualPropertyMetadata;
use Itdatex\Rechnungseingang\Vendor\Metadata\ClassMetadata;
use Itdatex\Rechnungseingang\Vendor\Metadata\Driver\DriverInterface;
use ReflectionClass;
use ReflectionException;
use ReflectionProperty;

class EnumPropertiesDriver implements DriverInterface
{
    /**
     * @var DriverInterface
     */
    protected $delegate;

    public function __construct(DriverInterface $delegate)
    {
        $this->delegate = $delegate;
    }

    public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
    {
        $classMetadata = $this->delegate->loadMetadataForClass($class);

        if (null === $classMetadata) {
            return null;
        }

        \assert($classMetadata instanceof SerializerClassMetadata);

        // We base our scan on the internal driver's property list so that we
        // respect any internal allow/blocklist like in the AnnotationDriver
        foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
            // If the inner driver provides a type, don't guess anymore.
            if ($propertyMetadata->type || $this->isVirtualProperty($propertyMetadata)) {
                continue;
            }

            try {
                $propertyReflection = $this->getReflection($propertyMetadata);
                if ($enum = $this->getEnumReflection($propertyReflection)) {
                    $serializerType = [
                        'name' => 'enum',
                        'params' => [
                            ['name' => $enum->getName(), 'params' => []],
                            $enum->isBacked() ? 'value' : 'name',
                        ],
                    ];
                    $propertyMetadata->setType($serializerType);
                }
            } catch (ReflectionException $e) {
                continue;
            }
        }

        return $classMetadata;
    }

    private function isVirtualProperty(PropertyMetadata $propertyMetadata): bool
    {
        return $propertyMetadata instanceof VirtualPropertyMetadata
            || $propertyMetadata instanceof StaticPropertyMetadata
            || $propertyMetadata instanceof ExpressionPropertyMetadata;
    }

    private function getReflection(PropertyMetadata $propertyMetadata): ReflectionProperty
    {
        return new ReflectionProperty($propertyMetadata->class, $propertyMetadata->name);
    }

    private function getEnumReflection(ReflectionProperty $propertyReflection): ?\ReflectionEnum
    {
        $reflectionType = $propertyReflection->getType();

        if (!($reflectionType instanceof \ReflectionNamedType)) {
            return null;
        }

        return enum_exists($reflectionType->getName()) ? new \ReflectionEnum($reflectionType->getName()) : null;
    }
}
