<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Mac;
use Doctrine\DBAL\Exception\ConstraintViolationException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

/**
 * Mac controller.
 *
 * @Route("/7073a00e724bbda643cb7e2a85110640/home/mac")
 */
class MacController extends Controller
{
    /**
     * Lists all mac entities.
     *
     * @Route("/", name="mac_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $macs = $em->getRepository('AppBundle:Mac')->findAll();

        return $this->render('mac/index.html.twig', array(
            'macs' => $macs,
        ));
    }

    /**
     * Creates a new mac entity.
     *
     * @Route("/new", name="mac_new")
     * @Method({"GET", "POST"})
     */
    public function newAction(Request $request)
    {
        $mac = new Mac();
        $form = $this->createForm('AppBundle\Form\MacType', $mac);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            try {
                $em = $this->getDoctrine()->getManager();
                $mac->setValue($request->request->get("appbundle_mac")["value"]);
                $em->persist($mac);
                $em->flush($mac);
                return $this->redirectToRoute('mac_index', array('id' => $mac->getId()));
            } catch (ConstraintViolationException $e) {
                $this->addFlash("error", "Mac adresi veya mail adresi daha önce kullanılmış");
            } catch (\Exception $e) {
                $this->addFlash("error", "Beklenmeyen bir hata oluştu");
                $this->get('logger')->error($e->getMessage());
            }
        }

        return $this->render('mac/new.html.twig', array(
            'mac' => $mac,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a mac entity.
     *
     * @Route("/{id}", name="mac_show")
     * @Method("GET")
     */
    public function showAction(Mac $mac)
    {
        $deleteForm = $this->createDeleteForm($mac);

        return $this->render('mac/show.html.twig', array(
            'mac' => $mac,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing mac entity.
     *
     * @Route("/{id}/edit", name="mac_edit")
     * @Method({"GET", "POST"})
     */
    public function editAction(Request $request, Mac $mac)
    {
        $deleteForm = $this->createDeleteForm($mac);
        $editForm = $this->createForm('AppBundle\Form\MacType', $mac);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            try {
                $em = $this->getDoctrine()->getManager();
                $em->persist($mac);
                $em->flush();
            }catch (ConstraintViolationException $e) {
                $this->addFlash("error", "Mac adresi veya mail adresi daha önce kullanılmış");
            } catch (\Exception $e) {
                $this->addFlash("error", "Beklenmeyen bir hata oluştu");
                $this->get('logger')->error($e->getMessage());
            }
            return $this->redirectToRoute('mac_edit', array('id' => $mac->getId()));
        }

        return $this->render('mac/edit.html.twig', array(
            'mac' => $mac,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a mac entity.
     *
     * @Route("/{id}", name="mac_delete")
     * @Method("DELETE")
     */
    public function deleteAction(Request $request, Mac $mac)
    {
        $form = $this->createDeleteForm($mac);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->remove($mac);
            $em->flush($mac);
        }

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

    /**
     * Creates a form to delete a mac entity.
     *
     * @param Mac $mac The mac entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm(Mac $mac)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('mac_delete', array('id' => $mac->getId())))
            ->setMethod('DELETE')
            ->getForm();
    }
}
