<?php

namespace AppBundle\Controller\Admin;

use AppBundle\Entity\Answer;
use AppBundle\Entity\Ad;
use AppBundle\Form\Admin\AdType;
use AppBundle\Form\Admin\ModerationDeclineType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

class AnswerController extends Controller
{

    /***********************
     * MODERATION
     ***********************/

    /**
     * @Route("/admin/moderation/anwser/list", name="admin_anwser_moderation_list")
     */
    public function adminAnswerModerationListAction()
    {
        $em = $this->getDoctrine()->getManager();
        $answers = $em->getRepository('AppBundle:Answer')->findAll();

        return $this->render('admin/answer_list_moderation.html.twig', array(
            'answers' => $answers,
        ));
    }

    /**
     * @Route("/admin/config/answer/{id}/status/{status}", name="admin_answer_status")
     */
    public function adminStatusAnswerAction(Answer $answer, $status)
    {
        $em = $this->getDoctrine()->getManager();
        $answer->setStatus($status);
        $em->persist($answer);
        $em->flush();
        $result = array("msg" => "toggle ok");
        return new JsonResponse($result);

    }

    /**
     * @Route("/admin/config/answer/{id}/delete", name="admin_answer_delete")
     */
    public function adminAnswerDeleteAction(Answer $answer)
    {
        $em = $this->getDoctrine()->getManager();
        $t = $em->getRepository('AppBundle:User')->findByRole('ROLE_TALENT');
        $media = $answer->getVideo();
        $answer->setVideo(null);
        $em->remove($answer);
        $mediaManager = $this->get('media_manager');
        if($media){
            $mediaManager->delete($media);
            $em->remove($media);
        }
        $em->flush();

        return $this->redirectToRoute('admin_anwser_moderation_list');
    }
}
