<?php

namespace Lc\SovBundle\Maker;

use Doctrine\Common\Annotations\Annotation;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;

use function Symfony\Component\String\u;

class FactoryMaker extends AbstractMaker
{
    public static function getCommandName(): string
    {
        return 'make:factory';
    }

    public static function getCommandDescription(): string
    {
        return 'Creates a factory';
    }

    public function configureCommand(Command $command, InputConfiguration $inputConf)
    {
        $command
                /*->addArgument(
                        'factory-class',
                        InputArgument::OPTIONAL,
                        'Choose a name for your factory (e.g. <fg=yellow>MyEntityFactory</>)'
                )*/
                ->addArgument(
                        'namespace',
                        InputArgument::OPTIONAL,
                        'Namespace (e.g. <fg=yellow>Lc\SovBundle\Factory</>)'
                )
                ->addArgument(
                        'domain',
                        InputArgument::OPTIONAL,
                        'Domain (e.g. <fg=yellow>Order</>)'
                )
                ->addArgument(
                        'entity-class',
                        InputArgument::OPTIONAL,
                        'Define the entity (e.g. <fg=yellow>MyEntity</>)'
                )
                ->setHelp('');
    }

    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
    {
        $entityDetails = $generator->createClassNameDetails(
                $input->getArgument('entity-class'),
                'Entity\\',
                ''
        );

        $factoryClass = $input->getArgument('entity-class') . 'Factory';
        $factoryDetails = $generator->createClassNameDetails(
                $factoryClass,
                'Factory\\',
                ''
        );

        $options = [
                'namespace_path' => $input->getArgument('namespace'),
                'domain' => $input->getArgument('domain'),
                'entity_class' => $input->getArgument('entity-class'),
                'entity' => $entityDetails->getFullName(),
                'entity_variable' => '$' . u($input->getArgument('entity-class'))->camel()
        ];

        $generator->generateController(
                $factoryDetails->getFullName(),
                __DIR__ . '/../Resources/maker/factory/factory.tpl.php',
                $options
        );

        $interfaceName = $factoryClass . 'Interface';
        $generator->generateFile(
                $generator->getRootDirectory() . '/src/Factory/' . $interfaceName . '.php',
                __DIR__ . '/../Resources/maker/factory/factory_interface.tpl.php',
                [
                        'namespace_path' => $input->getArgument('namespace'),
                        'domain' => $input->getArgument('domain'),
                        'interface_name' => $interfaceName,
                ]
        );

        $generator->writeChanges();

        $this->writeSuccessMessage($io);
        $io->text('Next: Open your new factory class and configure it!');
    }

    public function configureDependencies(DependencyBuilder $dependencies)
    {
        $dependencies->addClassDependency(
                Annotation::class,
                'doctrine/annotations'
        );
    }
}
