<?php

namespace OfficeBrain\Bundle\SubShopBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Doctrine\ORM\Query\ResultSetMapping;
use OfficeBrain\Bundle\SearchBundle\Form\Data\ProductSearchData;
use OfficeBrain\Bundle\SearchBundle\Form\Type\ProductSearchType;

/**
 * Subshop Product Update From Product Criteria Command
 *
 * @author OfficeBrain 4045 <info@officebrain.com>
 */
class SubShopProductUpdateCommand extends ContainerAwareCommand
{
    CONST COMMAND_NAME = 'office_brain:solr:sync:subshop-product-update';

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName(self::COMMAND_NAME)
            ->setDescription('Command to sync updated subshop product data')
            ->setDefinition(array(
                new InputOption('productId', null, InputOption::VALUE_REQUIRED, 'product Id'),
            	new InputOption('projectId', null, InputOption::VALUE_REQUIRED, 'project Id'),
            	new InputOption('culture', '', InputOption::VALUE_REQUIRED, 'Supported search culture.'),
            	new InputOption('languageId', null, InputOption::VALUE_REQUIRED, 'language Id'),
            ))
            ->setHelp(<<<EOT
The <info>office_brain:solr:sync:subshop-product-update</info> command helps you to sync updated subshop products into solar schema.
EOT
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
    	/*
    	 * 18-feb-2016-4:00 any Product Update it will execute for all dist site and for all instance
    	 * */
    	return false;
    	exit;
    	$productIds = array();
        $startAt = microtime(true);
        
        
        $productId = $input->getOption('productId', null);
        $projectId = $input->getOption('projectId', null);
        $culture = $input->getOption('culture', null);
        $languageId = $input->getOption('languageId', null);
        
        if ($projectId == '')
        {
        	$output->writeln("Please provide a project id !");
        	return;
        }
        
        if ($culture == null) {
        	$output->writeln("Please provide a culture !");
        	return;
        }
        
    	if ($languageId == '')
        {
        	$output->writeln("Please provide a language id !");
        	return;
        }
        
        if ($productId == '')
        {
        	$output->writeln("Please provide a product id !");
        	return;
        }
        
        
        $this->container = $this->getContainer();
        $this->subShopManager = $this->container->get('office_brain_subshop_manager');
        $em = $this->container->get('doctrine')->getManager();
        $allInstance = $em->getRepository('OfficeBrainAccessManagementBundle:InstanceMaster')->getAllSubInstanceMasterArray();
        $mainInstance = $em->getRepository('OfficeBrainAccessManagementBundle:InstanceMaster')->getMainInstanceMasterObject();
        $mainInstance = $mainInstance[0]->getId();
        $this->subShopThemeManager = $this->getContainer()->get('office_brain_subshop_theme_manager');
        $kernelRootDir = 'php '.$this->subShopThemeManager->subShopMediaUploadLocation['kernel_root_dir'];
        $environment = $this->getContainer()->getParameter("kernel.environment");

        foreach ($allInstance as $instance) {
        	$subShop = $this->subShopManager->searchByParentIdSubShop($instance['id']);
        	
        	$cmd = $kernelRootDir.DIRECTORY_SEPARATOR.'console office_brain:solr:sync:subshop-product --projectId='.$projectId.' --culture='.$culture. ' --instanceId='.$mainInstance.' --subShopInstanceId='.$subShop->getParentId().' --languageId='.$languageId.' --productId='.$productId.' --env="'.$environment.'" > /dev/null 2>/dev/null &';
        	shell_exec($cmd);
        	unset($cmd);

        }
        $output->writeln("Synchronous Product Id: " . $productId);
        $time_taken = microtime(true) - $startAt;
        $output->writeln("Processing time: " . sprintf("%.4f", ($time_taken)) . " seconds");
        $output->writeln("Memory Usage - " . number_format((memory_get_peak_usage() / 1048576),2) . " MB");
        
    }
    
}
