<?php

namespace App\Command;

use App\Entity\Campaign\PageSpeedCheck;
use App\Integrations\Google\PageSpeed\GuzzleHandler;
use App\Integrations\Google\PageSpeed\MobilePageSpeedRequest;
use App\Integrations\Google\PageSpeed\PageSpeedRequest;
use App\Service\Campaign\PageSpeedCheckService;
use App\Service\CampaignService;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class CrmCheckPageSpeedCommand extends Command
{
    protected static $defaultName = 'crm:check:speed';
    /**
     * @var CampaignService
     */
    private $campaignService;
    /**
     * @var GuzzleHandler
     */
    private $handler;
    /**
     * @var PageSpeedCheckService
     */
    private $pageSpeedCheckService;

    public function __construct(GuzzleHandler $handler, CampaignService $campaignService, PageSpeedCheckService $pageSpeedCheckService,
                                string $name = null)
    {
        parent::__construct($name);
        $this->campaignService = $campaignService;
        $this->handler = $handler;
        $this->pageSpeedCheckService = $pageSpeedCheckService;
    }

    protected function configure()
    {
        $this
            ->setDescription('Check Google PageSpeed')
            ->addArgument('campaignId', InputArgument::REQUIRED, 'Id of a campaign to check');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);
        $campaignId = $input->getArgument('campaignId');

        try {
            $campaign = $this->campaignService->getCampaign($campaignId);
            $landingPages = $campaign->getLandingPages();
        } catch (EntityNotFoundException $e) {
            $io->error('There is no Campaign for given ID');

            return 1;
        }

        foreach ($landingPages as $landingPage) {
            $io->note(sprintf('Checking PageSpeed for : %s', $landingPage->getUrl()));
            $mobileRequest = MobilePageSpeedRequest::fromUrl($landingPage->getUrl());
            $desktopRequest = PageSpeedRequest::fromUrl($landingPage->getUrl());

            $response = $this->handler->makeRequest($mobileRequest);
            $mobileResult = json_decode($response);
            $mobileScore = $mobileResult->lighthouseResult->categories->performance->score;

            $response = $this->handler->makeRequest($desktopRequest);
            $desktopResult = json_decode($response);
            $desktopScore = $desktopResult->lighthouseResult->categories->performance->score;

            $check = new PageSpeedCheck();
            $check->setCampaign($campaign);
            $check->setDesktopScore($desktopScore * 100);
            $check->setDesktopResult(json_encode($desktopResult));
            $check->setMobileScore($mobileScore * 100);
            $check->setMobileResult(json_encode($mobileResult));

            $this->pageSpeedCheckService->save($check);
        }

        $io->success('Job finished');

        return 0;
    }
}
