<?php

namespace OfficeBrain\Bundle\HomeSectionBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use OfficeBrain\Bundle\HomeSectionBundle\Entity\HomeSection;
use OfficeBrain\Bundle\HomeSectionBundle\Form\HomeSectionType;
use OfficeBrain\Bundle\HomeSectionBundle\Form\FunPromoSectionType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Validator\Constraints\Count;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use OfficeBrain\Bundle\CoreBundle\Cache\RedisCacheService;
/**
 *
 * @author OfficeBrain 4571 <info@officebrain.com>
 * Description: Manage Banner to perform CRUD operation
 *
 * @Route("admin/{_locale}")
 *
 */
class HomeSectionAdminController extends Controller
{
   private $translator;
    
    private $request;
   
    private $responseArray;
    
    private $session;
    
    private $commonLibraryManager;
    
    private $instanceId;
        
    /**
     * @author Employee Id: 4571
     * Description : To set member variables default value
     *
     * @param  : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __construct()
    {
        $this->request = null;
        $this->homeSectionManager = null;
        $this->responseArray = array();
        $this->session = null;
        $this->instanceId = 0;
    }
        
    /**
     * @author Employee Id: 4571
     * Description : To unset member variables
     *
     * @param  : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __destruct()
    {
        unset($this->request);
        unset($this->homeSectionManager);
        unset($this->responseArray);
        unset($this->session);
        unset($this->instanceId);
    }
        
    /**
     * @author Employee Id: 4571
     * Description : To initialize request and services
     *
     * @param  : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->homeSectionManager = $this->get('office_brain_home_section_manager');
        $instanceId = $this->request->get('project_setting')["instance_id"];
        $this->cmsInstanceId = $this->request->get('instance_id');
        if($this->cmsInstanceId > 0)
        {
            $this->instanceId = $this->cmsInstanceId;
        }
        else
        {
            $this->instanceId = $instanceId;
        }
        
        $this->translator = $this->get('translator');
        $this->session = $this->request->getSession();
        $this->commonLibraryManager = $this->get('office_brain_common_library');
        $this->page = $this->request->get('page', '1');
        $this->recordsPerPage = $this->request->get('records_per_page', 50);
        $this->statsh_cache_wrapper=RedisCacheService::create();
		$this->moduleCachePrefix = '__HomePageSectionList__';
    }

    /**
     *
     * @Route("/home-section/list",
     * name="office_brain_homesectionbundle_admin_homesectionlist",
     * options={"expose"=true}
     * )
     * @Method({"GET"})
     */
    public function homesectionListAction()
    {
        $this->initAction();
        $keywords = $this->request->get('keywords');
        $this->responseArray['records_per_page'] = $this->recordsPerPage;
        $this->responseArray['page'] = $this->page;
        $this->responseArray['data'] = $this->homeSectionManager->getHomeSectionList($keywords, $this->page, $this->recordsPerPage);

        return $this->render('OfficeBrainHomeSectionBundle:HomeSectionAdmin:list.html.twig',$this->responseArray);
    }

    /**
     *
     * @Route("/fun-promo-section/list",
     * name="office_brain_homesectionbundle_admin_funpromosectionlist",
     * options={"expose"=true}
     * )
     * @Method({"GET"})
     */
    public function funpromosectionListAction()
    {
        $this->initAction();
        $keywords = $this->request->get('keywords');
        $this->responseArray['records_per_page'] = $this->recordsPerPage;
        $this->responseArray['page'] = $this->page;
        $this->responseArray['data'] = $this->homeSectionManager->getFunpromoSectionList($keywords, $this->page, $this->recordsPerPage);

        return $this->render('OfficeBrainHomeSectionBundle:HomeSectionAdmin:funpromos.html.twig',$this->responseArray);
    }


    /**
     *
     * @author Employee Id: 4571
     * Description : To Sort Listing
     *
     * @param  : null
     *
     * @return Response
     *
     * @throws null
     *
     * @Route("/fun-promo-section/sort-position",
     * name="office_brain_homesectionbundle_admin_positionlist",
     * options={"expose"=true}
     * )
     * @Template()
     *
     **/
    public function positionListAction()
    {
        $this->initAction();
        $param=$this->request->request->all();
        if (count($param)) {
            foreach ($param['row'] as $key=>$val) {
                $homeSection=new HomeSection();
                $key=$key+1;

                $homeSectionManager = $this->homeSectionManager->setListPosition($homeSection, $key,$val);
            }
        }
        if ($this->request->isXmlHttpRequest()) {
			RedisCacheService::removeCacheByPrefix($this->statsh_cache_wrapper,$this->moduleCachePrefix);
            $response = new Response();
            $output = array('success' => 'success');
            $response->headers->set('Content-Type', 'application/json');
            $response->setContent(json_encode($output));
            return $response;
        }
    }

    /**
     *
     * @author Employee Id: 4571
     * Description : Add home section action to save section
     *
     * @param  : null
     *
     * @return Response Array to twig
     *
     * @throws null
     *
     * @Route("/funpromo-section/add", name="office_brain_homesectionbundle_admin_addfunpromosection",
     * options={"expose"=true}
     * )
     * @Method("GET")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:addfunpromoSection.html.twig")
     *
     **/
    public function addfunpromosectionAction()
    {
        $this->initAction();
        $homeSection = new HomeSection();
        $form = $this->createForm(new FunPromoSectionType(), $homeSection, array(
                'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processfunhomesection',array('instance_id'=>$this->instanceId)),
                'method' => 'POST',
        ));
        $this->responseArray['homeSection']=$homeSection;
        $this->responseArray['form']=$form->createView();
        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4571
     * Description : Add home section action to save section
     *
     * @param  : null
     *
     * @return Response Array to twig
     *
     * @throws null
     *
     * @Route("/home-section/add", name="office_brain_homesectionbundle_admin_addhomesection",
     * options={"expose"=true}
     * )
     * @Method("GET")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:addHomeSection.html.twig")
     *
     **/
    public function addHomeSectionAction()
    {
        $this->initAction();
        $homeSection = new HomeSection();
        $form = $this->createForm(new HomeSectionType(), $homeSection, array(
                'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processhomesection',array('instance_id'=>$this->instanceId)),
                'method' => 'POST',
        ));
        $this->responseArray['homeSection']=$homeSection;
        $this->responseArray['form']=$form->createView();
        return $this->responseArray;
    }

    /**
     *
     * @author Employee Id: 4571
     * Description : Header section Type submit form action to save Section
     *
     * @param  : null
     * @return Response Array to twig
     * @throws null
     *
     * @Route("/home-section/add", name="office_brain_homesectionbundle_admin_processhomesection",
     * options={"expose"=true}
     * )
     * @Method("POST")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:addHomeSection.html.twig")
     *
     **/
    public function processHomeSectionAction()
    {
        $this->initAction();
       
        $homeSection = new HomeSection();
        $form = $this->createForm(new HomeSectionType(), $homeSection, array(
                'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processhomesection',array('instance_id'=>$this->instanceId)),
                'method' => 'POST',
        ));
            
        $form->handleRequest($this->request);
        if ($form->isValid()) { // if form valid than save to db
            $this->homeSectionManager->setHomeSection($form->getData());
            RedisCacheService::removeCacheByPrefix($this->statsh_cache_wrapper,$this->moduleCachePrefix);
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('Section added successfully.'));
            return $this->redirect($this->generateUrl('office_brain_homesectionbundle_admin_homesectionlist'));
        }else {
            $this->responseArray['homeSection']=$homeSection;
            $this->responseArray['form']=$form->createView();
            return $this->responseArray;
        }                 
 
    }


    /**
     *
     * @author Employee Id: 4571
     * Description : Header section Type submit form action to save Section
     *
     * @param  : null
     * @return Response Array to twig
     * @throws null
     *
     * @Route("/funpromo-section/add", name="office_brain_homesectionbundle_admin_processfunhomesection",
     * options={"expose"=true}
     * )
     * @Method("POST")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:addfunpromoSection.html.twig")
     *
     **/
    public function processfunhomesectionAction()
    {
        $this->initAction();
       
        $homeSection = new HomeSection();
        $form = $this->createForm(new FunPromoSectionType(), $homeSection, array(
                'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processfunhomesection',array('instance_id'=>$this->instanceId)),
                'method' => 'POST',
        ));
            
        $form->handleRequest($this->request);
        if ($form->isValid()) { // if form valid than save to db
            $this->homeSectionManager->setHomeSection($form->getData());
            RedisCacheService::removeCacheByPrefix($this->statsh_cache_wrapper,$this->moduleCachePrefix);
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('Section added successfully.'));
            return $this->redirect($this->generateUrl('office_brain_homesectionbundle_admin_funpromosectionlist'));
        }else {
            $this->responseArray['homeSection']=$homeSection;
            $this->responseArray['form']=$form->createView();
            return $this->responseArray;
        }                 
 
    }

     /**
     *
     * @Route("/home-section/edit/{id}", name="office_brain_homesectionbundle_admin_edithomesection",
     * options={"expose"=true}
     * )
     * @Method("GET")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:editHomeSection.html.twig")
     *
     **/
    public function edithomesectionAction($id)
    {
        $this->initAction();
        
        $id=$this->request->get('id');
        $homeSection = $this->homeSectionManager->getHomeSection($id);
        $form=$this->createForm(new HomeSectionType(), $homeSection, array(
                    'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processhomesectionedit', array('id'=>$id)),
                    'method' => 'POST',
            ));
                                 
        $this->responseArray['homeSection']=$homeSection;
        $this->responseArray['form']=$form->createView();
        $this->responseArray['homeSection_id'] = $id;

        return $this->responseArray;
    }

     /**
     *
     * @Route("/home-section/edit/{id}", name="office_brain_homesectionbundle_admin_processhomesectionedit",
     * options={"expose"=true}
     * )
     * @Method("POST")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:editHomeSection.html.twig")
     *
     **/
    public function processhomesectioneditAction($id)
    {
        $this->initAction();
        
        $id=$this->request->get('id');
        $homeSection = $this->homeSectionManager->getHomeSection($id);
        $imagePathData  = $homeSection->getUploadedBanner();
        $form=$this->createForm(new HomeSectionType(), $homeSection, array(
                    'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processhomesectionedit', array('id'=>$id)),
                    'method' => 'POST',
            ));
                                 
        $form->handleRequest($this->request);

        if ($form->isValid()) {
            $this->homeSectionManager->updateSection($form->getData(),$homeSection);
            RedisCacheService::removeCacheByPrefix($this->statsh_cache_wrapper,$this->moduleCachePrefix);
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('Section edited successfully.'));
                return $this->redirect($this->generateUrl('office_brain_homesectionbundle_admin_homesectionlist'));
            
        }
        else {
        $this->responseArray['homeSection']=$homeSection;
        $this->responseArray['form']=$form->createView();
        $this->responseArray['homeSection_id'] = $id;
        return $this->responseArray;
        } 
    }


    /**
     *
     * @Route("/funpromo-section/edit/{id}", name="office_brain_homesectionbundle_admin_editfunpromosection",
     * options={"expose"=true}
     * )
     * @Method("GET")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:editfunpromoSection.html.twig")
     *
     **/
    public function editfunpromosectionAction($id)
    {
        $this->initAction();
        
        $id=$this->request->get('id');
        $homeSection = $this->homeSectionManager->getHomeSection($id);
        $form=$this->createForm(new FunPromoSectionType(), $homeSection, array(
                    'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processfunhomesectionedit', array('id'=>$id)),
                    'method' => 'POST',
            ));
                                 
        $this->responseArray['homeSection']=$homeSection;
        $this->responseArray['form']=$form->createView();
        $this->responseArray['homeSection_id'] = $id;

        return $this->responseArray;
    }

     /**
     *
     * @Route("/funpromo-section/edit/{id}", name="office_brain_homesectionbundle_admin_processfunhomesectionedit",
     * options={"expose"=true}
     * )
     * @Method("POST")
     * @Template("OfficeBrainHomeSectionBundle:HomeSectionAdmin:editfunpromoSection.html.twig")
     *
     **/
    public function processfunhomesectioneditAction($id)
    {
        $this->initAction();
        
        $id=$this->request->get('id');
        $homeSection = $this->homeSectionManager->getHomeSection($id);
        $imagePathData  = $homeSection->getUploadedBanner();
        $form=$this->createForm(new FunPromoSectionType(), $homeSection, array(
                    'action' => $this->generateUrl('office_brain_homesectionbundle_admin_processfunhomesectionedit', array('id'=>$id)),
                    'method' => 'POST',
            ));
                                 
        $form->handleRequest($this->request);

        if ($form->isValid()) {
            $this->homeSectionManager->updateSection($form->getData(),$homeSection);
            RedisCacheService::removeCacheByPrefix($this->statsh_cache_wrapper,$this->moduleCachePrefix);
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('Section edited successfully.'));
                return $this->redirect($this->generateUrl('office_brain_homesectionbundle_admin_funpromosectionlist'));
            
        }
        else {
        $this->responseArray['homeSection']=$homeSection;
        $this->responseArray['form']=$form->createView();
        $this->responseArray['homeSection_id'] = $id;
        return $this->responseArray;
        } 
    }
}
