<?php

namespace AutoAdmin\Bundle\Content\Duplicator;

use AutoAdmin\Bundle\Media\Model\FileInterface;
use AutoAdmin\Bundle\Media\Model\ImageInterface;
use AutoAdmin\Bundle\Media\Service\MediaManipulator;

class FileAndImageDuplicator extends AbstractDuplicator
{
    public function __construct(
        protected MediaManipulator $mediaManipulator
    )
    {
    }

    /**
     * @return string[]
     */
    public function supportInterfaces(): array
    {
        return [
            FileInterface::class,
            ImageInterface::class
        ];
    }

    /**
     * @param FileInterface $entity
     * @param FileInterface $duplicate
     *
     * @return FileInterface
     */
    public function duplicate(object $entity, object $duplicate, ?object $parent = null): object
    {
        $duplicate = $this->mediaManipulator->duplicate($entity, $duplicate);

        $duplicate->setTitle($entity->getTitle());
        $duplicate->setFile($entity->getFile());
        $duplicate->setMimeType($entity->getMimeType());
        $duplicate->setSize($entity->getSize());
        $duplicate->setExtension($entity->getExtension());

        if ($duplicate instanceof ImageInterface) {
            $duplicate->setAlt($entity->getAlt());
            $duplicate->setWidth($entity->getWidth());
            $duplicate->setHeight($entity->getHeight());
        }

        return $duplicate;
    }
}
