<?php

namespace Webkul\DamBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Command\Command;

// ContainerAwareCommand
class DamInstallationCommand extends Command
{
    protected static $defaultName = 'wk_dam:setup:install';

    protected function configure()
    {
        $this->setName('wk_dam:setup:install')
            ->setDescription('Install Dam Akeneo Connector module setup')
            ->setHelp('setups Dam Akeneo Connector bundle installation');
    }
 
    
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $errorFlag = false;

        /* version wise loading */
        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->updateLessFile($version);


        if ($version >= "4.0") {
            $this->runAkeneo4DamInstallationCommand($input, $output);
        } else {
            $this->runAkeneoDamInstallationCommand($input, $output);
        }

        return 0;
    }

    protected function runAkeneo4DamInstallationCommand(InputInterface $input, OutputInterface $output)
    {
        shell_exec('rm -rf public/bundles public/js');
        $this->runCommand(
            'pim:installer:assets',
            [
                '--clean' => null,
                '--symlink'  => null,
            ],
            $output
        );

        $this->runCommand(
            'doctrine:schema:update',
            [
                '--force' => null,
            ],
            $output
        );
        
        shell_exec('rm -rf public/css');
        shell_exec('yarnpkg run less');
        
        shell_exec('rm -rf public/dist-dev');
        shell_exec('yarnpkg run webpack-dev');
        
        shell_exec('rm -rf public/dist');
        shell_exec('yarnpkg run webpack');
    }

   

    protected function runAkeneoDamInstallationCommand(InputInterface $input, OutputInterface $output)
    {
        shell_exec('rm -rf ./var/cache/ && php bin/console cache:warmup;');
        shell_exec('rm -rf web/bundles web/js');
        $this->runCommand(
            'pim:install:asset',
            [
                '--env' => 'prod',
            ],
            $output
        );

        $this->runCommand(
            'assets:install',
            [
                '--symlink' => null,
                '--relative' => null,
            ],
            $output
        );

        $this->runCommand(
            'doctrine:schema:update',
            [
                '--force' => null,
            ],
            $output
        );
        shell_exec('rm -rf web/css');
        shell_exec('yarn run less');
        
        shell_exec('rm -rf web/dist-dev');
        shell_exec('yarn run webpack-dev');
        
        shell_exec('rm -rf web/dist');
        shell_exec('yarn run webpack');
    }

    protected function runCommand($name, array $args, $output)
    {
        $command = $this->getApplication()->find($name);
        $commandInput = new ArrayInput(
            $args
        );
        $command->run($commandInput, $output);
    }
    
    protected function updateLessFile($version)
    {
        $location = __DIR__.'/../Resources/public/less/index.less';
        if (!is_writable($location)) {
            throw new \Exception(sprintf('%s file is not writable, provide the write permission', $location));
        }
        $conf = file_get_contents($location);

        if ($version >= "4.0") {
            $searchValue = './web';
            $replaceValue = './public';
        } else {
            $searchValue = './public';
            $replaceValue = './web';
        }

        $value = str_replace($searchValue, $replaceValue, $conf);
        file_put_contents($location, $value);
    }
}
