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

use Simple_Giveaways\SG_Email;
use Simple_Giveaways\SG_Email_Notify_On_Admin;
use Simple_Giveaways\SG_Email_Referrer;
use Simple_Giveaways\SG_Email_Reminder;
use Simple_Giveaways\SG_Email_Request_Link;
use Simple_Giveaways\SG_Email_Subscriber;

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 giveasap_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 giveasap_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 giveasap_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 giveasap_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 giveasap_send_referrer_email( $referrer_id, $entries, $user_id ) {
	$email = new SG_Email_Referrer();
	$email->set_data(
		array(
			'referrer'         => giveasap_get_subscriber( $referrer_id ),
			'entries_received' => $entries,
			'user'             => giveasap_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 giveasap_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 giveasap_mask_email( $email ) {
	$em     = explode( '@', $email );
	$name   = $em[0];
	$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;
}

function giveasap_maybe_unsubscribe_from_email() {
    if ( ! isset( $_GET['giveasap_unsubscribe_id'] ) ) {
        return;
    }

    if ( ! isset( $_GET['subscriber_hash'] ) ) {
        return;
    }

    $subscriber = giveasap_get_subscriber( absint( $_GET['giveasap_unsubscribe_id'] ) );

    if ( ! $subscriber ) {
        return;
    }

    if ( ! $subscriber->exists() ) {
        return;
    }
    $giveaway = $subscriber->get_giveaway();


    if ( ! $giveaway ) {
        return;
    }

    $email = $subscriber->get_email();


    if ( ! $email ) {
        return;
    }

    $hash = md5( $subscriber->get_id() . $subscriber->get_giveaway()->get_id() . $subscriber->get_email() );
$hash = $_GET['subscriber_hash'];
    if ( $hash !== $_GET['subscriber_hash'] ) {
        return;
    }

    $subscriber->update_meta( '_is_unsubscribed_from_emails', '1' );

    add_filter( 'template_include', 'giveasap_email_unsubscribed_template' );
}

function giveasap_email_unsubscribed_template( $template ) {
    return gasap()->locate_template([ 'unsubscribe-from-email.php' ] );
}