<?php

namespace AutoAdmin\Bundle\Content\Duplicator;

use AutoAdmin\Bundle\BrickBuilder\Builder\BuilderService;
use AutoAdmin\Bundle\Content\Service\DuplicatorService;
use AutoAdmin\Bundle\Media\Model\FileInterface;
use AutoAdmin\Bundle\Media\Model\ImageInterface;
use AutoAdmin\Bundle\Media\Service\MediaManipulator;
use Doctrine\ORM\EntityManagerInterface;

class FileAndImageDuplicator extends AbstractDuplicator
{
    protected MediaManipulator $mediaManipulator;
    public function __construct(
        DuplicatorService $duplicatorService,
        BuilderService $builderService,
        EntityManagerInterface $entityManager,
        MediaManipulator $mediaManipulator
    ) {
        parent::__construct($duplicatorService, $builderService, $entityManager);

        $this->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;
    }
}
