<?php

namespace OfficeBrain\Bundle\EmailBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Response;

/**
 * @author OfficeBrain 4795 <info@officebrain.com>
 *
 * Description: Manage events of email
 *
 */
class EmailQueueAdminController extends Controller
{
    /**
     * @author Employee Id: 4795
     *
     * Description : Set variables
     *
     * @param 1 : Null
     *
     * @return type : Null
     *
     * @throws Exception: Null
     **/
    public function __construct()
    {
        $this->responseArray = array();
        $this->request = null;
        $this->emailServiceManager = null;
        $this->projectSetting = array();
        $this->translator = null;
        $this->accessRules = array();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Unset variables
     *
     * @param 1 : Null
     *
     * @return type : Null
     *
     * @throws Exception: Null
     **/
    public function __destruct()
    {
        unset($this->responseArray);
        unset($this->request);
        unset($this->emailServiceManager);
        unset($this->projectSetting);
        unset($this->accessRules);
        unset($this->translator);
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Set request and service
     *
     * @param 1 : Null
     *
     * @return type : Null
     *
     * @throws Exception: Null
     **/
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->projectSettings = $this->request->get('project_setting');
        $this->accessRules = $this->projectSettings['access_rule'];
        $this->emailServiceManager = $this->get('office_brain_email_service');
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Return all emails
     *
     * @param 1 : Null
     *
     * @return type : Html Response
     *
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/email/list-email/",
     * name="office_brain_emailbundle_emailqueueadmin_listemail")
     * @Method("GET")
     *
     * @Template("OfficeBrainEmailBundle:EmailQueueAdmin:listEmail.html.twig")
     */
    public function listEmailAction()
    {
        $this->initAction();
        
        $this->responseArray['page'] = $this->request->get('page', '1');
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4795office_brain_emailbundle_emailtemplateheaderadmin_listheader
     *
     * Description : Return all emails
     *
     * @param 1 : Null
     *
     * @return type : Html Response
     *
     * @throws Exception: Null
     *office_brain_emailbundle_emailtemplateheaderadmin_listheader
     * @Route("/admin/{_locale}/email/list-email/",
     * name="office_brain_emailbundle_emailqueueadmin_listemailpost")
     * @Method("POST")
     *
     * @Template("OfficeBrainEmailBundle:EmailQueueAdmin:listEmailAjax.html.twig")
     */
    public function listEmailPostAction()
    {
        $this->initAction();
        
        $page  = $this->request->get('page', '1');
        $recordsPerPage = $this->projectSettings['configuration_rule']['site_management']['option']['email']['option']['email_record_per_page']['status'];
    
        $emails = $this->emailServiceManager->getQueuedEmailList();
    
        $paginator  = $this->get('knp_paginator');
        
        $this->responseArray['pagination'] = $paginator->paginate($emails, $page, $recordsPerPage, array(), count($emails));
        $this->responseArray['page'] = $page;
        
        //unset variable
        unset($page);
        unset($recordsPerPage);
        unset($emails);
        unset($paginator);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Return all emails
     *
     * @param 1 : Null
     *
     * @return type : Html Response
     *
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/email/list-email-ajax/{page}",
     * name="office_brain_emailbundle_emailqueueadmin_listemailajax",
     * requirements={"page"="\d+"},
     * defaults={"page"=1}),
     *
     * @Template("OfficeBrainEmailBundle:EmailQueueAdmin:listEmailAjax.html.twig")
     */
    public function listEmailAjaxAction()
    {
        $this->initAction();
        
        $page  = $this->request->get('page', '1');
        $recordsPerPage = $this->projectSettings['configuration_rule']['site_management']['option']['email']['option']['email_record_per_page']['status'];
    
        $emails = $this->emailServiceManager->getQueuedEmailList();
    
        $paginator  = $this->get('knp_paginator');
        $this->responseArray['page'] = $page;
        $this->responseArray['pagination'] = $paginator->paginate($emails, $page, $recordsPerPage, array(), count($emails));
        
        //unset variable
        unset($page);
        unset($recordsPerPage);
        unset($emails);
        unset($paginator);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Search emails
     *
     * @param 1 : Null
     *
     * @return type : Html Response
     *
     * @throws Exception: Null
     *
     * @Route("/admin/{_locale}/email/search-email-ajax",
     * name="office_brain_emailbundle_emailqueueadmin_searchemailajax")
     *
     * @Template("OfficeBrainEmailBundle:EmailQueueAdmin:listEmailAjax.html.twig")
     */
    public function searchEmailAjaxAction()
    {
        $this->initAction();
       
        $page  = $this->request->get('page', '1');
        $recordsPerPage = $this->projectSettings['configuration_rule']['site_management']['option']['email']['option']['email_record_per_page']['status'];
        
        $dataArr['option'] = $this->request->get('option');
        $dataArr['keyword'] = $this->request->get('keyword');
        $dataArr['sendAt'] = $this->request->get('sendAt');
        $emails = $this->emailServiceManager->searchedEmailList($dataArr);
    
        $paginator  = $this->get('knp_paginator');
        $this->responseArray['page'] = $page;
        $this->responseArray['pagination'] = $paginator->paginate($emails, $page, $recordsPerPage, array(), count($emails));
        
        //unset variable
        unset($page);
        unset($recordsPerPage);
        unset($dataArr);
        unset($emails);
        unset($paginator);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Resend email
     *
     * @param 1 : Null
     *
     * @return type : Redirect to list page
     *
     * @throws Exception: Null
     *
     * @Route("admin/{_locale}/email/release-emails", name="office_brain_emailbundle_emailqueueadmin_releaseemails")
     *
     * @Method("GET")
     */
    public function releaseEmailAction()
    {
        $this->initAction();
        
        $this->emailServiceManager->releaseEmails();
        return new Response("sucess");
       // return $this->redirect($this->generateUrl('office_brain_emailbundle_emailqueueadmin_listemail'));
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Resend email
     *
     * @param 1 : Email Id
     *
     * @return type : Redirect to list page
     *
     * @throws Exception: Null
     *
     * @Route("/admin/email/resend-email/{id}", name="office_brain_emailbundle_emailqueueadmin_resendemail")
     *
     * @Method("GET")
     */
    public function resendEmailAction($id)
    {
        $this->initAction();
        
        $this->emailServiceManager->resendEmail($id);
        
        return $this->redirect($this->generateUrl('office_brain_emailbundle_emailqueueadmin_listemail'));
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Send email
     *
     * @param 1 : Email Id
     *
     * @return type : Redirect to list page
     *
     * @throws Exception: Null
     *
     * @Route("/admin/email/send-email/{id}", name="office_brain_emailbundle_emailqueueadmin_sendemail")
     *
     * @Method("GET")
     */
    public function sendEmailAction($id)
    {
        $this->initAction();
        
        $this->emailServiceManager->sendEmail($id);
        
        return $this->redirect($this->generateUrl('office_brain_emailbundle_emailqueueadmin_listemail'));
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Preview email
     *
     * @param 1 : Email Id
     *
     * @return type : Html Response
     *
     * @throws Exception: Null
     *
     * @Route("/admin/email/preview-email/{id}", name="office_brain_emailbundle_emailqueueadmin_previewemail")
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainEmailBundle:EmailQueueAdmin:previewEmail.html.twig")
     */
    public function previewEmailAction($id)
    {
        $this->initAction();
        
        $this->responseArray = $this->emailServiceManager->previewEmail($id);
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Read email
     *
     * @param 1 : Email Id
     *
     * @return type : Image Response
     *
     * @throws Exception: Null
     *
     * @Route("/email/read-email/{id}", name="office_brain_emailbundle_emailqueueadmin_reademail")
     *
     * @Method("GET")
     */
    public function readEmailAction($id)
    {
        $this->initAction();
        
        $this->emailServiceManager->readEmail($id);
        
        $kernel = $this->get('kernel');
        $file = $kernel->locateResource('@OfficeBrainEmailBundle/Resources/public/images/invisible.png');
        $image = "invisible.png";
        $file = imagecreatefrompng($file);
        ob_start();
        imagepng($file);
        $str = ob_get_clean();
        
        $headers = array(
                'Content-Type'     => 'image/png',
                'Content-Disposition' => 'inline; filename="'.$image.'"');
        
        //unset variable
        unset($kernel);
        unset($file);
        unset($image);
        
        return new Response($str, 200, $headers);
    }
}
