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

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;

/**
 * 4488
 *
*/
abstract class CompanyAbstract
{
	protected $entityManager;
    protected $session;
    protected $request;
    protected $paginatorManager;
    
    /**
     * @author Employee Id: 4493
     * Initialize the required variable on class load
     *
     */
    public function __construct($entityManager, $request, $sessionManager,$paginatorManager,$company_absolute_path,$company_relative_path)
    {
        $this->company_absolute_path = $company_absolute_path;
        $this->company_relative_path = $company_relative_path;
        $this->entityManager = $entityManager;
        $this->request= $request;
        $this->paginatorManager = $paginatorManager;
        $this->sessionManager = $sessionManager;
        $this->paginatorManager = $paginatorManager;
        $this->projectSetting = $this->request->get('project_setting');
        $this->projectUserSetting = $this->request->get('project_user_setting');
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->companyLogoParameter=$this->projectSetting['configuration_rule']['user']['company']['company_logo_upload_parameter'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->companyRepositoryManager = $this->entityManager->getRepository('OfficeBrainCompanyBundle:Company');
        $this->companyRepositoryManager->setProjectSetting($this->projectSetting);
        $this->companyRepositoryManager->setProjectUserSetting($this->projectUserSetting);
        $this->companyRepositoryManager->setPaginatorService($this->paginatorManager);
    }
    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - company
     * Function created for add company
     */
    
    public function addCompany($companyData,$logoUrl='',$fromerp='0')
    {
        $this->company=$this->companyRepositoryManager->saveCompany($companyData,$logoUrl,$fromerp);
    	if ($this->company) {
    		return array('status'=>'SUCCESS','data'=>$this->company);
    	}
    	return array('status'=>'FAIL');
    }
    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - company
     * Function created for check exist company
     */
    
    public function isCompanyExists($form)
    {	
    	return $this->companyExist =$this->companyRepositoryManager->checkCompanyExists($form);
    }
    
    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - company
     * Function created for edit company
     */
    public function editCompany($companyData,$logoUrl='',$fromerp='0')
    {
        $this->company=$this->companyRepositoryManager->editCompany($companyData,$logoUrl,$fromerp);
    	if ($this->company) {
    		return array('status'=>'SUCCESS','data'=>$this->company);
    	}
    	return array('status'=>'FAIL');
    }
  
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - company
     * Function created for get all company List
     */
    public function getCompanyList($userId,$companyCountFlag, $searchParameterArray, $page, $recordsPerPage, $status='',$companyId='')
    {
    	$this->companyData = $this->entityManager->getRepository("OfficeBrainCompanyBundle:Company")->getCompanyList($userId,$companyCountFlag, $searchParameterArray, $page, $recordsPerPage, $status, $companyId);
    	return $this->companyData ;
    }
    
    
    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - company
     * Function created for delete company
     */
    public function deleteCompany($companyId)
    {
    	$isDeletObj=$this->companyRepositoryManager->isDelete($companyId);
    	if (count($isDeletObj) <= 0) {
    		$companyObj=$this->companyRepositoryManager->deleteCompany($companyId);
    		if ($companyObj) {
    			return array('status'=>'SUCCESS','data'=>$companyObj);
    		} else {
    			return array('status'=>'FAIL');
    		}
    	}
    	return array('status'=>'ISEXIST');
    }
    
    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - company
     * Function created for get company by id(single record)
     */
    public function getCompanyListById($companyId)
    {
    	$companyData=$this->companyRepositoryManager->find($companyId);
    	return $companyData;
    }
    
    
    /**
     * @author Employee Id: 4493
     * OB eCommerce Product - company
     * Function created for update status
     */
    public function updateStatus($companyId, $status)
    {
    	$companyObj = $this->getCompanyListById($companyId);
    	$companyObj->setStatus($status);
    	$resultarray = $this->companyRepositoryManager->updateStatus($companyObj);
    	return $resultarray;
    }
    
    /**
     * @author Employee Id: 4834
     * OB eCommerce Product - company
     * Function created for save image from base64 data
     */
    public function saveCompanyLogo($data,$location)
    {
        
        try{
            if($data->getCompanyLogoUrl()!="")
            {
                $img_name = basename($data->getCompanyLogoUrl());
                $url = $data->getCompanyLogoUrl();
                $saveto = $this->company_absolute_path.$location.'/'.$img_name;
                //file_put_contents($this->request->getScheme().'://'.$this->request->getHttpHost().$this->request->getBasePath().'/images/uploads/'.$location./'.$data->getCompanyLogo(), $content);
                if($data->getCompanyLogo()!="")
                {
                    if(file_exists($this->company_absolute_path.$location.'/'.$img_name))
                    {
                        unlink($this->company_absolute_path.$location.'/'.$img_name);
                    }
                    
                    system("wget -q \"$url\" -O $saveto");
                    $data->setCompanyLogo($img_name);
                    $data->setCompanyLogoUrl($this->company_relative_path.'/'.$img_name);
                    //file_put_contents($this->company_absolute_path.$location.'/'.$img_name, $image);
                }else{
                    system("wget -q \"$url\" -O $saveto");
                    $data->setCompanyLogo($img_name);
                    $data->setCompanyLogoUrl($this->company_relative_path.'/'.$img_name);
                    //$name =rand(5,10).'.jpg';
                    //file_put_contents($this->company_absolute_path.$location."/".$name, $content);
                }
            }
        }catch (\Exception $e) {
               // return array('exception' => 1, 'status' => 'Error', 'message' => $e->getMessage().' Error in Run Save company logo queue...');
               echo $e->getMessage();die;
        }
    }
}
