<?php
namespace AppBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Console\Input\InputOption;

class SendPushCommand extends ContainerAwareCommand
{
    private const defaultEmail = 'thomas.berrod@e-declic.com';


    protected function configure()
    {
        $this
            ->setName('app:push:send')
            ->setDescription('Send a push to Users by email, profile completeness, keywords matching.')
            ->setHelp(' ! Your user must have subscribed to push\n'.
                      '   unless option -t, enter a .json file discribing your intent in the push dir.\n'.
                      '   You can specify keywords OR profile completeness\n'.
                      '   pushType can be "keyword" or "profile" or "general" or "users"\n'
                      )
            ->addArgument('email', InputArgument::OPTIONAL, 'The email of the user.')

            ->addOption(
                'filename',
                'f',
                InputOption::VALUE_OPTIONAL,
                'Name of file in the push directory "filename.json"',
                'coriolis.json'
            )
            ->addOption(
                'test',
                't',
                InputOption::VALUE_NONE,
                'Send a test to a defaultEmail '
            )
            ->addOption(
                'all',
                'a',
                InputOption::VALUE_NONE,
                'Send push to all subscribed users'
            );
        }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // email pour test en dev
        $email = $input->getArgument('email');
        if ($email == null) {
            $email = self::defaultEmail;
        }
        $filename = $input->getOption('filename');
        $testOption = $input->getOption('test');
        $globalOption = $input->getOption('all');

        // $style = new OutputFormatterStyle('red', 'yellow', array('bold', 'blink'));
        // $output->getFormatter()->setStyle('fire', $style);
        $output->writeln([
            'Push Sender',
            '============',
            $this->getContainer()->getParameter('kernel.root_dir'),
            '============', 
            
        ]);
        $notificationManager = $this->getContainer()->get('app.notification_sender') ; //$this->getContainer()->get('app.product_manager');
//lpio
        if($testOption !== false)
        {

            $output->writeln('sending a test probe to user (no config used) ---> '.$email );
            $notificationManager->sendTestNotification($email, $filename, $output);
        }
        else if ($globalOption !== false)
        {
             //send globaly
            $output->writeln('sending to all subscriptions');
            $notificationManager->sendPushToAllSubscribedUsers($filename, $output);

        }
        else
        {
            $output->writeln('sending file description '.$filename.' to user ---> '.$email );
            $output->writeln('Executing NotificationSender  test script');
            $notificationManager->sendSingleNotification($email, $filename, $output);
        }


    }
}
