<?php

namespace Webkul\WooCommerceBundle\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 WoocommerceInstallCommand extends Command
{
    protected static $defaultName = 'woocommerce:setup:install';

    protected function configure()
    {
        $this->setName('woocommerce:setup:install')
            ->setDescription('Install Woocommerce Akeneo connector setup')
            ->setHelp('setups Woocommerce 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 >= "5.0") {
            
            $this->runAkeneo5WoocommerceInstallationCommand($input, $output);
        } elseif ($version >= "4.0") {
            $this->runAkeneo4WoocommerceInstallationCommand($input, $output);
        } else {
            $this->runAkeneoWoocommerceInstallationCommand($input, $output);
        }



        return 0;
    }

    protected function runAkeneo5WoocommerceInstallationCommand(InputInterface $input, OutputInterface $output)
    {
        $this->runAkeneoCommanInstallation($input, $output);

        $yarn_pkg = preg_replace("/\r|\n/", "", shell_exec('which yarnpkg || which yarn || echo "yarn"'));
        shell_exec($yarn_pkg . ' run update-extensions');
    }

    protected function runAkeneoCommanInstallation(InputInterface $input, OutputInterface $output)
    {
        shell_exec('rm -rf ./var/cache/ && php bin/console cache:warmup;');
        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
        );

        $yarn_pkg = preg_replace("/\r|\n/", "", shell_exec('which yarnpkg || which yarn || echo "yarn"'));

        shell_exec('rm -rf public/css');
        shell_exec($yarn_pkg . ' run less');

        shell_exec('rm -rf public/dist-dev');
        shell_exec($yarn_pkg . ' run webpack-dev');
 
        shell_exec('rm -rf public/dist');
        shell_exec($yarn_pkg . ' run webpack');
    }



    protected function runAkeneo4WoocommerceInstallationCommand(InputInterface $input, OutputInterface $output)
    {   
        shell_exec('rm -rf ./var/cache/ && php bin/console cache:warmup;');
        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 runAkeneoWoocommerceInstallationCommand(InputInterface $input, OutputInterface $output)
    {
        $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('yarn run webpack');
    }

    protected function runCommand($name, array $args, $output)
    {
        $command = $this->getApplication()->find($name);
        $commandInput = new ArrayInput(
            $args
        );
        $command->run($commandInput, $output);
    }
}
