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

use Symfony\Component\HttpFoundation\Request;

/**
 * @author OfficeBrain 4464 <info@officebrain.com>
 *
 * Description : This is the abstract class which contains all the methods of its extended classes.
 *
 **/
abstract class FeedBackAbstract
{
    protected $entityManager;
    protected $request;
    protected $orderManager;
    protected $projectSetting;
    protected $emailManager;
    protected $userManager;
    protected $feedBackMasterRepository;
    protected $isFeedbackModerationOn;
    protected $serializer;
    protected $translator;
    protected $solrClientManager;
    protected $feedbackApi;
    protected $userId;
    protected $knpPaginator;
    protected $recordsPerPage;
    
    protected $instanceId;
    protected $projectId;
    protected $cultureCode;
    protected $locale;
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to set member variables default value
     *
     * @param 1: $entityManager
     * @param 2: $request
     * @param 3: $orderManager
     * @param 4: $emailManager
     * @param 5: $userManager
     * @param 6: $feedbackApiManager
     * @param 7: $solrManager
     * @param 8: $knpPaginator
     * @param 9: $serializer
     * @param 10: $translator
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function __construct($entityManager, $request, $orderManager, $emailManager, $userManager, $feedbackApiManager, $solrManager, $knpPaginator, $serializer, $translator)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->orderManager = $orderManager;
        $this->projectSetting = $this->request->get('project_setting');
        $this->projectId = $this->projectSetting['project_id'];
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->locale = $this->projectSetting['locale_language'];
        $this->isFeedbackModerationOn = $this->projectSetting['configuration_rule']['site_management']['option']['feedback']['option']['feedback_moderation']['status'];
        $this->emailManager = $emailManager;
        $this->userManager = $userManager;
        $this->serializer = $serializer;
        $this->feedbackApi = $feedbackApiManager;
        $this->userId = $this->projectSetting['user_id'];
        $this->knpPaginator = $knpPaginator;
        $this->recordsPerPage = $this->projectSetting['configuration_rule']['site_management']['option']['feedback']['option']['feedback_record_per_page']['status'];
        
        $this->feedBackMasterRepository = $this->entityManager->getRepository('OfficeBrainFeedBackBundle:FeedBackMaster');
        $this->feedBackMasterRepository->setProjectSetting($this->projectSetting);

        $this->translator = $translator;
        
        //$culture = explode('_', $this->locale);
        //$this->solrClientManager = $solrManager->getClient('product_'.$culture[0]);
        $this->solrClientManager = $solrManager->getClient('product_'.$this->locale);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * 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->orderManager);
        unset($this->projectSetting);
        unset($this->emailManager);
        unset($this->userManager);
        unset($this->serializer);
        unset($this->translator);
        unset($this->feedbackApi);
        unset($this->userId);
        unset($this->feedBackMasterRepository);
        unset($this->isFeedbackModerationOn);
        unset($this->locale);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to list feedbacks given by the user from the database
     *
     * @param 1: $userId
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function listFeedBack($userId, $page)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to insert feedback given by the user into the database
     *
     * @param 1: $formData
     * @param 2: $orderId
     * @param 3: $productId
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function setFeedBack($formData, $orderId, $productId)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to approve the feedback given by the user
     *
     * @param 1: $feedBackId
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function approveFeedBack($feedBackId)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to reject the feedback given by the user
     *
     * @param 1: $feedBackId
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function rejectFeedBack($feedBackId)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to (soft)delete the feedback given by the user
     *
     * @param 1: $feedBackId
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function deleteFeedBack($feedBackId)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get product details from the product id
     *
     * @param 1: $productId
     *
     * @return : Product Details array
     *
     * @throws Exception : Null
     *
     **/
    public function getProductDetails($productId)
    {
        //$productDetailArray = $this->productDetailService->getProductDetailById($productId, $this->request->get('_locale'));
    	$productDetailArray = $this->orderManager->getProductDetailById($productId, $this->request->get('_locale'));
        if ($productDetailArray == null) {
            //throw new NotFoundHttpException($this->translator->trans('feedback.product_not_found'));
            $productDetailArray['product_not_found'] = $this->translator->trans('feedback.product_not_found');
        }
        unset($productId);
        return $productDetailArray;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get user details from the user id
     *
     * @param 1: $userId
     *
     * @return : tbl_user object
     *
     * @throws Exception : Null
     *
     **/
    public function getUserDetails($userId)
    {
        $userDetails = $this->userManager->getUserById($userId);
        if (empty($userDetails)) {
            //throw new NotFoundHttpException($this->translator->trans('feedback.user_not_found'));
            $userDetails['user_not_found'] = $this->translator->trans('feedback.user_not_found');
        }
        unset($userId);
        return $userDetails;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get order details from the order id
     *
     * @param 1: $orderId
     *
     * @return : Order Details (Order Master Object)
     *
     * @throws Exception : Null
     *
     **/
    public function getOrderDetailsById($orderId)
    {
        $orderDetail = $this->orderManager->getOrderDetailById($orderId);
        
        if ($orderDetail == null) {
            //throw new NotFoundHttpException($this->translator->trans('feedback.product_not_found'));
            $orderDetail['order_not_found'] = $this->translator->trans('feedback.order_not_found');
        }
        unset($orderId);
        return $orderDetail;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to send emails
     *
     * @param 1: $eventKey
     * @param 2: $to
     * @param 3: $params
     * @param 4: $languageId
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function sendMail($eventKey, $to, $params, $languageId)
    {
        $this->emailManager->sendMail($eventKey, $to, $params, $languageId);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to convert symfony object into array
     *
     * @param 1: $symfonyObject
     *
     * @return serialized object in json format
     *
     * @throws Exception : Null
     *
     **/
    public function serializeObject($symfonyObject)
    {
        return $this->serializer->serialize($symfonyObject, 'json');
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to insert feedback given by the user into the product master database
     *
     * @param 1: $projectId
     * @param 2: $instanceId
     * @param 3: $productId
     * @param 4: $userDetails
     * @param 5: $feedBackData
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function setFeedBackToProductMaster($projectId, $instanceId, $productId, $userDetails, $feedBackData)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get feedback list given by the user for a product from the product master database
     *
     * @param 1: $projectId
     * @param 2: $instanceId
     * @param 3: $productId
     * @param 4: $page
     * @param 5: $recordsPerPage
     *
     * @return NULL
     *
     * @throws Exception : Null
     *
     **/
    public function getFeedBackListFromProductMaster($productId, $page, $recordsPerPage)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to delete (permenantly) feedback given by the user from the product master database
     *
     * @param 1: $projectId
     * @param 2: $instanceId
     * @param 3: $productId
     * @param 4: $userDetails
     * @param 5: $feedBackData
     *
     * @return Object
     *
     * @throws Exception
     *
     **/
    public function deleteFeedBackFromProductMaster($projectId, $instanceId, $productId, $userDetails, $feedBackData)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to update the ratings to solr
     *
     * @param 1: $productId
     * @param 2: $ratings
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function updateRatingsToSolr($productId, $ratings)
    {
        $update = $this->solrClientManager->createUpdate();
        $doc = $update->createDocument();
        
        //set key same as unique key in schema.xml
        $doc->setKey('id', $this->instanceId."-".$productId);
        // var_dump($doc->getFieldValuesMap('keyword_txt'));exit;
        //set partial update for every field that is given
        $doc->setField('rating_f', $ratings);
        $doc->setFieldModifier('rating_f', 'set');
        
        //add document and commit
        $update->addDocument($doc)->addCommit();
        
        // this executes the query and returns the result
        $result = $this->solrClientManager->update($update);
        
        unset($productId);
        unset($ratings);
        unset($update);
        unset($doc);
        unset($result);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to check if the feedback for this product in an order is given or not
     *
     * @param 1: $orderId
     * @param 2: $productId
     * @param 3: $userId
     *
     * @return Null
     *
     * @throws Exception : Null
     *
     **/
    public function isFeedBackGiven($orderId, $productId = null, $userId = null)
    {
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get product id by SKU
     *
     * @param 1: $productSku
     *
     * @return : Product Details array
     *
     * @throws Exception : Null
     *
     **/
    public function getProductIdBySku($productSku)
    {
        return $this->getProductMasterIdsFromProductMaster($this->projectId, $this->instanceId, $productSku);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get product id by product title
     *
     * @param 1: $productTitle
     *
     * @return : Product Details array
     *
     * @throws Exception : Null
     *
     **/
    public function getProductIdByTitle($productTitle)
    {
        return $this->getProductMasterIdsFromProductMaster($this->projectId, $this->instanceId, null, $productTitle);
    }
    
    /**
     * @author Employee Id: 5069
     *
     * Description : This function is used to get product id by productslug
     *
     * @param 1: $productSlug
     *
     * @return : Product Details array
     *
     * @throws Exception : Null
     *
     **/
    public function getProductIdByslug($productSlug)
    {
    	return $this->getProductMasterIdsFromProductMaster($this->projectId, $this->instanceId, null, $productSlug);
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get user id matching the user name.
     *
     * @param 1: $productId
     *
     * @return : Product Details array
     *
     * @throws Exception : Null
     *
     **/
    public function getUserIdByName($userName)
    {
        $userId = $this->userManager->getUserIdByName($userName);
        if (empty($userId)) {
            $userId = array(0);
        }
        return $userId;
    }
    
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to get product id from product master table from api
     *
     * @param 1: $projectId
     * @param 2: $instanceId
     * @param 3: $productSku
     * @param 4: $productTitle
     *
     * @return array of product ids
     *
     * @throws Exception : Null
     *
     **/
    public function getProductMasterIdsFromProductMaster($projectId, $instanceId, $productSku = null, $productTitle = null,$productSlug = null)
    {
        $params = array(
            'project_id' => $projectId,
            'instance_id' => $instanceId,
            'product_sku' => $productSku,
            'product_title' => $productTitle,
        	'product_slug' => $productSlug,
            'language' => $this->locale
        );
    
        $this->feedbackApi->init('get_product_id');
        $this->feedbackApi->setCallParameters($params);
        $this->feedbackApi->doProcess();
    
        $returnData = null;
    
        if ($this->feedbackApi->isSuccess()) {
            $returnData = $this->feedbackApi->getResultArray();
        }
        
        unset($projectId);
        unset($instanceId);
        unset($productSku);
        unset($productTitle);
        unset($productSlug);
        unset($params);
    
        return $returnData;
    }
}
