<?php

namespace OfficeBrain\Bundle\CmsBundle\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 Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * Sync Subshop Product From Product Criteria Command
 *
 * @author OfficeBrain 4494 <info@officebrain.com>
 */
class CmsMenuCloneByInstanceIdCommand extends ContainerAwareCommand
{
    CONST COMMAND_NAME = 'office_brain_cms:menu-clone-by-instance-id';

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName(self::COMMAND_NAME)
            ->setDescription('Command to sync subshop product data')
            ->setDefinition(array(
            	new InputOption('menuSlug', null, InputOption::VALUE_REQUIRED, 'Enter cms menu slug'),
                new InputOption('sourceInstanceId', null, InputOption::VALUE_REQUIRED, 'Enter source instance id'),
            	new InputOption('targetInstanceId', null, InputOption::VALUE_REQUIRED, 'Enter target instance id'),
            ))
            ->setHelp(<<<EOT
The <info>office_brain_cms:menu-clone-by-instance-id</info> command helps you to clone menu item by instance id.
EOT
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
    	$productIds = array();
    	$startAt = microtime(true);
    	$this->container = $this->getContainer();

    	$menuSlug = $input->getOption('menuSlug', null);
    	$sourceInstanceId = $input->getOption('sourceInstanceId', null);
    	$targetInstanceId = $input->getOption('targetInstanceId', null);

    	if ($menuSlug == '')
    	{
    		$output->writeln("Please provide a cms menu slug !");
    		return;
    	}

    	if ($sourceInstanceId == '')
    	{
    		$output->writeln("Please provide a source instance id !");
    		return;
    	}

    	if ($targetInstanceId == '')
    	{
    		$output->writeln("Please provide a target instance id !");
    		return;
    	}

    	$this->cmsMenuManager = $this->container->get('office_brain_cms_menu_manager');

    	$cmsMenuList = $this->cmsMenuManager->cmsMenuMasterRepository->findBySlugWithInstanceId($menuSlug, $sourceInstanceId);

    	foreach($cmsMenuList as $cmsMenu) {
    		$targetcmsMenu = $this->cmsMenuManager->cmsMenuMasterRepository->findBySlugWithCountryIdWithInstanceId($menuSlug, $cmsMenu->getCountryId(), $targetInstanceId);

    		if (count($targetcmsMenu) == 1) {
    			$targetcmsMenuItem = $targetcmsMenu->getMenuItemMapping();
    			$sourcecmsMenuItem = $cmsMenu->getMenuItemMapping();

    			if (count($sourcecmsMenuItem) != count($targetcmsMenuItem)) {

    				if (count($targetcmsMenuItem) > 0) {
    					//delete Old menu Item
    					$output->writeln("Delete Menu Item :- Instance Id: ".$targetInstanceId." Slug: ".$menuSlug. " Country Id: ".$targetcmsMenu->getCountryId()." Menu Item Count: ".count($targetcmsMenuItem). " Menu Id: ".$targetcmsMenu->getId());
    					
    					foreach($targetcmsMenuItem as $targetItem) {
    						$this->cmsMenuManager->deleteMenuItem($targetItem->getId(), false);
    					}	
    				}

    				//clone new menu item
    				$cloneMenuItems = $this->cmsMenuManager->cloneMenuItems($targetcmsMenu, $sourcecmsMenuItem);
    				$output->writeln("Added Menu Item :- Instance Id: ".$targetInstanceId." Slug: ".$menuSlug. " Country Id: ".$targetcmsMenu->getCountryId()." Menu Item Count: ".$cloneMenuItems['menu_items']. " Menu Id: ".$cloneMenuItems['menu_id']);
    			}
    		}
    	}
	}
}