<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace BaksDev\Core\Command;

use DirectoryIterator;
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\InvalidArgumentException;
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\Attribute\Autowire;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;


#[AsCommand(name: 'temp:rename.image.dir')]
class RenameImageDitCommand extends Command
{
    private string $upload;

    public function __construct(#[Autowire('%kernel.project_dir%/public/upload/')] string $upload)
    {
        parent::__construct();
        $this->upload = $upload;
    }


    protected function execute(InputInterface $input, OutputInterface $output): int
    {


        foreach (new DirectoryIterator($this->upload) as $module)
        {
            if ($module->isDot() || !$module->isDir())
            {
                continue;
            }

            if (is_dir($module->getRealPath()))
            {

                foreach (new DirectoryIterator($module->getRealPath()) as $imgDir)
                {
                    if ($imgDir->isDot() || !$imgDir->isDir())
                    {
                        continue;
                    }


                    foreach (new DirectoryIterator($imgDir->getRealPath()) as $image)
                    {
                        if($image->isDot())
                        {
                            continue;
                        }

                        $filename = pathinfo($image->getPathName(), PATHINFO_FILENAME);

                        //$many = explode('.', $filename);

                        //if(isset($many[1])) { continue; }


                        /** Переименовываем файл */
                        $oldDir = $imgDir->getPathName();
                        $newDir = $imgDir->getPath().DIRECTORY_SEPARATOR.$filename;

                        if(is_dir($newDir))
                        {
                            $newFileName = 'image';

                            $pathInfo = pathinfo($image->getPathName());
                            $extension = isset($pathInfo['extension']) ? '.' . $pathInfo['extension'] : '';
                            $newFilePath = $image->getPath() . '/' . $newFileName . $extension;

                            if(is_file($image->getPathName()) && !file_exists($newFilePath))
                            {
                                rename($image->getPathName(), $newFilePath);
                            }


                        }

                        if($oldDir === $newDir)
                        {
                            continue;
                        }

                        if(is_dir($newDir))
                        {
                            continue;
                        }

                        if(is_dir($oldDir))
                        {
                            rename($oldDir, $newDir);
                        }



//                        try
//
//                        {
//                            rename($oldDir, $newDir);
//                        } catch(\ErrorException $exception)
//                        {
//
//                        }

                    }

                }

            }

        }



        return 0;
    }

}
