<?php

namespace App\EntityListener;

use App\Entity\Campaign;
use App\Service\CitationFlowChecker;
use App\Service\DomainAuthorityChecker;
use App\Service\PageAuthorityChecker;
use App\Service\StatsProcessor;
use App\Service\TrustFlowChecker;

final class CampaignListener
{
    /**
     * @var PageAuthorityChecker
     */
    private $pageAuthorityChecker;
    /**
     * @var DomainAuthorityChecker
     */
    private $domainAuthorityChecker;
    /**
     * @var StatsProcessor
     */
    private $processor;
    /**
     * @var TrustFlowChecker
     */
    private $trustFlowChecker;
    /**
     * @var CitationFlowChecker
     */
    private $citationFlowChecker;

    public function __construct(PageAuthorityChecker $pageAuthorityChecker, DomainAuthorityChecker $domainAuthorityChecker, TrustFlowChecker $trustFlowChecker, CitationFlowChecker $citationFlowChecker,
                                StatsProcessor $processor)
    {
        $this->pageAuthorityChecker = $pageAuthorityChecker;
        $this->domainAuthorityChecker = $domainAuthorityChecker;
        $this->processor = $processor;
        $this->trustFlowChecker = $trustFlowChecker;
        $this->citationFlowChecker = $citationFlowChecker;
    }

    public function postPersist(Campaign $campaign)
    {
        $domainEntity = $campaign->getDomain();
        $this->processor->setDomain($domainEntity->getUrl());
        $this->processor->setDomainId($domainEntity->getId());

        foreach ($domainEntity->getStats() as $stat) {
            switch ($stat->getStat()->getSymbol()) {
                case 'DA':
                    $this->processor->addChecker($this->domainAuthorityChecker);

                    break;
                case 'PA':
                    $this->processor->addChecker($this->pageAuthorityChecker);

                    break;
                case 'TF':
                    $this->processor->addChecker($this->trustFlowChecker);

                    break;
                case 'CF':
                    $this->processor->addChecker($this->citationFlowChecker);

                    break;
                default:
                    break;
            }
        }

        $this->processor->process();
    }
}
