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

use Payum\Paypal\ExpressCheckout\Nvp\Api;
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']['paypal']['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='paypal_express';
    	$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']=="distributor"){ // end_user
    			//echo "here i have to add code for subshop owner id"; die;
    			//$this->userId=$this->userData->getLocatedDistributorId();
    			//echo $this->projectSetting->site_owner_id; die;
    			$this->userId=$this->projectSetting['site_owner_id'];
    		}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 createPaypalExpressCheckoutApi()
    {
    	if(!$this->userId==NULL && $this->userId>0){
    	$paymentDetail=$this->prepareDetail($this->thirdPartyService->getConfigDetails($this->tpsKey, $this->countryId, $this->userId, $this->instanceId, $this->mode));
    	return new Api($paymentDetail);
    	}else{
    		// here if userId Not Found.
    		throw new \Exception('Error occurred with your request please try again');
    	}
    }
    
    public function prepareDetail($paymentDetail){
    	$usernameKey = 'paypal_express_'.$this->mode.'_username';
    	$passwordKey = 'paypal_express_'.$this->mode.'_password';
    	$signatureKey = 'paypal_express_'.$this->mode.'_signature';
    	$data = array();
    	if(count($paymentDetail)>0){
    		foreach($paymentDetail as $key=>$value){
    			if($value['key']==$usernameKey){
    				$data['username']=$value['value'];
    			}
    			if($value['key']==$passwordKey){
    				$data['password']=$value['value'];
    			}
    			if($value['key']==$signatureKey){
    				$data['signature']=$value['value'];
    			}
    		}
    		$data['sandbox']= $this->sandbox ? true : false;
    		return $data;
    	}else{
    		throw new \Exception('Error : Invalid Payment Detail.'); 
    	}
    }
}
