<?php

namespace OfficeBrain\Bundle\SubShopBundle\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;
use OfficeBrain\Bundle\SubShopBundle\Entity\SubShopTheme;
use OfficeBrain\Bundle\SubShopBundle\Form\SubShopThemeType;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * @author OfficeBrain 4494 <info@officebrain.com>
 *
 * Description : Sub shop Theme Admin class.
 *
 * @Route("/admin/{_locale}/sub-shop/theme")
 **/
class SubShopThemeAdminController extends Controller
{
    private $translator;

    private $request;

    private $projectSetting;

    private $responseArray;

    private $recordsPerPage;

    private $session;

    private $subShopManager;

    private $subshopThemeManager;

    public function __construct()
    {
        $this->translator = null;
        $this->request = null;
        $this->projectSetting = array();
        $this->responseArray = array();
        $this->recordsPerPage = null;
        $this->session = null;
        $this->subShopManager = null;
        $this->subshopThemeManager = null;
    }

    public function __destruct()
    {
        unset($this->translator);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->responseArray);
        unset($this->recordsPerPage);
        unset($this->session);
        unset($this->subShopManager);
        unset($this->subshopThemeManager);
    }

    public function initAction()
    {
        $this->translator = $this->get('translator');
        $this->request = $this->getRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->recordsPerPage = $this->projectSetting['configuration_rule']['site_management']['option']['sub_shop']['option']['sub_shop_theme_record_per_page']['status'];
        $this->session = $this->get('session');
        $this->subShopManager = $this->get('office_brain_subshop_manager');
        $this->subshopThemeManager = $this->get('office_brain_subshop_theme_manager');
        $this->responseArray = array();
    }

    /**
     * @author Employee Id: 4494
     * Description : Display subshop theme design.
     *
     * @param 1 : Subshop id
     *
     * @return type : Subshop theme design data.
     *
     * @throws Exception: Null
     *
     * @Route("/design/{subshop_id}", name="office_brain_subshopbundle_subshopthemeadmin_subshopthemedesign")
     *
     * @Method("GET")
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:theme_design.html.twig")
     */
    public function subshopThemeDesignAction()
    {
        $this->initAction();

        $this->responseArray['subshop'] = $this->subShopManager->getSubShop();

        $this->responseArray['subShopthemeDesign'] = $this->subshopThemeManager->prepareSubShopThemeDesign($this->responseArray['subshop']->getShopThemeId());

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Display subshop theme preview.
     *
     * @param 1 : Theme id
     * @param 2 : Subshop id
     *
     * @return type : Subshop theme design data.
     *
     * @throws Exception: Null
     *
     * @Route("/preview/{theme_id}/{subshop_id}",
     * name="office_brain_subshopbundle_subshopthemeadmin_subshopthemepreview",
     * defaults={"subshop_id"=0})
     *
     * @Method("GET")
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:theme_preview.html.twig")
     */
    public function subshopThemePreviewAction()
    {
        $this->initAction();

        $this->responseArray['subshopId'] = $this->request->get('subshop_id');

        if ($this->responseArray['subshopId'] > 0) {
            $this->responseArray['subShopthemeDesign'] = $this->subshopThemeManager->prepareSubShopThemeDesign($this->request->get('theme_id'));
        }

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Get subshop theme custom css.
     *
     * @param 3 : Subshop id
     *
     * @return type : Subshop theme design css data.
     *
     * @throws Exception: Null
     *
     * @Route("/design/custom/{subshop_id}",
     * name="office_brain_subshopbundle_subshopthemeadmin_subshopthemedesigncustomcss",
     * defaults={"subshop_id"=0})
     *
     * @Method("GET")
     * @Template()
     */
    public function subshopThemeDesignCustomCssAction()
    {
        $this->initAction();

        $this->responseArray['themeDesign'] = $this->subshopThemeManager->getSubShopThemeDesign($this->request->get('subshop_id'));

        $this->responseArray['themeDesign'] = json_decode($this->responseArray['themeDesign']->getCustomDesign(), 'Array');

        $response = new Response();
        $response->headers->set('Content-Type', 'text/css');

        return $this->render(
                'OfficeBrainSubShopBundle:SubShopThemeAdmin:theme_design_css.html.twig',
                $this->responseArray,
                $response
        );
    }

    /**
     * @author Employee Id: 4494
     * Description : Update subshop theme custom design.
     *
     * @param 1 : Subshop id
     *
     * @return type : Subshop theme design data.
     *
     * @throws Exception: Null
     *
     * @Route("/design/custom/update/{subshop_id}",
     * name="office_brain_subshopbundle_subshopthemeadmin_subshopthemedesigncustomupdateajax",
     * defaults={"subshop_id"=0})
     *
     * @Method("POST")
     */
    public function subshopThemeDesignCustomUpdateAjaxAction()
    {
        $this->initAction();

        $this->responseArray = $this->subshopThemeManager->updateSubShopThemeCustomDesing();

        return new JsonResponse($this->responseArray);
    }

    /**
     * @author Employee Id: 4494
     * Description : Update subshop theme design .
     *
     * @param 1 : Subshop id
     *
     * @return type : Redirect to subshop theme design.
     *
     * @throws Exception: Null
     *
     * @Route("/design/status/{action}/{subshop_id}",
     * name="office_brain_subshopbundle_subshopthemeadmin_subshopthemedesignstatus",
     * defaults={"subshop_id"=0})
     *
     * @Method("GET")
     */
    public function subshopThemeDesignStatusAction()
    {
        $this->initAction();

        $this->subshopThemeManager->setSubShopThemeDesingStatus();

        $this->session->getFlashBag()->set('success_message', $this->translator->trans('subshop_content.admin.design.action.subshop_theme_updated_success'));

        return $this->redirect($this->generateUrl('office_brain_subshopbundle_subshopthemeadmin_subshopthemedesign', array('subshop_id' => $this->request->get('subshop_id'))));
    }

    /**
     * @author Employee Id: 4494
     * Description : Subshop theme list.
     *
     * @param 1 : Record per page
     * @param 2 : Page number
     *
     * @return type : Subshop theme data.
     *
     * @throws Exception: Null
     *
     * @Route("/list/{records_per_page}/{page}",
     * name="office_brain_subshopbundle_subshopthemeadmin_subshoplisttheme",
     * requirements={"records_per_page"="2|4|8|16|996", "page"="\d+"},
     * defaults={"records_per_page"=2,"page"=1})
     *
     * @Method("GET")
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:theme.html.twig")
     */
    public function subshopListThemeAction()
    {
        $this->initAction();

        $this->responseArray['records_per_page'] = $this->recordsPerPage;
        $this->responseArray['page'] = $this->request->get('page');

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Get subshop themne by status
     *
     * @param 1 : Record per page
     * @param 2 : page number
     *
     * @return type : Inquiry data.
     *
     * @throws Exception: Null
     *
     * @Route("/list/{records_per_page}/{page}",
     * name="office_brain_subshopbundle_subshoplistthemeajax",
     * requirements={"records_per_page"="2|4|8|16|996", "page"="\d+"},
     * defaults={"records_per_page"=2, "page"=1})
     *
     *
     * @Method("POST")
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:list.html.twig")
     */
    public function subshopListThemeAjaxAction()
    {
        $this->initAction();

        $this->responseArray['pagination'] = $this->subshopThemeManager->searchByStatusSubShopTheme();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Display subshop theme form.
     *
     * @param 1 : No paramater
     *
     * @return type : Form view.
     *
     * @throws Exception: Null
     *
     * @Route("/add", name="office_brain_subshopbundle_subshopthemeadmin_subshopaddtheme")
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:add.html.twig")
     */
    public function subshopAddThemeAction()
    {
        $this->initAction();

        $this->responseArray['form'] = $this->createForm(new SubShopThemeType($this->getDoctrine()->getManager()))->createView();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Submit subshop theme form.
     *
     * @param 1 : No paramater
     *
     * @return type : Form view and data.
     *
     * @throws Exception: Null
     *
     * @Route("/add", name="office_brain_subshopbundle_subshopthemeadmin_processsubshopaddtheme")
     *
     * @Method("POST")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:add.html.twig")
     */
    public function processSubshopAddThemeAction()
    {
        $this->initAction();

        $form = $this->createForm(new SubShopThemeType($this->getDoctrine()->getManager()), new SubShopTheme());
        $form->handleRequest($this->request);

        if ($form->isValid()) {
            $this->subshopThemeManager->setSubShopThemeColor($form->get('themeColorList')->getData(), $form->getData());

            $this->session->getFlashBag()->set('success_message',  $this->translator->trans('subshop_content.admin.design.action.subshop_theme_added_success'));

            return $this->redirect($this->generateUrl('office_brain_subshopbundle_subshopthemeadmin_subshoplisttheme'));
        }

        $this->responseArray['form'] = $form->createView();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Display edit subshop theme form.
     *
     * @param 1 : Subshop theme id
     *
     * @return type : Form view and data.
     *
     * @throws Exception: Null
     *
     * @Route("/edit/{id}", name="office_brain_subshopbundle_subshopthemeadmin_subshopthemeedit")
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:edit.html.twig")
     */
    public function subshopThemeEditAction()
    {
        $this->initAction();

        $subshopTheme = $this->subshopThemeManager->getSubShopTheme($this->request->get('id'));

        if (!$subshopTheme) {
            throw $this->createNotFoundException('No record found for id '.$this->request->get('id'));
        } else {
        	$shopThemeImage = $subshopTheme->getThemeImageName();
        	$subshopTheme->setThemeImageName(null);
            $form = $this->createForm(new SubShopThemeType($this->getDoctrine()->getManager()), $subshopTheme);
            $form->get('shopThemeImage')->setData($shopThemeImage);
        }

        $this->responseArray['form'] = $form->createView();

        $this->responseArray['media_location'] = $this->subshopThemeManager->mediaLocation();      
        $this->responseArray['media_image'] = $shopThemeImage;
        
        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Submit edit subshop theme form.
     *
     * @param 1 : Subshop theme id
     *
     * @return type : Form view and data.
     *
     * @throws Exception: Null
     *
     * @Route("/edit/{id}", name="office_brain_subshopbundle_subshopthemeadmin_processsubshopthemeedit")
     *
     * @Method("POST")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:edit.html.twig")
     */
    public function processSubshopThemeEditAction()
    {
        $this->initAction();

        $subshopTheme = $this->subshopThemeManager->getSubShopTheme($this->request->get('id'));

        if (!$subshopTheme) {
            throw $this->createNotFoundException($this->translator->trans('subshop_content.admin.design.action.no_record_found').$this->request->get('id'));
        } else {
            $subshopTheme->setThemeImageName(null);
            $form = $this->createForm(new SubShopThemeType($this->getDoctrine()->getManager()), $subshopTheme);

            $form->handleRequest($this->request);

            if ($form->isValid()) {
            	
                $this->subshopThemeManager->setSubShopThemeColor($form->get('themeColorList')->getData(), $form->getData(),$form->get('shopThemeImage')->getData());

                $this->session->getFlashBag()->set('success_message', $this->translator->trans('subshop_content.admin.design.action.subshop_theme_updated_success'));

                return $this->redirect($this->generateUrl('office_brain_subshopbundle_subshopthemeadmin_subshoplisttheme'));
            }
        }

        $this->responseArray['form'] = $form->createView();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Delete subshop theme.
     *
     * @param 1 : Subshop theme id
     *
     * @return type : Redirect to subshop theme list.
     *
     * @throws Exception: Null
     *
     * @Route("/delete/{id}", name="office_brain_subshopbundle_subshopthemeadmin_subshopthemedelete")
     *
     * @Method("GET")
     */
    public function subshopThemeDeleteAction()
    {
        $this->initAction();

        $this->subshopThemeManager->deleteSubShopTheme($this->request->get('id'));

        $this->session->getFlashBag()->set('success_message', $this->translator->trans('subshop_content.admin.design.action.subshop_theme_deleted_success'));

        return $this->redirect($this->generateUrl('office_brain_subshopbundle_subshopthemeadmin_subshoplisttheme'));
    }

    /**
     * @author Employee Id: 4494
     * Description : Revert subshop theme.
     *
     * @param 1 : Subshop theme id
     *
     * @return type : Redirect to subshop theme list.
     *
     * @throws Exception: Null
     *
     * @Route("/revert/{id}", name="office_brain_subshopbundle_subshopthemeadmin_subshopthemerevert")
     *
     * @Method("GET")
     */
    public function subshopThemeRevertAction()
    {
        $this->initAction();

        $this->subshopThemeManager->revertSubShopTheme($this->request->get('id'));

        $this->session->getFlashBag()->set('success_message', $this->translator->trans('subshop_content.admin.design.action.subshop_theme_reverted_success'));

        return $this->redirect($this->generateUrl('office_brain_subshopbundle_subshopthemeadmin_subshoplisttheme'));
    }

    /**
     * @author Employee Id: 4494
     * Description : Search subshop theme by status.
     *
     * @param 1 : status id
     * @param 2 : Record per page
     * @param 3 : Page number
     *
     * @return type : Subshop theme list.
     *
     * @throws Exception: Null
     *
     * @Route("/search/status/{status_id}/{records_per_page}/{page}",
     * name="office_brain_subshopbundle_subshopthemeadmin_subshopthemesearchbystatus",
     * requirements={"records_per_page"="2|4|8|16|996", "page"="\d+"},
     * defaults={"records_per_page"=2, "page"=1})
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:list.html.twig")
     */
    public function subshopThemeSearchByStatusAction()
    {
        $this->initAction();

        $this->responseArray['pagination'] = $this->subshopThemeManager->searchByStatusSubShopTheme();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Search deleted subshop theme.
     *
     * @param 1 : Record per page
     * @param 2 : Page number
     *
     * @return type : Subshop theme list.
     *
     * @throws Exception: Null
     *
     * @Route("/search/delete/{records_per_page}/{page}",
     * name="office_brain_subshopbundle_subshopthemeadmin_subshopthemesearchbydelete",
     * requirements={"records_per_page"="2|4|8|16|996", "page"="\d+"},
     * defaults={"records_per_page"=2,"page"=1})
     *
     * @Method("GET")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:list.html.twig")
     */
    public function subshopThemeSearchByDeleteAction()
    {
        $this->initAction();

        $this->responseArray['pagination'] = $this->subshopThemeManager->searchByDeleteSubShopTheme();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Search SubShop Theme list
     *
     * @param 1 : No paramater
     *
     * @return type : SubShop list .
     *
     * @throws Exception: Null
     *
     * @Route("/search/{records_per_page}/{page}",
     * name="office_brain_subshopbundle_subshopadmin_subshopthemesearchajax",
     * requirements={"records_per_page"="2|4|8|16|996", "page"="\d+"},
     * defaults={"records_per_page"=2,"page"=1})
     *
     * @Method("POST")
     *
     * @Template("OfficeBrainSubShopBundle:SubShopThemeAdmin:list.html.twig")
     */
    public function subshopThemeSearchAjaxAction()
    {
        $this->initAction();

        $this->responseArray['pagination'] = $this->subshopThemeManager->searchSubShopTheme();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Search SubShop Theme list
     *
     * @param 1 : No paramater
     *
     * @return type : SubShop list .
     *
     * @throws Exception: Null
     *
     * @Route("/change", name="office_brain_subshopbundle_subshopadmin_ubshopthemechangeajax")
     *
     * @Method("POST")
     */
    public function subshopThemeChangeAjaxAction()
    {
        $this->initAction();

        $this->responseArray = $this->subshopThemeManager->changeSubShopTheme();

        return new JsonResponse($this->responseArray);
    }

    /**
     * @author Employee Id: 4494
     * Description : Upload subshop theme files.
     *
     * @param 1 : Subshop id
     *
     * @return type : Subshop theme design data.
     *
     * @throws Exception: Null
     *
     * @Route("/design/custom/file-upload", name="office_brain_subshopbundle_subshopthemeadmin_subshopthemedesigncustomfileuploadajax", options={"expose"=true})
     *
     * @Method("POST")
     */
    public function subshopThemeDesignCustomFileUploadAjaxAction()
    {
        $this->initAction();

        $uploadedFile = $this->subshopThemeManager->updateSubShopThemeCustomImageDesing();

        if ($uploadedFile) {
            $this->responseArray['status'] = strtolower($this->translator->trans('subshop_content.admin.action.success'));
            $this->responseArray['file'] = $uploadedFile['file'];
            $this->responseArray['name'] = $uploadedFile['name'];
        } else {
            $this->responseArray['status'] = $this->translator->trans('subshop_content.admin.action.error');
            $this->responseArray['error_message'] = $this->translator->trans('subshop_content.admin.action.technical_issue');
        }

        return new JsonResponse($this->responseArray);
    }
}