<?php

namespace Libraries;

class ServiceLibrary {

	public function __construct( $objEmail ) {
		
		$this->email = $objEmail;
    }

    public function sendRequest( $strUrl, $strMethod, $arrmixRequestData ) {
		
		$objCurlInit = curl_init();
		curl_setopt( $objCurlInit, CURLOPT_URL, $strUrl );
        curl_setopt( $objCurlInit, CURLOPT_RETURNTRANSFER, true );

		if( 'post' == $strMethod ) {
			curl_setopt( $objCurlInit, CURLOPT_POST, true );
        	curl_setopt( $objCurlInit, CURLOPT_POSTFIELDS, $arrmixRequestData );
		}
        
        //Ignore SSL certificate verification
        curl_setopt( $objCurlInit, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $objCurlInit, CURLOPT_SSL_VERIFYPEER, 0);

        $strResponse = curl_exec( $objCurlInit );
        curl_close( $objCurlInit );
        
		$arrmixResponseData = json_decode( $strResponse, true );

		return $arrmixResponseData;
	}

    protected function sendSms( $intMobileNumber, $strTextMessage ) {
		
		$strTextMessage = urlencode( $strTextMessage );

		//Prepare you post parameters
		$arrmixSmsDetails = [
			'authkey' => SMS_AUTH_KEY,
			'mobiles' => $intMobileNumber,
			'message' => $strTextMessage,
			'sender'  => SMS_SENDER_ID,
			'route'   => 1
		];
			
		$arrmixResponse = $this->sendRequest( SMS_API_URL, 'post', $arrmixSmsDetails );

		return $arrmixResponse;
	}

    public function sendEmail( $strTo, $strSubject, $strMessage, $intEmailUserType = 1, $strAttachmentUrl = '' ) {
		
		$arrmixConfig = [];
		
		$arrmixClientEmailSettingDetails = getEmailSettingDetails( $intEmailUserType );

		$arrmixConfig['SMTPHost'] = $arrmixClientEmailSettingDetails['host'];
		$arrmixConfig['SMTPPort'] = $arrmixClientEmailSettingDetails['port'];
		$arrmixConfig['SMTPUser'] = $arrmixClientEmailSettingDetails['username'];
		$arrmixConfig['SMTPPass'] = $arrmixClientEmailSettingDetails['password'];
		$arrmixConfig['protocol'] = $arrmixClientEmailSettingDetails['protocol'];

		$arrmixConfig['SMTPCrypto'] = 'ssl';
		$arrmixConfig['mailType']   = 'html';
		$arrmixConfig['charset']    = 'utf-8';
		$arrmixConfig['wordWrap']   = true;
		$arrmixConfig['newline']    = "\r\n";
		 
		$this->email->initialize( $arrmixConfig );
		$this->email->setTo( $strTo );
		$this->email->setFrom( $arrmixClientEmailSettingDetails['username'], COMPANY_NAME );
		$this->email->setSubject( $strSubject );
		$this->email->setMessage( $strMessage );
		
		if( true == isVal( $strAttachmentUrl ) ) {
			$this->email->attach( $strAttachmentUrl );
		}

		if( $this->email->send() ) {
			$arrmixResponse['success'] = true;
			$arrmixResponse['message'] = "Email has been sent Successfully";
		} else {
			if( ENVIRONMENT_DEVELOPMENT == getEnvironment() )  {
				// printDie( $this->email->printDebugger() );
			} 
			$arrmixResponse['success'] = false;
			$arrmixResponse['message'] = "Unable to send the mail.";
		}
		
		return $arrmixResponse;
	}

    public function logger( $strMessage, $strFilePathWithName = '' ) {
    
		$strMessage = '[' . date( 'Y-m-d H:i:s' ) . '] : ' . $strMessage . PHP_EOL;
		
		if( true == isVal( $strFilePathWithName ) ) {
			$strFileName = LOG_PATH . $strFilePathWithName . '.txt';
		} else {
			$strFileName = LOG_PATH . 'general/' . replaceDateTimeDashFormatToUnderscore( date( 'Y-m-d' ) ) . '.txt';
		}
        
		$objFileHandleList = fopen( $strFileName, 'a+' );

		/*if( true == file_exists( $strFileName ) ) {
			chown( $strFileName, LINUX_USER_GROUP );
		}*/
        
        fwrite( $objFileHandleList, $strMessage );
    }

	public function updateConfigFile( $strConfigJsonData, $strConfigFileName = '' ) {

		if( false == \isVal( $strConfigFileName ) ) {
			$strConfigFileName = TEMP_CONFIG_FILE_NAME;	
		}
		
		$strConfigFileFullPath = CONFIG_FILE_PATH . $strConfigFileName;
		
		$objConfigFileHandleList = fopen( $strConfigFileFullPath, 'w+' );

		fwrite( $objConfigFileHandleList, $strConfigJsonData );
	}

	public function getConfigFile( $strConfigFileName = '' ) {

		if( false == \isVal( $strConfigFileName ) ) {
			$strConfigFileName = CONFIG_FILE_NAME;	
		}
		
		$strConfigFileFullPath = CONFIG_FILE_PATH . $strConfigFileName;

		$objConfigFileHandleList = fopen( $strConfigFileFullPath, "r" );//open file in read mode    
  
		$strConfigFileData = fread( $objConfigFileHandleList, filesize( $strConfigFileFullPath ) );//read file    
		
		fclose( $objConfigFileHandleList );//close file    

		return $strConfigFileData;
	}

	public function checkConfigFilePresentOrNot( $strConfigFileName ) {

		if( false == isVal( $strConfigFileName ) ) {
			return false;
		}

		$strTempConfigFileFullPath = CONFIG_FILE_PATH . $strConfigFileName;

		if( true == file_exists( $strTempConfigFileFullPath ) ) {
			return true;
		}

		return false;
	}
 
}