<?php

namespace OfficeBrain\CustomBundle\UserBundle\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\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use OfficeBrain\Bundle\CoreBundle\CustomeHandler as FormErrorManager;
use OfficeBrain\Bundle\UserBundle\Entity\User;
use OfficeBrain\Bundle\UserBundle\Form\UserType;
use OfficeBrain\CustomBundle\UserBundle\Form\FindDistributorType;
use OfficeBrain\Bundle\UserBundle\Form\UserPhotoType;
use OfficeBrain\Bundle\UserBundle\Form\LocateDistributorType;
use OfficeBrain\Bundle\UserBundle\Controller\UserController as BaseUserController;

class UserController extends BaseUserController
{
    /**
     * @author Employee Id: 4834
     * set default value
     * @return NULL
     */
    public function __construct()
    {
        parent::__construct();
        $this->searchFindDistributorParameterArray = array();
    }

    /**
     * @author Employee Id: 4834
     * unset default value
     * @return NULL
     */
    public function __destruct()
    {
        parent::__destruct();
        unset($this->searchFindDistributorParameterArray);
    }
    /**
     *
     * @author Employee Id: 4273
     *
     *         Create various objects required action in this class
     *
     * @return NULL
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        parent::initAction();        
		$this->customUserManager = $this->get('office_brain_custom_user_manager');
    }

    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - User
     * Function created to find distributor
     *
     * @Route("/{_locale}/find-distributor",
     * 	name="office_brain_userbundle_user_find_distributor",
     *  options={"expose"=true}
     * )
     * @Method({"GET","POST"})
     * @Template()
     */
    public function findDistributorSiteAction()
    {
    	$this->initAction();
    	$findDistributorObj = new FindDistributorType($this->container);
    	$formFindDistributor = $this->createForm($findDistributorObj);
    	$formFindDistributor->handleRequest($this->request);
    	$searchQuery = $formFindDistributor->getData();
    	$search_query = $this->prepareFindDistributorSearchParameterArray($searchQuery);    	
    	$this->responseArray['distributor'] = $this->customUserManager->findDistributorSite($this->page,$this->distributorRecordsPerPage,$search_query);
    	//echo "<pre>"; print_R($this->responseArray['distributor']);die;
    	$this->responseArray['findDistributor'] = $formFindDistributor->createView();
    	return $this->responseArray;
    	/*if($this->projectUserSetting['user_data']){ 
    		return $this->redirect($this->generateUrl('office_brain_page_homepage_view'));
    	}  else{
    		return $this->responseArray;
    	} 	*/
    }
    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - prepareFindDistributorSearchParameterArray
     */
    
    private function prepareFindDistributorSearchParameterArray($searchQuery)
    {
    	if(!empty($searchQuery)){
    		foreach($searchQuery as $key=>$value){
    			if($value !=''){
    				if($key == 'countryId' || $key == 'stateId' || $key == 'cityId' || $key == 'zipcode'){
    					$this->searchFindDistributorParameterArray[$key]=$value;
    				}
    			}    			
    		}
    	}
    	return $this->searchFindDistributorParameterArray;
    }

    /**
     * @Route("/{_locale}/changeFindDistributorCityStateCountry", name="office_brain_userbundle_changefinddistributorcitystatecountry", options={"expose"=true})
     * @Method({"POST"})
     * @Template()
     */
    public function changeFindDistributorCityStateCountryAction()
    {
    	$this->initAction();
    	$country = $this->getRequest()->get('countryId');
    	$state = $this->getRequest()->get('stateId');
    	$callType = $this->getRequest()->get('call_type','');
    	$data = array();
    	$master = $this->get('office_brain_core_manager');
    
    	if($callType == 'country_code'){
    		$data['state_data'] = array();
    		$dataStateData=$this->customUserManager->getFindDistributorStateData($country,$callType);
    		// array override with keychange for json object issue (sorting issue)
    		foreach ($dataStateData as $key=>$value) {
    			$data['state_data']['_'.$key]=$value;
    		}
    	}
    	if($callType == 'state_code'){
    		$data['city_data'] = array();
    		$dataCityData=$this->customUserManager->getFindDistributorStateData($state,$callType);
    		// array override with keychange for json object issue (sorting issue)
    		foreach ($dataCityData as $key=>$value) {
    			$data['city_data']['_'.$key]=$value;
    		}
    	}
    
    	unset($country);
    	unset($state);
    	unset($callType);
    	unset($master);
    
    	return new JsonResponse($data);
    }
    
}
