<?php

namespace Webkul\DamBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;

class UninstallCommand extends ContainerAwareCommand
{
    /** @var attributeRepository */
    protected $attributeRepository;

    /** @var EntityManager */
    protected $em;

    public function __construct(
        $attributeRepository,
        $em
    ) {
        $this->attributeRepository = $attributeRepository;
        $this->em = $em;
        parent::__construct();
    }
    protected function configure()
    {
        parent::configure();

        $this->setName('dam:asset:uninstall')
            ->setDescription('Unistall Dam Module');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        //remove from routing.yml
        
        $damAttributes = $this->attributeRepository
                              ->findBy(['type'=>'pim_catalog_asset_group']);
     
        if (!empty($damAttributes)) {
            $em = $this->em;
            
            foreach ($damAttributes as $damAttribute) {
                $em->remove($damAttribute);
            }
            $em->flush();
        }

        if (class_exists('Pim\Bundle\CatalogBundle\Version')) {
            // version 2
            $versionClass = new \Pim\Bundle\CatalogBundle\Version();
        } elseif (class_exists('Akeneo\Platform\CommunityVersion')) {
            // version 3 or later
            $versionClass = new \Akeneo\Platform\CommunityVersion();
        }
        $version = $versionClass::VERSION;
        
        
        $this->removeFromRouting($version);
        $this->removeFromAppKernel($version);

        $output->writeln("Dam module has been successfully uninstalled.");
    }

    protected function removeFromRouting($version)
    {
        if($version > '4.0') {
            $routeFile = $this->getContainer()->get('kernel')->getProjectDir() . '/config/routes/webkul_dambundle.yml';
            $search      = "WebkulDamBundle/Resources/config/routing.yml";
            $lines       = file($routeFile);
            $line_number = false;
            
            while (list($key, $line) = each($lines) and !$line_number) {
                $line_number = (strpos($line, $search) !== false) ? $key + 1 : $line_number;
            }
        } else {
            $routeFile = $this->getContainer()->get('kernel')->getRootDir() . '/config/routing.yml';
            $search      = "WebkulDamBundle/Resources/config/routing.yml";
            $lines       = file($routeFile);
            $line_number = false;
    
            while (list($key, $line) = each($lines) and !$line_number) {
                $line_number = (strpos($line, $search) !== false) ? $key + 1 : $line_number;
            }
        }

        if ($line_number) {
            $delete_from_line = $line_number - 1;
            $delete_to_line = $line_number + 1;
    
            $this->deleteLineFromFile($delete_from_line, $delete_to_line, $routeFile);
        }
    }

    protected function removeFromAppKernel($version)
    {
        if($version > '4.0') {
            $fileName = $this->getContainer()->get('kernel')->getProjectDir() . '/config/bundles.php';
            $search      = "Webkul\DamBundle\WebkulDamBundle::class => ['all' => true]";
            $lines       = file($fileName);
            $line_number = false;
    
            while (list($key, $line) = each($lines) and !$line_number) {
                $line_number = (strpos($line, $search) !== false) ? $key + 1 : $line_number;
            }
            
        } else {
            $fileName = $this->getContainer()->get('kernel')->getRootDir() . '/AppKernel.php';
            $search      = "new Webkul\DamBundle\WebkulDamBundle()";
            $lines       = file($fileName);
            $line_number = false;
    
            while (list($key, $line) = each($lines) and !$line_number) {
                $line_number = (strpos($line, $search) !== false) ? $key + 1 : $line_number;
            }
        }

        if ($line_number) {
            $delete_from_line = $line_number;
            $delete_to_line = $line_number;
    
            $this->deleteLineFromFile($delete_from_line, $delete_to_line, $fileName);
        }
    }

    protected function deleteLineFromFile($from, $to, $fileName)
    {
        exec('sed -i.bak ' . $from . ',' . $to . 'd ' . $fileName);
        exec('rm -rf '.$fileName. '.bak');
    }
}
