<?php

namespace App\Command;

use App\Service\AwardeeManager;
use App\Service\JudgeManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class MigrateImages extends Command
{
    /** @var AwardeeManager */
    private $awardeeManager;

    /** @var JudgeManager */
    private $judgeManager;

    public function __construct(AwardeeManager $awardeeManager, JudgeManager $judgeManager)
    {
        $this->awardeeManager = $awardeeManager;
        $this->judgeManager = $judgeManager;
        parent::__construct();
    }

    protected function configure()
    {
        $this->setName('image:migrate')
            ->setDescription('Migrate images from old format to new');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->write("Migrating awardee images ...");
        $awardees = $this->awardeeManager->getAwardees();
        $this->awardeeManager->ensureImageDirectoryExists();
        foreach ($awardees as $awardee) {
            if ($awardee->hasLegacyProfileImage() && !$awardee->hasProfileImage()) {
                link(
                  sprintf('public/img/awardees/%s.jpg', $awardee->getSlug()),
                  sprintf('%s/%s-profile.jpg',AwardeeManager::IMAGE_DIRECTORY, $awardee->getId())
                );
            }
            if ($awardee->hasLegacyAwardImage() && !$awardee->hasAwardImage()) {
                link(
                  sprintf('public/img/awardees/%s-atadas.jpg', $awardee->getSlug()),
                  sprintf('%s/%s-award.jpg',AwardeeManager::IMAGE_DIRECTORY, $awardee->getId())
                );
            }
        }
        $output->writeln("done.");

        $output->write("Migrating judge images ...");
        $judges = $this->judgeManager->getJudges();
        $this->judgeManager->ensureImageDirectoryExists();
        foreach ($judges as $judge) {
            if ($judge->hasLegacyProfileImage() && !$judge->hasProfileImage()) {
                link(
                  sprintf('public/img/judges/%s.jpg', $judge->getSlug()),
                  sprintf('%s/%s.jpg',JudgeManager::IMAGE_DIRECTORY, $judge->getId())
                );
            }
        }
        $output->writeln("done.");
    }
}
