<?php

namespace OfficeBrain\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * ThirdPartyServiceKeyRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ThirdPartyServiceKeyRepository extends EntityRepository
{
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $cultureId;
    protected $cultureCode;
    protected $userId;
    protected $projectId;

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set project settings
     *
     * @param 1: $projectSetting
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to get project settings
     *
     * @param : None
     *
     * @return: project setting array
     *
     * @throws Exception: Null
     */
    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set member variables default value
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function prepareProjectSettingData()
    {
        $this->projectId = $this->projectSetting['project_id'];
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->userId = $this->projectSetting['user_id'];
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Find single result  based on ID
     *
     * @param $id
     *
     * @return : Third Party Service Key Object
     *
     * @throws Exception: Null
     */
    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        return $this->createQueryBuilder('ml')
        ->select('ml')
        ->where('ml.id = :id')
        ->setParameter(':id', $id)
        ->getQuery()
        ->getSingleResult();
    }

    /**
     * @author Employee Id: 4571
     *
     * Description : Find Id based on Form
     *
     * @param $formId
     *
     * @return : Result
     *
     * @throws Exception: Null
     */
    public function findId($formId, $countryId, $userId=null, $instId = null, $mode=null)
    {
        $sql =  $this->createQueryBuilder('tpsk')
        ->select(['tpsk, tpsm'])
        ->leftJoin('tpsk.thirdPartyServiceMeta', 'tpsm')
        ->where('tpsk.thirdPartySettingId = :formId')
        ->andWhere('tpsm.countryId= :countryId')
        ->setParameter(':formId', $formId)
        ->setParameter(':countryId', $countryId);
        
        if ($mode) {  
        	$sql->andwhere('tpsk.keyFor = :keyFor');
        	$sql->setParameter('keyFor', $mode);
        }
        
        if ($userId) { 
        	$sql->andwhere('tpsm.createdUid = :createdUid');
        	$sql->setParameter('createdUid', $userId);
        }
        
        if ($instId) {  
        	$sql->andwhere('tpsm.instanceId = :instanceId');
        	$sql->setParameter('instanceId', $instId);
        }
        
        
        $result = $sql->getQuery()->getResult();
        return $result;
        
    }
}
