<?php
namespace OfficeBrain\Bundle\ProductBundle\ApiClient;

use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use OfficeBrain\Bundle\ApiClientBundle\ApiClient\AbstractApiClient;
use OfficeBrain\Bundle\ProductBundle\Helper\CacheAPIProductHelper;

class ProductApiClient extends AbstractApiClient
{
    
    public function doProcess()
    {
        $cacheObj = new CacheAPIProductHelper();
        $cacheStatus=false;
        switch ($this->apiCallType) {
            case 'view_product':
            	  //$this->trace = true;
                $this->apiCallUrl = 'product';
                $cacheStatus=$cacheObj->viewProduct($this);
                break;
            case 'authenticate_access':
            	$this->apiCallUrl = 'supplier-signature';
            	break;
            case 'view_product_admin': //ADDED SEPERATLEY AS WE DON'T WANT TO RUN SERVICE RULE IN ADMIN
               // $this->trace = true;
            	$this->apiCallUrl = 'product';
                      break;
            case 'view_dist_product_admin':
            	// $this->trace = true;
                $this->apiCallUrl = 'product';
                break;
            case 'supplier_count':
                $this->apiCallUrl = 'product/supplier/count';
                break;
            case 'supplier_product_list':
                $this->apiCallUrl = 'product/virtual';
                break;
            case 'list_product':
//                 $this->trace = true;
                $this->apiCallUrl = 'product/list';
                break;
            case 'product_image':
                $this->apiCallUrl='product-image';
                break;
            case 'add_product_image':
            	//$this->post = true;
            	$this->apiCallUrl = 'upload-product-image-library';
            	$this->uploadFileAndCallApi();
            	break;
            case 'remove_product_images':
            	$this->apiCallUrl = 'remove-product-images';
            	break;
            case 'add_product':
				//$this->trace = true;
            	$this->post = true;
            	$this->apiCallUrl = 'product/add';
            	$this->callApi();
            	break;
            case 'edit_product_info':
//             	$this->trace = true;
            	$this->apiCallUrl = 'product/edit/data';
            	break;
            case 'related_product':
            	$this->apiCallUrl = 'product/related-product';
            	break;
            case 'recommended_product':
            	$this->apiCallUrl = 'product/recommended-product';
            	break;
            case 'delete_product':
//             	$this->trace = true;
            	$this->apiCallUrl = 'product/delete';
            	break;
            case 'change_product_status':
            	$this->apiCallUrl = 'product/changestatus';
            	break;
            case 'get_supplier_name':
            	$this->apiCallUrl = 'product/supplier/name';
            	break;
            case 'update_product_group_price':
            	$this->apiCallUrl = 'product/user-group-price/update';
            	break;
            case 'product_country_language':
            	$this->apiCallUrl = 'product/country/language';
            	break;
            case 'check_duplicate_sku':
            	$this->apiCallUrl = 'product/check-duplicate-sku';
            	break;
            case 'get_matrix_type':
                $this->apiCallUrl = 'product/matrix-type';
                break;
            case 'get_matrix_list':
                $this->apiCallUrl = 'product/matrix/list';
                break;
            case 'pms_color':
            	$this->apiCallUrl = 'pmscolor';
            	break;
            case 'product_inventory_lookup':
            	$this->apiCallUrl='http://test.officebeacon.com/AakronAPI/api/inventorydetails?';
            	return $this->callProductInventoryApi();
            	break;
            case 'pms_hex_code':
            	$this->apiCallUrl = 'getpmsofhexcode';
            	break;
            case 'copy_product':
            	//$this->trace = true;
            	$this->apiCallUrl = 'product/copy';
            	break;
            case 'export_product_data':
            	$this->apiCallUrl = 'product/export-product-country-culture-wise';
            	break;
            case 'upload_import_file':
            	$this->post = true;
            	$this->apiCallUrl = 'product-import-api/upload-import-file';
            	$this->uploadFileAndCallApi();
            	break;
            case 'check_import_status':
            	//$this->trace = true;
            	$this->apiCallUrl = 'product-import-api/check-import-status';
            	break;
            case 'import_error_log':
            	//$this->trace = true;
            	$this->apiCallUrl = 'product-import-api/import-error-log';
            	break;
            case 'get_all_product_category_mapping':
            	$this->apiCallUrl ='product/category-mapping';
            	break;
            case 'get_category_by_slug':
                //$this->trace = true;
                $this->apiCallUrl ='category/info-by-slug';
                break;
            default:
                break;
        }
        
        
        if(!$cacheStatus)
        {
            if(!$this->post) {
            	$this->callApi();
            }

			if(in_array($this->apiCallType, array('view_product', 'view_product_admin', 'view_dist_product_admin', 'add_product', 'related_product', 'recommended_product', 'update_product_group_price', 'export_product_data', 'supplier_product_list')) && $this->isSuccess()){
	        	//$this->result_set = $this->container->get("office_brain_product_detail_manager")->updateCurrencyByCountry($this->result_set);
	        	//$this->result_set = $this->container->get("office_brain_product_detail_manager")->applyPriceMatrix($this->result_set);
				// print_r($this->result_set);exit;
				
	        	$this->result_set = $this->container->get("office_brain_product_detail_manager_custom")->updateMaskingSkuNumber($this->result_set);
	        	$this->result_set_for_cacheObj = $this->result_set;
	        }
            
            $this->result_set = $this->container->get("office_brain_service_group_rule_manager")->addmanualSpecialPricingNotExpired($this->result_set);
            /*
             * Modified By EmpId: 4589
             * Replace price with rule price if any
             */
            if(($this->apiCallType == "view_product" || $this->apiCallType == "supplier_product_list" || $this->apiCallType == "view_product_admin")){
            	//print_r($this->errorDescription);
            	if($this->isSuccess()){
            		$defaultInstance = strtolower($this->container->getParameter('default_main_instance_type'));
    
            		//OVERRIDING PRODUCT DATA WITH DISTRIBUTOR'S UPDATED DATA 
    				if($defaultInstance=="distributor" && $this->apiCallType != "supplier_product_list"){
            			$this->result_set = $this->container->get("officebrain_distributor_admin_manager")->distributorView($this->result_set);
            		}
            	}
            }else if($this->apiCallType == "list_product"){
            	//$this->result_set = $this->serviceRuleManager->updatePriceWithRule($this->result_set,$this->getCallParameters());
            }
            /*
             * END EmpId: 4589
            */
            if($cacheObj->getProductCachePartialMergeFlag())
            {
                $cacheObj->updatePartialResultSet($this);
            }
        }
        $applyDiscount = (isset($this->getCallParameters()['applyDiscount']) && $this->getCallParameters()['applyDiscount'] == 'no') ? false : true;
        
     	//   echo "<br/>Flag: ".$applyDiscount;exit;
        if($applyDiscount && $this->apiCallType != 'view_product_admin')
        {
        	$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->addmanualSpecialPricingNotExpired($this->result_set);
        	$parameters = $this->getCallParameters();
        	$isFromSolr = (isset($parameters['is_solr']) && $parameters['is_solr'] == 1) ? true : false;
// 	        if($isFromSolr)
// 	        {
// 	        	$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->insertVariationPricingForOtherCountry($this->result_set,$this->getCallParameters());
// 	        }
			$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->insertVariationPricingForOtherCountry($this->result_set,$parameters);
			$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->decideFreeShipping($this->result_set);
			
			if($this->apiCallType != "view_product_admin"){
	        	$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->updatePriceWithRule($this->result_set,$parameters);
	        }
	        
	        if(($this->apiCallType == "view_product" || $this->apiCallType == "supplier_product_list" || $this->apiCallType == "view_product_admin"))
	        {
	        	if(!$isFromSolr)
	        	{
		        	$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->updateCountryByVariation($this->result_set);
					$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->updatePriceByCountry($this->result_set);
					$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->updateMinMaxParametersForVariations($this->result_set);				
	        	}
	        	
	        	$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->updateAsLowAsPrice($this->result_set);
// 	        	if (!empty($this->container->getParameter('sample_product'))) {
// 	        		$sampleProduct = $this->container->getParameter('sample_product');
// 	        		$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->samplePrice($this->result_set,$this->container->getParameter('sample_product'));
// 	        	}

				//$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->updateVariationPriceByCountry($this->result_set);
	        }
	        
	        if(!$isFromSolr)
	        {
	        	//$this->result_set = $this->container->get("office_brain_service_group_rule_manager")->updatePriceByCountryVariation($this->result_set);
	        }
	        //dump($this->result_set);exit;
	        
        }
        //echo "<pre>";print_r($this->result_set);exit;
        //dump($this->result_set);exit;
    }
}
