<?php
namespace OfficeBrain\Bundle\PaymentBundle\Payum\Api\Authorize;

use Payum\AuthorizeNet\Aim\Bridge\AuthorizeNet\AuthorizeNetAIM;
use Symfony\Component\DependencyInjection\ContainerInterface;

class Factory
{
    /**
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    protected $container;
    protected $thirdPartyService;
    protected $request;
    protected $projectSetting;
    protected $projectUserSetting;
    protected $sandbox;
    protected $locale;
    protected $country;
    protected $tpsKey;
    protected $countryId;
    protected $userId;
    protected $instanceId;
    protected $mode;
    protected $userData;

    /**
     * @param ContainerInterface $container
     */
    public function __construct(ContainerInterface $container, $country)
    {
    	$this->container = $container;
    	$this->request = $this->container->get('request');
    	$this->projectSetting =  $this->request->get('project_setting');
    	$this->projectUserSetting =  $this->request->get('project_user_setting');
    	$this->userData = $this->projectUserSetting['user_data'];
    	$this->sandbox = $this->projectSetting['configuration_rule']['transaction']['shopping_cart']['checkout']['option']['payment_method']['option']['authorize_net']['sandbox']['status'];
    	$this->locale = $this->projectSetting['locale_language'];
    	$loc=explode('_',$this->locale);
    	$this->country = $country[$loc[1]];
    	$this->thirdPartyService=$this->container->get('office_brain_cms_third_party_service_manager');
    	$this->tpsKey='authorize_dot_net_aim';
    	$this->countryId=$this->country['id'];
    	$this->userId=$this->projectSetting['user_id'];
    	$this->instanceId=$this->projectSetting['instance_id'];
    	$this->mode=$this->sandbox ? 'sandbox' : 'production';
    	if($this->userId>0){
    		
    		if($this->userData->getUserType()=="end_user" && $this->projectSetting['instance_type']=="sub_shop"){ // end_user
    			echo "here i have to add code for subshop owner id"; die;
    			//$this->userId=$this->userData->getLocatedDistributorId();
    		}else{
    			if($this->userData->getUserType()=="end_user"){
					$this->userId=$this->userData->getLocatedDistributorId();
     			}
    		}
    	}else{
    		throw new \Exception('Error your session has expired.');
    	}
    }

    /**
     * @return Api
     */
    public function createAuthorizeNetAimApi()
    {
    	if(!$this->userId==NULL && $this->userId>0){
    	$paymentDetail=$this->prepareDetail($this->thirdPartyService->getConfigDetails($this->tpsKey, $this->countryId, $this->userId, $this->instanceId, $this->mode));
    	//echo "<pre>";print_r($paymentDetail); die;
    	$api = new AuthorizeNetAIM($paymentDetail['login_id'], $paymentDetail['transaction_key']);
    	$api->setSandbox($paymentDetail['sandbox']);
    	return $api;
    	//return new AuthorizeNetAIM($paymentDetail);
    	}else{
    		throw new \Exception('Error not find preferred payment'); 
    	}
    }
    
    public function prepareDetail($paymentDetail){
    	 $loginIdKey = 'authorize_aim_'.$this->mode.'_loginid';
    	 $transactionKey = 'authorize_aim_'.$this->mode.'_transactionkey';
    	 $data = array();
    	 if(count($paymentDetail)>0){
    	 	foreach($paymentDetail as $key=>$value){
    	 		if($value['key']==$loginIdKey){
    	 			$data['login_id']=$value['value'];
    	 		}
    	 		if($value['key']==$transactionKey){
    	 			$data['transaction_key']=$value['value'];
    	 		}
    	 	}
    	 	$data['sandbox']= $this->sandbox ? true : false;
    	 	return $data;
    	 }else{
    		throw new \Exception('Error : Invalid Payment Detail.'); 
    	}
    }
}
