<?php

namespace App\EntityListener;

use App\Entity\Campaign;
use App\Integrations\Semrush\Client;
use App\Integrations\Semrush\Requests\CreateCampaignRequest;
use App\Integrations\Semrush\Requests\DeleteCampaignRequest;
use App\Integrations\Semrush\Requests\UpdateCampaignRequest;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelInterface;

final class SemrushCampaignListener
{
    /**
     * @var Client
     */
    private $client;
    /**
     * @var CreateCampaignRequest
     */
    private $request;
    /**
     * @var ContainerInterface
     */
    private $container;
    /**
     * @var KernelInterface
     */
    private $kernel;
    /**
     * @var DeleteCampaignRequest
     */
    private $deleteCampaignRequest;

    /**
     * @var bool
     */
    private $dirty = false;
    /**
     * @var UpdateCampaignRequest
     */
    private $updateCampaignRequest;

    public function __construct(
        Client $client,
        CreateCampaignRequest $request,
        DeleteCampaignRequest $deleteCampaignRequest,
        UpdateCampaignRequest $updateCampaignRequest,
        ContainerInterface $container,
        KernelInterface $kernel)
    {
        $this->client = $client;
        $this->request = $request;
        $this->container = $container;
        $this->kernel = $kernel;
        $this->deleteCampaignRequest = $deleteCampaignRequest;
        $this->updateCampaignRequest = $updateCampaignRequest;
    }

    public function postPersist(Campaign $campaign)
    {
        if (null != $campaign->getSemrushId()) {
            $application = new Application($this->kernel);
            $application->setAutoExit(false);

            $input = new ArrayInput([
                'command' => 'crm:semrush:report',
                'campaign' => $campaign->getId(),
            ]);

            $output = new BufferedOutput();
            $application->run($input, $output);
        }
    }

    public function postRemove(Campaign $campaign)
    {
        if ($campaign->getSemrushId()) {
            $this->deleteCampaignRequest->setProjectId($campaign->getSemrushId());

            $this->client->execute($this->deleteCampaignRequest);
        }
    }

    public function postUpdate(Campaign $campaign)
    {
        if ($this->dirty) {
            $this->updateCampaignRequest
                ->setProjectId($campaign->getSemrushId())
                ->setProjectName($campaign->getTitle())
                ->setProjectUrl($campaign->getDomain()->getUrl());

            $this->client->execute($this->updateCampaignRequest);
        }
        $this->dirty = false;
    }

    public function preUpdate(Campaign $campaign, LifecycleEventArgs $lifecycleEventArgs)
    {
        if ($campaign->getSemrushId()) {
            if (in_array('domain', array_keys($lifecycleEventArgs->getEntityChangeSet())) ||
                in_array('title', array_keys($lifecycleEventArgs->getEntityChangeSet()))) {
                $this->dirty = true;
            }
        }
    }
}
