<?php
/**
 * All email facing functions
 */
namespace Codexpert\WC_Affiliate;

use Codexpert\Plugin\Base;
use EasyCommerce\Helpers\Email as EasyEmail;

/**
 * if accessed directly, exit.
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @package Plugin
 * @subpackage Email
 * @author codexpert <hello@codexpert.io>
 */
class Email extends Base {

	public $plugin;

	/**
	 * Constructor function
	 */
	public function __construct( $plugin ) {
		$this->plugin = $plugin;
	}

	public function send( $to, $subject, $message, $attachments = '', $headers = "Content-Type: text/html\r\n" ) {

		$wc_email = new \WC_Emails();
		$_message = stripslashes( $message );

		ob_start();
		$wc_email->email_header( $subject );
		echo wp_kses_post( $_message );
		$wc_email->email_footer();
		$message = ob_get_clean();

		$wc_email->send( sanitize_email( $to ), sanitize_text_field( strip_tags( $subject ) ), wpautop( $message ), $headers, $attachments );
	}

	public function affiliate_applied_affiliate( $user_id ) {
		$enable              = Helper::get_option( 'wc_affiliate_email', 'affiliate_applied_enable' );
		$enable_email_verify = Helper::get_option( 'wc_affiliate_basic', 'enable_email_validation' );
		$user                = get_userdata( $user_id );
		$platform 			 = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$email   			 = $user->user_email;
		$first_name 		 = $user->first_name;
		$site_name  		 = get_bloginfo( 'name' );
		$footer 			 = '<p style="color:#999;">Thank you for using our service.</p>';
		$subject 			 = __( 'Email Verification', 'wc-affiliate' );
		$dashboard_id    	 = Helper::get_option( 'wc_affiliate_basic', 'dashboard' );
		$dashboard_url   	 = get_the_permalink( $dashboard_id );
		$validation_data 	 = json_encode([
			'id'    => $user->ID,
			'email' => $user->user_email,
			'time'  => time(),
		]);
		$validation_data 	 = Helper::ncrypt()->encrypt( $validation_data );
		$verify_url      	 = add_query_arg( 'validate', $validation_data, $dashboard_url );
		$message         	 = __( 'Verify your email: ', 'wc-affiliate' ) . $verify_url;

		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $message, $footer );
		} else {
			$this->send( $email, $subject, $message );
		}
	
		if ( empty( $enable ) || $enable !== 'on' ) {
			return;
		}
	
		$_subject = Helper::get_option( 'wc_affiliate_email', 'affiliate_applied_subject' );
		$subject  = str_replace( '%%site_title%%', $site_name, $_subject );
	
		$_message = Helper::get_option( 'wc_affiliate_email', 'affiliate_applied_message' );
		$message  = str_replace( '%%first_name%%', $first_name, $_message );
		$message  = wpautop( $message );
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $message, $footer );
		} else {
			$this->send( $email, $subject, $message );
		}
	}	
	

	public function affiliate_applied_admin( $user_id ) {

		$enable = Helper::get_option( 'wc_affiliate_email', 'affiliate_applied_admin_enable' );
		if ( empty( $enable ) || $enable !== 'on' ) {
			return;
		}
	
		$platform = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$email    = wc_affiliate_admin_email();
		$user     = get_userdata( $user_id );
	
		$first_name = $user->first_name;
		$_site_url  = get_bloginfo( 'url' );
		$site_name  = get_bloginfo( 'name' );
		$site_url   = '<a href="' . esc_url( $_site_url ) . '">' . esc_html( $site_name ) . '</a>';
		$admin_url  = admin_url( 'admin.php?page=affiliates' );
		$_user_url  = add_query_arg( 'affiliate', $user_id, $admin_url );
		$user_url   = '<a href="' . esc_url( $_user_url ) . '">' . esc_url( $_user_url ) . '</a>';
	
		$subject  = Helper::get_option( 'wc_affiliate_email', 'affiliate_applied_admin_subject' );
		$message  = Helper::get_option( 'wc_affiliate_email', 'affiliate_applied_admin_message' );
		$message  = str_replace(
			array( '%%first_name%%', '%%site_url%%', '%%user_url%%' ),
			array( $first_name, $site_url, $user_url ),
			$message
		);
		$message  = wpautop( $message );
		$footer   = '<p style="color:#999;">This email was sent from ' . esc_html( $site_name ) . '.</p>';
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $message, $footer );
		} else {
			$this->send( $email, $subject, $message );
		}
	}	

	public function account_review_affiliate( $user_id, $review_action, $message ) {

		$user       = get_userdata( $user_id );
		$email      = $user->user_email;
		$first_name = $user->first_name;
		$platform 	= function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$site_name 	= get_bloginfo( 'name' );
		$footer    	= '<p style="color:#999;">Thank you for being a part of ' . esc_html( $site_name ) . '.</p>';

		if ( $review_action == 'approve' ) {

			$enable = Helper::get_option( 'wc_affiliate_email', 'affiliate_approved_enable' );

			if ( ! empty( $enable ) && $enable == 'on' ) {
				$subject  = Helper::get_option( 'wc_affiliate_email', 'account_approved_subject' );
				$_message = Helper::get_option( 'wc_affiliate_email', 'account_approved_message' );

				$rep_message = str_replace( array( '%%first_name%%', '%%message%%' ), array( $first_name, $message ), $_message );
				$rep_message = wpautop( $rep_message );

				if ( $platform === 'easycommerce' ) {
					$ec_mail = new EasyEmail();
					$ec_mail->send( $email, $subject, $rep_message, $footer );
				} else {
					$this->send( $email, $subject, $rep_message );
				}
			}
		} elseif ( $review_action == 'reject' ) {
			$enable = Helper::get_option( 'wc_affiliate_email', 'account_reject_enable' );

			if ( ! empty( $enable ) && $enable == 'on' ) {
				$subject     = Helper::get_option( 'wc_affiliate_email', 'account_reject_subject' );
				$_message    = Helper::get_option( 'wc_affiliate_email', 'account_reject_message' );
				$rep_message = str_replace( array( '%%first_name%%', '%%message%%' ), array( $first_name, $message ), $_message );
				$rep_message = wpautop( $rep_message );

				if ( $platform === 'easycommerce' ) {
					$ec_mail = new EasyEmail();
					$ec_mail->send( $email, $subject, $rep_message, $footer );
				} else {
					$this->send( $email, $subject, $rep_message );
				}
			}
		}
	}

	public function add_credit_affiliate( $user, $order_id ) {
		$enable = Helper::get_option( 'wc_affiliate_email', 'add_credit_enable' );
	
		if ( empty( $enable ) || $enable !== 'on' ) {
			return;
		}
	
		$email      = $user->user_email;
		$first_name = $user->first_name;
		$currency   = get_wc_currency_symbol();
		$amount     = get_post_meta( $order_id, '_wc_affiliate_credited', true );
	
		$subject     = Helper::get_option( 'wc_affiliate_email', 'add_credit_subject' );
		$_message    = Helper::get_option( 'wc_affiliate_email', 'add_credit_message' );
		$rep_message = str_replace(
			array( '%%first_name%%', '%%amount%%' ),
			array( $first_name, $currency . $amount ),
			$_message
		);
		$rep_message = wpautop( $rep_message );
	
		$platform = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$site_name = get_bloginfo( 'name' );
		$footer = '<p style="color:#999;">Thank you for using ' . esc_html( $site_name ) . '.</p>';
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $rep_message, $footer );
		} else {
			$this->send( $email, $subject, $rep_message );
		}
	}	

	public function add_credit_admin( $user, $order_id ) {
		$enable = Helper::get_option( 'wc_affiliate_email', 'add_credit_admin_enable' );
	
		if ( empty( $enable ) || $enable !== 'on' ) {
			return;
		}
	
		$email = wc_affiliate_admin_email();
		$first_name = $user->first_name;
		$currency = get_wc_currency_symbol();
	
		$amount = get_post_meta( $order_id, '_wc_affiliate_credited', true );
		$_subject = Helper::get_option( 'wc_affiliate_email', 'add_credit_admin_subject' );
		$_message = Helper::get_option( 'wc_affiliate_email', 'add_credit_admin_message' );
	
		$rep_subject = str_replace( array( '%%first_name%%' ), array( $first_name ), $_subject );
		$rep_message = str_replace( array( '%%first_name%%', '%%amount%%' ), array( $first_name, $currency . $amount ), $_message );
		$rep_message = wpautop( $rep_message );
	
		$platform = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$site_name = get_bloginfo( 'name' );
		$footer = '<p style="color:#999;">Thank you for using ' . esc_html( $site_name ) . '.</p>';
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $rep_subject, $rep_message, $footer );
		} else {
			$this->send( $email, $rep_subject, $rep_message );
		}
	}	

	public function request_payout_affiliate( $user_id, $amount ) {

		$enable = Helper::get_option( 'wc_affiliate_email', 'request_payout_enable' );
	
		if ( empty( $enable ) || $enable !== 'on' ) {
			return;
		}
	
		$user       = get_userdata( $user_id );
		$email      = $user->user_email;
		$first_name = $user->first_name;
	
		$subject     = Helper::get_option( 'wc_affiliate_email', 'request_payout_subject' );
		$message     = Helper::get_option( 'wc_affiliate_email', 'request_payout_mail_message' );
		$rep_message = str_replace( array( '%%first_name%%' ), array( $first_name ), $message );
		$rep_message = wpautop( $rep_message );
	
		$platform  = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$site_name = get_bloginfo( 'name' );
		$footer    = '<p style="color:#999;">Thank you for using ' . esc_html( $site_name ) . '.</p>';
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $rep_message, $footer );
		} else {
			$this->send( $email, $subject, $rep_message );
		}
	}	

	public function request_payout_admin( $user_id, $amount ) {

		$enable = Helper::get_option( 'wc_affiliate_email', 'request_payout_admin_enable' );
	
		if ( empty( $enable ) || $enable !== 'on' ) {
			return;
		}
	
		$email = wc_affiliate_admin_email();
		$user  = get_userdata( $user_id );
	
		$first_name = $user->first_name;
		$currency   = get_wc_currency_symbol();
	
		$subject     = Helper::get_option( 'wc_affiliate_email', 'request_payout_admin_subject' );
		$message     = Helper::get_option( 'wc_affiliate_email', 'request_payout_admin_message' );
		$rep_message = str_replace(
			[ '%%first_name%%', '%%amount%%' ],
			[ $first_name, $currency . $amount ],
			$message
		);
		$rep_message = wpautop( $rep_message );
	
		$platform  = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$site_name = get_bloginfo( 'name' );
		$footer    = '<p style="color:#999;">Thank you for using ' . esc_html( $site_name ) . '.</p>';
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $rep_message, $footer );
		} else {
			$this->send( $email, $subject, $rep_message );
		}
	}	

	public function payout_processed( $affiliate ) {

		$enable = Helper::get_option( 'wc_affiliate_email', 'payout_process_enable' );
	
		if ( empty( $enable ) || $enable !== 'on' ) {
			return;
		}
	
		$user     = get_userdata( $affiliate );
		$email    = $user->user_email;
		$amount   = Helper::get_user_unpaid_amount( $affiliate );
		$currency = function_exists( 'get_woocommerce_currency_symbol' ) ? get_woocommerce_currency_symbol() : '[currency]';
	
		$subject     = Helper::get_option( 'wc_affiliate_email', 'payout_process_subject' );
		$message     = Helper::get_option( 'wc_affiliate_email', 'payout_process_message' );
		$rep_message = str_replace( [ '%%amount%%' ], [ $currency . $amount ], $message );
		$rep_message = wpautop( $rep_message );
	
		$platform  = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$site_name = get_bloginfo( 'name' );
		$footer    = '<p style="color:#999;">Thank you for using ' . esc_html( $site_name ) . '.</p>';
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $rep_message, $footer );
		} else {
			$this->send( $email, $subject, $rep_message );
		}
	}	

	/**
	 * Resend Varify email to
	 * */
	public function resend_varify_url( $response, $user ) {

		$user    = get_user_by( 'ID', get_current_user_id() );
		$email   = $user->data->user_email;
		$subject = __( 'Email Verification', 'wc-affiliate' );
	
		$dashboard_id    = Helper::get_option( 'wc_affiliate_basic', 'dashboard' );
		$dashboard_url   = get_the_permalink( $dashboard_id );
		$validation_data = array(
			'id'    => $user->ID,
			'email' => $user->data->user_email,
			'time'  => time(),
		);
	
		$validation_data = json_encode( $validation_data );
		$validation_data = Helper::ncrypt()->encrypt( $validation_data );
		$verify_url      = add_query_arg( 'validate', $validation_data, $dashboard_url );
	
		$message = __( 'Verify your email: ', 'wc-affiliate' ) . $verify_url;
		$message = wpautop( $message );
	
		$platform  = function_exists( 'wc_get_platform' ) ? wc_get_platform() : 'woocommerce';
		$site_name = get_bloginfo( 'name' );
		$footer    = '<p style="color:#999;">Thank you for using ' . esc_html( $site_name ) . '.</p>';
	
		if ( $platform === 'easycommerce' ) {
			$ec_mail = new EasyEmail();
			$ec_mail->send( $email, $subject, $message, $footer );
		} else {
			$this->send( $email, $subject, $message );
		}
	
		$response['status']  = 1;
		$response['message'] = __( 'Verification mail was sent. Please check your inbox.', 'wc-affiliate' );
	
		return $response;
	}
}
