<?php
/**
 * Functions considering emails.
 */

if ( ! defined( 'ABSPATH' ) ) {
	return;
}

/**
 * Sending a Giveaway Email with filtered contents.
 *
 * @since 2.15.0
 *
 * @param string|array $to      Emails
 * @param string       $subject Email Subject.
 * @param string       $message Email Message.
 * @param string|array $headers Headers.
 * @param array        $attachments Attachments.
 *
 * @return boolean
 */
function sg_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
	$email = new SG_Email();
	$ret = $email->send( $to, $subject, $message, $headers, $attachments );
	return $ret;
}

/**
 * Sending a Registered Giveaway Email.
 *
 * @since 2.22.0
 *
 * @param integer $subscriber_id Subscriber ID.
 * @param array   $posted_data   Posted data.
 *
 * @return SG_Email_Notify_On_Admin
 */
function sg_send_notify_on_subscribe_email( $subscriber_id, $posted_data ) {
	$email = new SG_Email_Notify_On_Admin();
	$email->set_data( array( 'subscriber_id' => $subscriber_id, 'posted_data' => $posted_data ) );
	$email->process();
	return $email;
}

/**
 * Sending a Registered Giveaway Email.
 *
 * @since 2.18.0
 *
 * @param integer $subscriber_id Subscriber ID.
 *
 * @return SG_Email_Subscriber
 */
function sg_send_registered_email( $subscriber_id ) {
	$email = new SG_Email_Subscriber();
	$email->set_data( array( 'subscriber_id' => $subscriber_id ) );
	$email->process();
	return $email;
}

/**
 * Sending a Request Link Email.
 *
 * @since 2.18.0
 *
 * @param string $subscriber_email Subscriber Email.
 * @param integer $giveaway_id Giveaway ID
 *
 * @return SG_Email_Request_Link
 */
function sg_send_request_link_email( $subscriber_email, $giveaway_id ) {
	$email = new SG_Email_Request_Link();
	$email->set_data( array( 'subscriber_email' => $subscriber_email, 'giveaway_id' => $giveaway_id ) );
	$email->process();
	return $email;
}

/**
 * Sending a Referrer Email
 *
 * @since 2.19.0
 *
 * @param string $subscriber_email Subscriber Email.
 * @param integer $giveaway_id Giveaway ID
 *
 * @return SG_Email_Referrer
 */
function sg_send_referrer_email( $referrer_id, $entries, $user_id ) {
	$email = new SG_Email_Referrer();
	$email->set_data( array( 'referrer' => sg_get_subscriber( $referrer_id ), 'entries_received' => $entries, 'user' => sg_get_subscriber( $user_id ) ) );
	$email->process();
	return $email;
}

/**
 * Sending a Reminder Email.
 *
 * @since 2.31.0
 *
 * @param string $subscriber_id Subscriber ID.
 * @param integer $giveaway_id Giveaway ID
 *
 * @return SG_Email_Reminder
 */
function sg_send_reminder_email( $subscriber_id, $giveaway_id ) {
	$email = new SG_Email_Reminder();
	$email->set_data( array( 'subscriber_id' => $subscriber_id, 'giveaway_id' => $giveaway_id ) );
	$email->process();
	return $email;
}


/**
 * Mask the email.
 *
 * @since 2.25.0
 *
 * @param $email
 *
 * @return string
 */
function sg_mask_email( $email ) {
	$em   = explode( "@", $email );
	$name = implode( array_slice( $em, 0, count( $em ) - 1 ), '@' );
	$len  = strlen( $name );
	$masked = '';
	if ( strlen( $name ) > 5 ) {
		$len  = floor(strlen( $name ) / 2 );
		$masked .= substr( $name,0, $len );
	} else {
		$masked .= '***';
	}
	$masked .= str_repeat( '*', $len ) . "@" . end( $em );
	return $masked;
}