<?php

namespace App\Command;

use App\Entity\Department;
use App\Entity\Specialist;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;


class BaseCommand extends Command
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        parent::__construct();
        $this->container = $container;
    }

    protected static $defaultName = 'app:base';
    protected static $defaultDescription = 'Add a short description for your command';

    protected function configure(): void
    {
        $this
            ->setDescription(self::$defaultDescription)
            ->addArgument('var', InputArgument::OPTIONAL, 'variant update table')
            ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $entityManager = $this->container->get('doctrine')->getManager();
        $io = new SymfonyStyle($input, $output);

        $var = $input->getArgument('var');

        //  php bin/console app:base setAlias
        if ($var == 'setAlias') {
            $departments = $entityManager->getRepository(Department::class)
                ->findAll();

            foreach ($departments as $department) {
                $department->setAlias($this->translit($department->getName(), '-'));

                $entityManager->persist($department);
                $entityManager->flush();
                
                $io->success('departament updated ' . $department->getAlias());
            }

            $specialists = $entityManager->getRepository(Specialist::class)
                ->findAll();

            foreach ($specialists as $specialist) {
                $specialist->setAlias($specialist->getId() .'-'. $this->translit($specialist->getName(), '-'));

                $entityManager->persist($specialist);
                $entityManager->flush();
                
                $io->success('specialist updated ' . $specialist->getAlias());
            }
        }

        return Command::SUCCESS;
    }

    private function translit($string, $replacement = '_') {
        if (empty($string)) 
            return $string;

        $converter = array(
            'а' => 'a',   'б' => 'b',   'в' => 'v',
            'г' => 'g',   'д' => 'd',   'е' => 'ye',
            'ё' => 'yo',  'ж' => 'zh',  'з' => 'z',
            'и' => 'i',   'й' => 'y',   'к' => 'k',
            'л' => 'l',   'м' => 'm',   'н' => 'n',
            'о' => 'o',   'п' => 'p',   'р' => 'r',
            'с' => 's',   'т' => 't',   'у' => 'u',
            'ф' => 'f',   'х' => 'kh',  'ц' => 'ts',
            'ч' => 'ch',  'ш' => 'sh',  'щ' => 'shch',
            'ь' => '',    'ы' => 'y',   'ъ' => '',
            'э' => 'e',   'ю' => 'yu',  'я' => 'ya'
        );

        $string = mb_strtolower($string);
        $string = strtr($string, $converter);
        $string = preg_replace('~[^-a-z0-9_]+~u', $replacement, $string);
        $string = preg_replace('/\-{2,10}/m', $replacement, $string);
        return trim($string, "-");
    }
}
