<?php

namespace OfficeBrain\Bundle\CmsBundle\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 OfficeBrain\Bundle\SearchBundle\Form\Type\ProductSearchType;
use OfficeBrain\Bundle\SearchBundle\Form\Data\ProductSearchData;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

/**
 * @author OfficeBrain 410175 <info@officebrain.com>
 *
 * Description : CMS Menu controller class
 *
 * @Route("/api/v1/{_locale}/cms/menu", defaults={"_format" = "json"})
 **/
class ApiCmsMenuController extends CmsMenuController
{
	const SUCCESS = 'success';
	const ERROR = 'error';
	const SUCCESS_CODE = 1;
	const ERROR_CODE = 0;
	
    protected $request;

    protected $projectSetting;

    protected $cmsMenuManager;

    protected $responseArray;
    
    protected $searchManager;
    
    protected $languageId;
    
    protected $entityManager;
    
    protected $userId;

    public function __construct()
    {
        $this->request = null;
        $this->projectSetting = array();
        $this->responseArray = array();
        $this->cmsMenuManager = null;
        $this->searchManager = null;
        $this->languageId = null;
        $this->entityManager = null;
        $this->userId = null;
    }

    public function __destruct()
    {
        unset($this->request);
        unset($this->projectSetting);
        unset($this->cmsMenuManager);
        unset($this->responseArray);
        unset($this->searchManager);
        unset($this->languageId);
        unset($this->entityManager);
        unset($this->userId);
    }

    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->cmsMenuManager = $this->get('office_brain_cms_menu_manager');
        $this->responseArray = array();
        $this->searchManager = $this->get('office_brain_search_manager');
        $this->languageId = $this->searchManager->getLanguageId();
        $this->entityManager = $this->getDoctrine()->getManager();
        $this->mobileAppHeaderProductMenuId = $this->getParameter('mobile_app.header_product_menu_id');
        $this->baseUrl = $this->getParameter('base_url');
        $this->userId = $this->projectSetting['user_id'];
    }

    /**
     * @author Employee Id: 4494
     * Description : menu detail.
     *
     * @param 1 :Menu slug
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/{slug}/{allow_override}", name="office_brain_cmsbundle_cmsmenu_menu_api",
     * defaults={"slug"="header-menu", "allow_override"=0})
     *
     * @ApiDoc(
     *      resource=true,
     *      section="Menu",
     *      description="Get Menu by slug.",
     *      statusCodes={
     *              1="Returned when success",
     *              403="Returned when the user is not authorized to see this link",
     *              0="Returned when form validation is failed",
     *      },
     *      requirements={     
     *             {"name"="_locale", "dataType"="string", "required"=true, "default"="en_us"},
     *             {"name"="access_token", "dataType"="string", "required"=true, "description"="valid access token"},
     *             {"name"="instance_id", "dataType"="integer", "required"=true, "description"="valid instance_id"},
     *             {"name"="instance_type", "dataType"="string", "required"=true, "description"="valid instance_type"},
     *             {"name"="slug", "dataType"="string",  "default"="header-menu"},
     *             {"name"="allow_override", "dataType"="boolean",  "default"="0"},
     *      },
     * )
     * 
     * @Method("GET")
     * @Template()
     */
    public function menuAction()
    {
    	$this->initAction();
    
    	//$data = $this->cmsMenuManager->prepareMenu ( false, false, true, true );
    	$data = $this->cmsMenuManager->prepareMenuMobileApi($this->baseUrl, $this->userId, false, false, true, true);
    	   
        if ( $data != null && isset($data['menu']) && count( $data['menu'] ) > 0 ) {
        	 return array(
                    'status' => self::SUCCESS,
                    'status_code' => self::SUCCESS_CODE,
                    'response_data' => $data,
            );
        }
        
        return array(
        		'status' => self::ERROR,
        		'status_code' => self::ERROR_CODE,
        		'message' => 'no data found',
        );
    }
}