<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Charity;
use Endroid\QrCode\QrCode;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * Charity controller.
 *
 * @Route("charity")
 */
class CharityController extends Controller
{
    /**
     * Lists all charity entities.
     *
     * @Route("/", name="charity_index")
     * @Method("GET")
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();

        $charities = $em->getRepository('AppBundle:Charity')->findAll();

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


    /**
     * Lists all charity entities.
     *
     * @Route("/qr/{id}", name="charity_qr")
     * @Method("GET")
     */
    public function qrAction(Charity $charity)
    {
        $path = $this->generateURl('charity_donate',array('id'=>$charity->getId()),UrlGeneratorInterface::ABSOLUTE_URL);

        return $this->render(':charity:qr.html.twig',array(
            'charity'=>$charity,
            'url_path'=>$path
        ));
    }


    /**
     * Creates a new charity entity.
     *
     * @Route("/donate/{id}", name="charity_donate")
     * @Method({"GET", "POST"})
     */
    public function donateAction(Request $request,Charity $charity)
    {
        $em = $this->getDoctrine()->getManager();
        $amount = $request->request->get('amount');

        if($request->isMethod('post')){
            $balance = $charity->getBalance() + $amount;
            $charity->setBalance($balance);
            $em->persist($charity);
            $em->flush();

            return $this->redirectToRoute('charity_thanks');

        }

        return $this->render(':charity:donate.html.twig',array(
            'id'=>$charity->getId()
        ));
    }

    /**
     * @Route("/download/qr/{id}", name="charity_download_qr")
     * @Method("GET")
     *
     */
    public function downloadAction(Request $request,Charity $charity)
    {
//        var_dump($request->request->all());




        $url = $this->generateUrl('charity_donate',array('id'=>$charity->getId()),UrlGeneratorInterface::ABSOLUTE_URL);
        $qrCode = new QrCode();
        $qrCode
            ->setText($url)
            ->setSize(300)
            ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
            ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
            ->setLabel($charity->getName())
            ->setLabelFontSize(16)

        ;

        // now we can directly output the qrcode
        header('Content-Type: '.$qrCode->getContentType());


        // or create a response object
        $response = new Response($qrCode->writeString(), 200, array('Content-Type' => $qrCode->getContentType()));


        $disposition = $response->headers->makeDisposition(
            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
            $charity->getName().".png"
        );

        $response->headers->set('Content-Disposition', $disposition);

        return $response;

    }

    /**
     * Lists all charity entities.
     *
     * @Route("/thanks", name="charity_thanks")
     * @Method("GET")
     */
    public function thanksAction()
    {
        return $this->render(':charity:thanks.html.twig');
    }

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

        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($charity);
            $em->flush();

            return $this->redirectToRoute('charity_qr', array('id' => $charity->getId()));
        }

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

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

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

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

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $this->getDoctrine()->getManager()->flush();

            return $this->redirectToRoute('charity_edit', array('id' => $charity->getId()));
        }

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

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

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

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

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