<?php

namespace App\Controller;

use App\Entity\Player;
use App\Service\EvaluationService;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class PlayerAdminController extends CRUDController
{
    private $evaluationService;

    public function __construct(EvaluationService $evaluationService)
    {
        $this->evaluationService = $evaluationService;
    }

    public function recalculateAction($id)
    {
        $object = $this->admin->getSubject();

        if (!$object) {
            throw new NotFoundHttpException(sprintf('unable to find the object with id: %s', $id));
        }

        /** @var EvaluationService $this->evaluationService */
        $result = $this->evaluationService->checkAndUpdtePlayerStatus($object);

        if($result) {
            $this->addFlash(
                'success',
                'Re-calculated Successfully.'
            );
        }else {
            $this->addFlash(
                'error',
                'Unable to Re-calculate.'
            );
        }
        return $this->redirect('/admin/app/player/list');
        exit;
    }
}
