<?php

namespace AppBundle\Controller\Admin;

use AppBundle\Entity\Company;
use AppBundle\Entity\TerritoryAd;
use AppBundle\Form\Admin\TerritoryAdminType;
use AppBundle\Form\Admin\TerritoryAdType;
use AppBundle\Form\Admin\TerritoryCompanyRemoveType;
use AppBundle\Form\Admin\TerritoryCompanyType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class TerritoryController extends Controller
{

    /**
     * @Route("/admin/config/territory/list", name="admin_config_territory")
     */
    public function adminTerritoryListAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $territories = $em->getRepository('AppBundle:Company')->findByType("TERRITORY");

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

    /**
     * @Route("/admin/config/territory/{id}/edit", name="admin_territory_edit")
     */
    public function adminTerritoryEditAction(Request $request, Company $territory)
    {
        $form = $this->createForm(TerritoryAdminType::class, $territory);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $type = $form['terType']->getData();
            $arrayExtra = $territory->getExtra();
            if (is_array($arrayExtra)) {
                $arrayExtra['terType'] = $type;
            } else {
                $arrayExtra = array('terType' => $type);
            }
            $territory->setExtra($arrayExtra);
            $companyManager = $this->get('app.company_manager');
            $companyManager->updateCompany($territory);
            return $this->redirectToRoute('admin_config_territory');
        }
        return $this->render('admin/territory_edit.html.twig', array(
            'form' => $form->createView(),
            'territory' => $territory,
        ));
    }

    /**
     * @Route("/admin/config/territory/{id}/link", name="admin_territory_link")
     */
    public function adminTerritoryLinkAction(Request $request, Company $territory)
    {
        $em = $this->getDoctrine()->getManager();
        $form = $this->createForm(TerritoryAdType::class);
        $form->handleRequest($request);
        if ($form->isValid()) {
            $ads = $form['territoryAds']->getData();
            foreach ($ads as $a) {
                $ta = new TerritoryAd($territory, $a);
                $territory->addTerritoryAd($ta);
            }
            $em->persist($territory);
            $em->flush();
            return $this->redirect($this->generateUrl('admin_territory_link', array('id' => $territory->getId())));
        }
        $territoryAds = $em->getRepository('AppBundle:TerritoryAd')->getTerritoryAds($territory->getId());

        return $this->render('admin/territory_link_ad.html.twig', array(
            'form' => $form->createView(),
            'territory' => $territory,
            'territoryAds' => $territoryAds,
        ));
    }

    /**
     * @Route("/admin/config/territory/{id}/link-company", name="admin_territory_link_company")
     */
    public function adminTerritoryLinkCompanyAction(Request $request, Company $territory)
    {
        $em = $this->getDoctrine()->getManager();
        $form = $this->createForm(TerritoryCompanyType::class);
        $form->handleRequest($request);
        if ($form->isValid()) {
            $companies = $form['territoryCompanies']->getData();
            foreach ($companies as $c) {
                $tc = new TerritoryAd($territory, null, $c);
                $territory->addTerritoryAd($tc);
            }
            $em->persist($territory);
            $em->flush();
            return $this->redirect($this->generateUrl('admin_territory_link_company', array('id' => $territory->getId())));
        }
//        $territoryCompanies = $em->getRepository('AppBundle:TerritoryAd')->getTerritoryCompanies($territory->getId());

        $form2 = $this->createForm(TerritoryCompanyRemoveType::class, null, ['id' => $territory->getId()]);
        $form2->handleRequest($request);
        if ($form2->isValid()) {
            $territoryCompanies = $form2['territoryCompanies']->getData();
            foreach ($territoryCompanies as $tc) {
                $em->remove($tc);
            }
            $em->flush();
            return $this->redirect($this->generateUrl('admin_territory_link_company', array('id' => $territory->getId())));
        }

        return $this->render('admin/territory_link_company.html.twig', array(
            'form' => $form->createView(),
            'form2' => $form2->createView(),
            'territory' => $territory,
//            'territoryCompanies' => $territoryCompanies,
        ));
    }

    /**
     * @Route("/admin/config/territory/unlink/{id}", name="admin_territory_unlink")
     */
    public function adminTerritoryUnlinkAction(TerritoryAd $ta)
    {
        $territory = $ta->getTerritory();
        $em = $this->getDoctrine()->getManager();
        $em->remove($ta);
        $em->flush();
        return $this->redirect($this->generateUrl('admin_territory_link', array('id' => $territory->getId())));
    }
}
