<?php

namespace Schatz\CrmBundle\Controller\Leads;

use Schatz\CrmBundle\Constants\ContactStatusConstants;
use Schatz\CrmBundle\Constants\ContactTypeConstants;
use Schatz\GeneralBundle\Constants\PermissionsConstants;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class LeadsController extends Controller
{
    const MENU = 'leads';

    private $leads = array();
    private $user;
    private $allCampaigns = array();
    private $myCampaigns = array();
    private $contactCampaigns = array();
    private $allAssignedLeadsOrContacts = array();

    public function indexAction($filter)
    {
        if ($this->get('user.permission')->checkPermission(PermissionsConstants::PERMISSION_7) and !$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) return $this->redirect($this->generateUrl('schatz_general_homepage'));

        $this->user = $this->getUser();

        if (!$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {

            $this->allCampaigns = $this->getDoctrine()->getRepository('SchatzCrmBundle:Campaign')->findAll();

            if (!empty($this->allCampaigns)) {

                foreach ($this->allCampaigns as $campaign) {

                    if (in_array($this->user->getId(), $campaign->getAssociates())) {

                        $this->myCampaigns[] = $campaign;
                    }
                }
            }

            $this->contactCampaigns = $this->getDoctrine()->getRepository('SchatzCrmBundle:ContactCampaign')->findAll();

            if (!empty($this->contactCampaigns)) {

                foreach ($this->contactCampaigns as $contactCampaign) {

                    if ($contactCampaign->getContact()->getFlag() == ContactTypeConstants::CONTACT_TYPE_2 and !in_array($contactCampaign->getContact(), $this->allAssignedLeadsOrContacts)) {

                        $this->allAssignedLeadsOrContacts[] = $contactCampaign->getContact();
                    }
                }
            }
        }

        $this->_getLeads($filter);

        return $this->render('SchatzCrmBundle:Leads:index.html.twig',
            array(
                'menu' => $this::MENU,
                'leads' => $this->leads,
                'filter' => $filter
            )
        );
    }

    private function _getLeads($filter)
    {
        if (empty($this->myCampaigns) and empty($this->allAssignedLeadsOrContacts) and !$this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {

            $this->leads = array();

        } else {

            if ($filter == 'my_leads') {

                $this->leads = $this->getDoctrine()->getRepository('SchatzCrmBundle:Contact')->findAllLeadsOrContacts(ContactTypeConstants::CONTACT_TYPE_2, $this->myCampaigns, $this->user, $this->allAssignedLeadsOrContacts, null, $this->user);

            } else if ($filter == 'new') {

                $this->leads = $this->getDoctrine()->getRepository('SchatzCrmBundle:Contact')->findAllLeadsOrContacts(ContactTypeConstants::CONTACT_TYPE_2, $this->myCampaigns, $this->user, $this->allAssignedLeadsOrContacts, ContactStatusConstants::CONTACT_STATUS_1);

            } else if ($filter == 'contacted') {

                $this->leads = $this->getDoctrine()->getRepository('SchatzCrmBundle:Contact')->findAllLeadsOrContacts(ContactTypeConstants::CONTACT_TYPE_2, $this->myCampaigns, $this->user, $this->allAssignedLeadsOrContacts, ContactStatusConstants::CONTACT_STATUS_2);

            } else if ($filter == 'rejected') {

                $this->leads = $this->getDoctrine()->getRepository('SchatzCrmBundle:Contact')->findAllLeadsOrContacts(ContactTypeConstants::CONTACT_TYPE_2, $this->myCampaigns, $this->user, $this->allAssignedLeadsOrContacts, ContactStatusConstants::CONTACT_STATUS_3);

            } else {

                $this->leads = $this->getDoctrine()->getRepository('SchatzCrmBundle:Contact')->findAllLeadsOrContacts(ContactTypeConstants::CONTACT_TYPE_2, $this->myCampaigns, $this->user, $this->allAssignedLeadsOrContacts);
            }
        }
    }
}
