<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

class fbbttmEmails
{

	/**
	 * Array of email notification classes
	 *
	 * @var fbbttm_Email[]
	 */
	public $emails = array();


	/**
	 * Constructor for the email class hooks in all emails that can be sent.
	 */

	public function __construct() {
		$this->init();

		// Email Header, Footer and content hooks.
		add_action( 'fbbttm_email_header', array( $this, 'fbbttmm_email_header_set' ) );
		add_action( 'fbbttm_email_footer', array( $this, 'fbbttmm_email_footer_set' ) );
	}


	public function init() {
		// Include email classes.
		//include_once __DIR__ . '/emails/class-wc-email.php';
	}

	/**
	 * Set header for email
	*/ 

	public function fbbttmm_email_header_set(){

		include(FBBTTM_PATH.'/templates/emails/email-header.php');

	}

	/**
	 * Set footer for email
	 */ 
	public function fbbttmm_email_footer_set(){

		include(FBBTTM_PATH.'/templates/emails/email-footer.php');
		
	}

	/**
	 * Set content for mail
	 */ 

	public function fbbttm_set_email_content($contentname=null){

		
 		ob_start();
		

		//$this->setHeader();

		$this->fbbttm_set_email_template($contentname);
		ob_end_clean();
		
	

	}

	/**
	 * Set content for mail
	 */ 

	public function fbbttm_set_email_template($templatename=null)	
	{

		if( file_exists(FBBTTM_DIR . '/helper/'.$templatename.'.php') ){
			require_once FBBTTM_DIR . '/helper/'.$templatename.'.php';
		}

	}

	


	/**
	 * Set function for new account create mail functon 
	 */ 

	public function fbbttm_new_account_createmail($username= null, $useremail=null, $userpass =null)
	{

		global $uname ,$uemail,$fbbttm_upass,$blogname,$fbbttm_checkoutobj;

		ob_start();
		$uname  = $username;
		$blogname = $this->fbbttm_get_blogname();
		$fbbttm_upass = $userpass;
		
		fbbttm_get_template_part('emails/customer-new-account','fooditems');

		$message = ob_get_contents();

		ob_end_clean();

		$email_subject = sprintf(
							/* translators: 1: site info */
					    esc_html__('Your account has been created on %1$s.', 'foodboxbooker'),
					    esc_html($blogname)
					    );

	  //$fbbttm_checkoutobj->fbbttm_get_payment_methodkey('fieldkey','fbbttm_s_sendername','fbbttm_s_sendermail');

		$this->fbbttm_sendemail($useremail,'',$email_subject,$message);

	}


	/**
	 * 
	 * function for cron mail
	 * 
	 */ 

	public function fbbttm_order_renew_remainder($userdata= array(), $packname= null,$lastdate= null, $pagelink=null,$mtype=null)
	{


		global $user_email, $userpackname, $packlastdate, $productpagelink,$username,$fbbttm_messagetype;

		$user_email = $userdata->user_email;
		$username = $userdata->user_nicename;
		
		$userpackname = $packname;
		$packlastdate = $lastdate;
		$productpagelink = $pagelink;
		$blogname = $this->fbbttm_get_blogname();
		$fbbttm_messagetype=$mtype;

		ob_start();
		fbbttm_get_template_part('emails/renewremainder','fooditems');

		$message = ob_get_contents();

		ob_end_clean();

		$title = sprintf(
							/* translators: 1: Site title */
					    esc_html__('[%1$s] Renew remainder.', 'foodboxbooker'),
					    esc_html($blogname)
					    );

		$this->fbbttm_sendemail($user_email,'',$title,$message);

	}



	/**
	 * Public function reset password mail
	 */ 

	public function fbbttm_reset_mail($user_login=null,$hashed=null,$user_id=null,$email=null,$key=null)
	{

		

		global $userlogin ,$fbbttmhashed,$fbbttmuser_id,$fbbttm_blogname;


		
		$userlogin = $user_login;
		$fbbttmhashed = $hashed;
		$fbbttmuser_id = $user_id;
		$blogname = $this->fbbttm_get_blogname();
		ob_start();
		fbbttm_get_template_part('emails/reset-password','fooditems');


		$message = ob_get_contents();

		ob_end_clean();

		$title = sprintf(
							/* translators: 1: Site title */
					    esc_html__('[%1$s] Password Reset.', 'foodboxbooker'),
					    esc_html($blogname)
					    );

		 $title = apply_filters('retrieve_password_title', $title);
		 $message = apply_filters('retrieve_password_message', $message, $key);

	  //$fbbttm_checkoutobj->fbbttm_get_payment_methodkey('fieldkey','fbbttm_s_sendername','fbbttm_s_sendermail');

		$this->fbbttm_sendemail($email,'',$title,$message);

	}


	/**
	 * New order email
	 * 
	 */ 

	public function fbbttm_new_ordermail($orderdata=null){
	
	global $orderdatafull,$fbbttm_settingdata;

	
	$fbbttm_blogname = $this->fbbttm_get_blogname();
	$orderdatafull = $orderdata;

	ob_start();
	fbbttm_get_template_part('emails/new-order','fooditems');

	$message = ob_get_contents();

	ob_end_clean();

	 $title = sprintf(
							/* translators: 1: Site title */
					    esc_html__('Your [%1$s] order has been received!', 'foodboxbooker'),
					    esc_html($fbbttm_blogname)
					    );
		
		$useremail = $orderid ='';	

		if(!empty($orderdatafull)){

	    	$oderinfo = $orderdatafull['orderinfo'];

	    	foreach ($oderinfo as $orderinfodata) {

	    		$useremail = $orderinfodata->user_email;
	    		$orderid = $orderinfodata->fbbttm_fdbid;

	    	}
	    }

		$this->fbbttm_sendemail($useremail,'',$title,$message);

		//$this->fbbttm_new_admin_ordermail($orderdata);
		

	}


	public function fbbttm_new_admin_ordermail($orderdata=null){

		global $orderdatafull,$fbbttm_settingdata;
		$orderdatafull = $orderdata;

		ob_start();
		fbbttm_get_template_part('emails/new-order-admin','fooditems');

		$message = ob_get_contents();

		ob_end_clean();




		$fbbttm_blogname = $this->fbbttm_get_blogname();
		$adminemail = get_option('admin_email');

		
		$useremail = $orderid ='';	

		if(!empty($orderdatafull)){

	    	$oderinfo = $orderdatafull['orderinfo'];

	    	foreach ($oderinfo as $orderinfodata) {

	    		$useremail = $orderinfodata->user_email;
	    		$orderid = $orderinfodata->fbbttm_fdbid;

	    	}
	    }

			

			$title =  sprintf( 
				/* translators: 1: Site title , 2: order id */
				esc_html__('[Order %1$s] New Order #%2$s','foodboxbooker'), esc_html( $fbbttm_blogname ), esc_html( $orderid )) ;

			$this->fbbttm_sendemail($adminemail,'',$title,$message);

	}

	/**
	 *  Set email function 
	 * 
	 */ 

	public function fbbttm_sendemail($toemail=null,$fromemail=null,$email_subject=null,$message= null){

		$headers = array('Content-Type: text/html; charset=UTF-8');
		wp_mail($toemail, $email_subject, $message,$headers);
	}

	/**
	 * Replace placeholder text in strings.
	 *
	 * @since  3.7.0
	 * @param  string $string Email footer text.
	 * @return string         Email footer text with any replacements done.
	 */
	public function fbbttm_replace_placeholders( $string ) {

		

		$domain = wp_parse_url( home_url(), PHP_URL_HOST );

		$blogname =  $this->fbbttm_get_blogname();

		return str_replace(
			array(
				'{site_title}',
				'{site_address}',
				'{site_url}',
				'{fbb}',
				'{fbb}',
			),
			array(
				$blogname,
				$domain,
				$domain,
				'<a href="https://themetechmount.com/" target="_blank">Themetechmount</a>',
				'<a href="https://themetechmount.com/" target="_blank">Themetechmount</a>',
			),
			$string
		);
	}


		/**
	 * Get blog name formatted for emails.
	 *
	 * @return string
	 */
	public function fbbttm_get_blogname() {

		return get_bloginfo();

	}
}
?>