<?php
/**
* @author OfficeBrain 4160 <info@officebrain.com>
*
* Description : Create Queue Command to add in queue of MongoDB
*
**/

namespace OfficeBrain\Bundle\QueueBundle\Command;

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

class CreateQueueCommand extends ContainerAwareCommand
{
    CONST COMMAND_NAME = 'office_brain_queue:queue:import:product';
    CONST RECORDS_LIMIT = 10;
    CONST SEPARATOR = "|----";
    
    /**
     * @var Stopwatch
     */
    private $stopWatch;
    private $container;
    
    /**
    * @author Employee Id: 4160
    * Description :
    * command name and add arguments for command
    *
    * @return type
    *
    **/
    protected function configure()
    {
        $this
            ->setName(self::COMMAND_NAME)
            ->setDescription('Import products in product Queue')
            ->addOption('supplierId', null, InputOption::VALUE_REQUIRED, 'Id of Supplier')
            ->addOption('culture', null, InputOption::VALUE_REQUIRED, 'culture')
            ->addOption('limit', null, InputOption::VALUE_REQUIRED, 'Limit of records');
    }
    
    public function init()
    {
        $this->container = $this->getContainer();
        $this->stopWatch = new Stopwatch();
    }

    /**
    * @author Employee Id: 4160
    * Description :
    * Execute command to add data in queue
    *
    * @return type
    *
    **/
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->init();
        $this->stopWatch->start(self::COMMAND_NAME);
        
        $supplierId = (int)$input->getOption('supplierId');
        $culture = $input->getOption('culture');
        $limit = $input->getOption('limit');
        
        if (!is_int($supplierId)) {
            $output->writeln('Please Provide Valid SupplierId');
            return;
        }
        
        if ($limit > 50) {
            $limit = 50;
            $output->writeln('Limit of records can not be more than 50 so limit is '. $limit);
        }
        
        $request = $this->container->get('request');
        $request->attributes->set('object', 'Product');
        
        $queueApiManager = $this->container->get('office_brain_queue_api_manager');
        $productApiManager = $this->container->get('office_brain_product_admin_manager');
		
        $productResult = $productApiManager->getProductsBySupplierId($supplierId, $culture, 0, $limit);
        $productCount = $productResult['count'];
        if ($productCount == 0) {
            $output->writeln('No Record Found To import For Supplier - ' . $supplierId);
            return;
        }
        foreach($productResult['products'] as $products) {
            $queueApiManager->mapQueue('create', $products);
        }
        if ($limit > $productCount) {
            $limit = $productCount;
        }
        $start = $limit + 1;
        $steps = 50;
        $productRange = range($start, $productCount + $steps, $steps);
        
        foreach($productRange as $index) {
            $productResult = $productApiManager->getProductsBySupplierId($supplierId, $culture, $index, $steps);
            foreach($productResult['products'] as $products) {
                $queueApiManager->mapQueue('create', $products);
            }
        }
        echo "Imported product's records in product queue successfully...";
        $event = $this->stopWatch->stop(self::COMMAND_NAME);
        $output->writeln(" | Memory Usage :- " . $event->getMemory());
        $output->writeln(" | Total Time Taken :- " . $event->getDuration());
    }
}
