<?php

namespace OfficeBrain\Bundle\EmailBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use OfficeBrain\Bundle\UserBundle\Entity\User;

/**
 * Email Data Clone By Instance ID command
 *
 * @author OfficeBrain 4494 <info@officebrain.com>
 */
class EmailCloneByInstanceIdCommand extends ContainerAwareCommand
{
	CONST COMMAND_NAME = 'office_brain_email:clone:email-by-instance-id';

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName(self::COMMAND_NAME)
            ->setDescription('Command to clone email data by instance id')
            ->setDefinition(array(
            	new InputOption('sourceInstanceId', null, InputOption::VALUE_REQUIRED, 'Enter source instance id'),
            	new InputOption('targetInstanceId', null, InputOption::VALUE_REQUIRED, 'Enter target instance id'),
            	new InputOption('sourceInstanceType', null, InputOption::VALUE_REQUIRED, 'Enter source instance type'),
            	new InputOption('targetInstanceType', null, InputOption::VALUE_REQUIRED, 'Enter target instance type'),
            ))
            ->setHelp(<<<EOT
				The <info>office_brain_email:clone:email-by-instance-id</info> Migration Script For Clone Email Data By Instance Id
EOT
			);
    }
    
    public function init()
    {
        $this->emailEventManager = $this->getContainer()->get('office_brain_email_event_manager');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->init();

        $startAt = microtime(true);
        $this->container = $this->getContainer();

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

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

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

        if ($sourceInstanceType == '') {
        	$sourceInstanceType = 'association';
        }

        if ($targetInstanceType == '') {
        	$targetInstanceType = 'distributor';
        }

        $targetInstanceIdExplode = array_unique(explode(',', $targetInstanceId));

        $sourceEmailEvents = $this->emailEventManager->getEventList($sourceInstanceId, $sourceInstanceType);

        $output->writeln("Source Email Data Records : ".count($sourceEmailEvents));

        foreach($targetInstanceIdExplode as $targetInstanceId) {
        	$targetEmailEvents = $this->emailEventManager->getEventList($targetInstanceId, $targetInstanceType);

        	if (count($targetEmailEvents) == 0) {
        		$output->writeln("Email Data Importing for Instance Id : ".$targetInstanceId);

        		$this->emailEventManager->cloneEmailData($sourceEmailEvents, $targetInstanceId, $targetInstanceType);
        	} else {
        		$output->writeln("Email Data Imported for Instance Id : ".$targetInstanceId);
        	}
        }
    }
}