<?php
namespace OfficeBrain\Bundle\CmsBundle\Manager\AbstractManager;

use OfficeBrain\Bundle\CmsBundle\Form\ThirdPartyServiceMetaType;
use OfficeBrain\Bundle\CmsBundle\Entity\ThirdPartySetting;
use OfficeBrain\Bundle\CmsBundle\Entity\ThirdPartyServiceKey;
use OfficeBrain\Bundle\CmsBundle\Entity\ThirdPartyServiceMeta;

/**
 * @author OfficeBrain 4571 <info@officebrain.com>
 *
 * Description : This is the abstract class which contains all the methods of its extended class.
 *
 **/
abstract class ThirdPartyServiceAbstract
{
    protected $projectSettings;
    protected $thirdPartySettingRepository;
    protected $thirdPartyServiceKeyRepository;
    protected $thirdPartyServiceMetaRepository;
    protected $instanceId;
    protected $responseArray;
    
    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set member variables default value
     *
     * @param 1: $entityManager
     * @param 2: $request
     * @param 3: $translator
     * @param 4: $knpPaginator
     * @param 5: $formFactory
     * @param 5: $country
     * @param 6: $language
     * @param 7: $basePath
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function __construct($entityManager, $request, $translator, $knpPaginator, $formFactory, $country, $language, $basePath)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->projectSettings = $this->request->get('project_setting');
        
        $this->thirdPartySettingRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:ThirdPartySetting');
        $this->thirdPartySettingRepository->setProjectSetting($this->projectSettings);
        
        $this->thirdPartyServiceKeyRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:ThirdPartyServiceKey');
        $this->thirdPartyServiceKeyRepository->setProjectSetting($this->projectSettings);
        $this->responseArray = null;

        $this->thirdPartyServiceMetaRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:ThirdPartyServiceMeta');
        $this->thirdPartyServiceMetaRepository->setProjectSetting($this->projectSettings);
        
        $this->translator = $translator;
        $this->formFactory = $formFactory;
        $this->knpPaginator = $knpPaginator;
        $this->country = $country;
        $this->language = $language;
        $this->basePath = $basePath;
        $this->instanceId = $this->projectSettings['instance_id'];
    }
    
    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to unset member variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->projectSettings);
        unset($this->thirdPartySettingRepository);
        unset($this->thirdPartyServiceKeyRepository);
        unset($this->thirdPartyServiceMetaRepository);
        unset($this->instanceId);
        unset($this->translator);
        unset($this->knpPaginator);
        unset($this->country);
        unset($this->language);
        unset($this->basePath);
        unset($this->formFactory);
        unset($this->responseArray);
    }

    /**
     * @author OfficeBrain 4571 <info@officebrain.com>
     *
     * Description:  To generate edit form.
     *
     * @param : NULL
     *
     * @return: data
     *
     * @throws Exception : Null
     */
    public function getForm($paymentMethod)
    {
        $country = $this->getCountry();
         
        $form = $this->formFactory->create(new ThirdPartyServiceMetaType($paymentMethod, $country, $this->projectSettings));
    
        return $form;
    }
    
    /**
     * @author OfficeBrain 4571 <info@officebrain.com>
     *
     * Description:  To load all payment methods
     *
     * @param : NULL
     *
     * @return: data
     *
     * @throws Exception : Null
     */
    public function getPaymentMethods()
    {
        return  $this->thirdPartySettingRepository->findParentPaymentMethods();
    }
    
    /**
     * @author OfficeBrain 4571 <info@officebrain.com>
     *
     * Description:  To load all shipment methods
     *
     * @param : NULL
     *
     * @return: data
     *
     * @throws Exception : Null
     */
    public function getShipmentMethods()
    {
        return $this->thirdPartySettingRepository->findParentShipmentMethods();
    }
    
    
    
    /**
     * @author Employee Id: 4571
     * Description : load Country wise collection
     *
     * @param : null
     *
     * @return banner object
     *
     * @throws null
     *
     **/
    
    public function getCountry()
    {
        $countryConfiguration=array();
        foreach ($this->country as $key=>$value) {
            $countryConfiguration[$value['id']]=$value['name']; // short_name
        }
        return $countryConfiguration;
    }
    
    /**
     * @author Employee Id: 4571
     * Description : get culture code
     *
     * @param 1 : string $cultureCode
     *
     * @return int $countryId
     *
     * @throws null
     *
     * */
    public function getCountryIdByCode($cultureCode)
    {
        foreach ($this->country as $key => $country) {
            if ($cultureCode == $country['short_name']) {  // $country['short_name'] // $key
                return $country['id'];
            }
        }
    }
}
