<?php

namespace OfficeBrain\Bundle\RewardReferralBundle\Entity;

use Doctrine\ORM\EntityRepository;

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

    public function getPaginatorService()
    {
        return $this->paginatorService;
    }

    public function setPaginatorService($paginatorService)
    {
        $this->paginatorService = $paginatorService;
    }

    public function getProjectSetting()
    {
        return $this->paginatorService;
    }

    public function setProjectSetting($projectSetting, $configuration)
    {
        $this->projectSetting = $projectSetting;
        $this->prepareProjectSettingData();
        $this->configuration = $configuration;
    }

    public function prepareProjectSettingData()
    {
        $this->userId = $this->projectSetting['user_id'];
        $this->createdUid = $this->upatedUid = $this->userId;
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
    }
    
    /**
     * @Author Employee Id: 4589
     * Description : Function created for define a common querybuilder object
     *
     * @return object
     */
    public function getQueryBuilder()
    {
        return $this->getEntityManager()->getRepository('OfficeBrainRewardReferralBundle:ReferralPoint')->createQueryBuilder('rp');
    }
    
    /**
     * @Author Employee Id: 4589
     * Description : Function created for get points by user
     *
     * @return object
     */
    public function getPointByUserId($userId)
    {
        $queryBuilder = $this->getQueryBuilder();
        $queryBuilder->select('rp');
        $queryBuilder->where('rp.userId = :userId');
        $queryBuilder->setParameter('userId', $userId);
        $result = $queryBuilder->getQuery()->getResult();
        
        if ($result) {
            return $result[0];
        }
        
        return $result;
    }
        
    /**
     * @Author Employee Id: 4589
     * Description : Function created for save points by user
     *
     * @return object
     */
    public function updatePointByUserId($userId, $point, $type)
    {
        $referralPoint = $this->getPointByUserId($userId);
        
        if (!$referralPoint) {
            $referralPoint = new ReferralPoint();
            $referralPoint->setUserId($userId);
            $referralPoint->setPoint(0);
            $referralPoint->setInstanceId($this->instanceId);
            $referralPoint->setInstanceType($this->instanceType);
            $referralPoint->setUpdatedAt(new \DateTime());
        }
                    
        if ($type == "add") {
            $updatePoint = $referralPoint->getPoint() + $point;
            $maxRewardPoint = (int)$this->configuration['max_reward_point'];
            if ($maxRewardPoint > 0 && $updatePoint > $maxRewardPoint) {
                $updatePoint =  $maxRewardPoint;
            }
        } else {
            if (($referralPoint->getPoint() - $point) < 0) {
                $updatePoint = 0;
            } else {
                $updatePoint = $referralPoint->getPoint() - $point;
            }
        }
        $referralPoint->setPoint($updatePoint);
        
        $em = $this->getEntityManager();
        $em->persist($referralPoint);
        
        return $referralPoint;
    }
    
    public function commit()
    {
        $em = $this->getEntityManager();
        $em->flush();
    }

    public function clear()
    {
        $em = $this->getEntityManager();
        $em->clear();
    }
}
