<?php

namespace OfficeBrain\Bundle\CategoryBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use OfficeBrain\Bundle\SolrBundle\Helper\ProductHelper;
use OfficeBrain\Bundle\SearchBundle\Form\Data\ProductSearchData;
use OfficeBrain\Bundle\SearchBundle\Form\Type\ProductSearchType;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Form\FormError;

/**
 * Sync product Category data command
 *
 * @author OfficeBrain 4220 <info@officebrain.com>
 */
class UpdateCategoryStatusAndSyncProductCommand extends ContainerAwareCommand
{
    CONST COMMAND_NAME = 'office_brain:solr:sync:update-category-sync-product';
    //CONST RECORD_PER_PAGE = 100;
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName(self::COMMAND_NAME)
            ->setDescription('Command to sync category product data after update status')
        	->addOption('page', null, InputOption::VALUE_OPTIONAL, 'page no')
            ->addOption('action', null, InputOption::VALUE_OPTIONAL, 'Action is update/delete')
        	->addOption('categoryId', null, InputOption::VALUE_OPTIONAL, 'Valid Category id that is pms id of category. ')
            ->addOption('recordPerPage', null, InputOption::VALUE_OPTIONAL, 'Valid recordPerPage. ');
            
            
    }
    
    public function init() {
    	$this->container = $this->getContainer();
    	//$this->request = $this->container->get('request');
    	//$this->projectSetting = $this->request->get('project_setting');   	
    }
    

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
    	$this->init();
    	$startAt = microtime(true);
    	$pmsId= $input->getOption('categoryId','');
    	$page = $input->getOption('page','');
        $recordPerPage = $input->getOption('recordPerPage','');
        $action = $input->getOption('action','');
       
    	if($page==""){
            $page=1;
        }
        
        if($action==""){
            $output->writeln("Please provide a action (update or delete)");
            return;
        }

        if($recordPerPage==""){
            $output->writeln("Please provide a recordsPerPage");
            return;
        }

    	if ($pmsId == '') {
    		$output->writeln("Please provide a categoryId");
    		return;
    	}

    	$api_manager = $this->container->get('officebrain_category_api_manager');
        $this->productApiManager = $this->container->get('office_brain_product_admin_manager');
        //$recordsPerPage = self::RECORD_PER_PAGE;
        $offset = ($page - 1) * $recordPerPage;
        // Limit is same as records per page
        $limit = $recordPerPage;
        $categoryMappingData = $api_manager->getProductMappingDataFromApi($pmsId,$offset,$limit);
        if(count($categoryMappingData)>0){
            //$page = 1;

           $env = $this->container->getParameter("kernel.environment");
           $localization_to_update = $this->container->getParameter('localization_to_fetch');
           $output->writeln('Start batch ====='.$page.'========');

           $products = array_column($categoryMappingData, 'product_id');
           $productStr = implode(',',$products);
          
           /*foreach($categoryMappingData as $value){

            $productId= $value['product_id'];
            if($action=="update"){
                $command = $this->updateInSolr($productId);
            }
            if($action=="delete"){
                $command = $this->deleteFromSolr($productId);
            }
            
            shell_exec($command);
            $output->writeln($action.' product data '.$productId.' sync in solr');
            }*/

            /*if($action=="update"){
                $command = $this->updateInSolr($productStr);
            }*/
            if($action=="delete"){
                $command = $this->deleteFromSolr($productStr);
            }
            echo $command;
            shell_exec($command);
            $output->writeln($action.' product data '.$productStr.' sync in solr');
            sleep(5);
            $page = $page+1;

            $command = '/usr/bin/php ' . $this->getContainer()->get('kernel')->getRootDir() . '/console ' . self::COMMAND_NAME . ' --categoryId='.$pmsId. ' --page=' . $page.' --recordPerPage='.$recordPerPage.'  --action="'.$action.'"';

            //$command .= ' --no-debug';


            passthru($command);
        }else
        {
            $output->writeln('No more product data found for category  '.$pmsId);
            return;
        }
        
    }


    protected function updateInSolr($productId){
        $culture = 'en_us';
        $projectId = 51;
        $command = '/usr/bin/php ' . $this->getContainer()->get('kernel')->getRootDir() . '/console office_brain:solr:sync:product --projectId='.$projectId.' --productIds="'.$productId.'" --culture="'.$culture.'"';
        return $command;
        
    }

    protected function deleteFromSolr($productId){
        $culture = 'en_us';
        $projectId = 51;
        $command = '/usr/bin/php ' . $this->getContainer()->get('kernel')->getRootDir() . '/console office_brain:solr:sync:delete-product --projectId='.$projectId.' --productId="'.$productId.'" --culture="'.$culture.'"';
        return $command;
    }
}