<?php

/*
 * This source file is available under two different licenses:
 *   - GNU General Public License version 3 (GPLv3)
 *   - DACHCOM Commercial License (DCL)
 * Full copyright and license information is available in
 * LICENSE.md which is distributed with this source code.
 *
 * @copyright  Copyright (c) DACHCOM.DIGITAL AG (https://www.dachcom-digital.com)
 * @license    GPLv3 and DCL
 */

namespace MembersBundle\Tool;

use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\Exception\NotFoundException;
use Symfony\Component\Console\Output\OutputInterface;

class ClassInstaller
{
    protected ?OutputInterface $logger = null;

    public function setLogger(OutputInterface $logger): void
    {
        $this->logger = $logger;
    }

    public function installClasses(array $classes): void
    {
        foreach ($this->getClasses($classes) as $className => $path) {
            $class = new ClassDefinition();

            $id = null;

            try {
                $id = $class->getDao()->getIdByName($className);
            } catch (NotFoundException $e) {
                // fail silently
            }

            if ($id !== null) {
                $this->log(sprintf('<comment>Class "%s" already exists.</comment>', $className));

                continue;
            }

            $class->setName($className);

            $data = file_get_contents($path);
            $success = ClassDefinition\Service::importClassDefinitionFromJson($class, $data);

            if (!$success) {
                $this->log(sprintf('Could not import Class "%s".', $className));
            } else {
                $this->log(sprintf('Class "%s" <info>successfully</info> installed.', $className));
            }
        }
    }

    protected function getClasses(array $classes): array
    {
        $result = [];

        foreach ($classes as $className) {
            $filename = sprintf('class_%s_export.json', $className);
            $path = realpath(sprintf('%s/config/install/classes/%s', dirname(__DIR__, 2), $filename));

            if ($path === false || !is_file($path)) {
                throw new \RuntimeException(sprintf(
                    'Class export for class "%s" was expected in "%s" but file does not exist',
                    $className,
                    $path
                ));
            }

            $result[$className] = $path;
        }

        return $result;
    }

    protected function log(string $msg): void
    {
        if (!$this->logger instanceof OutputInterface) {
            return;
        }

        $this->logger->writeln($msg);
    }
}
