<?php
/**
 * Simple Giveaways Reminder Emails that is sent if we want to remind a subscriber
 */
namespace Simple_Giveaways;

class SG_Email_Reminder extends SG_Email {

	/**
	 * @var null
	 */
	protected $subscriber = null;

	/**
	 * @var null
	 */
	protected $giveaway = null;

    protected $can_unsubscribe = true;

	/**
	 * Get the Subscriber data.
	 *
	 * @return array|null|string|SG_Subscriber
	 */
	public function get_subscriber() {
		if ( null === $this->subscriber ) {
			$subscriber_id = isset( $this->data['subscriber_id'] ) ? absint( $this->data['subscriber_id'] ) : '';

			if ( ! $subscriber_id ) {
				return '';
			}

			$this->subscriber = giveasap_get_subscriber( $subscriber_id );
		}

		return $this->subscriber;
	}

	/**
	 * Get the Giveaway data.
	 *
	 * @return array|null|string|SG_Giveaway
	 */
	public function get_giveaway() {
		if ( null === $this->giveaway ) {
			$giveaway_id = isset( $this->data['giveaway_id'] ) ? absint( $this->data['giveaway_id'] ) : '';

			if ( ! $giveaway_id ) {
				return '';
			}

			$this->giveaway = giveasap_get_giveaway( $giveaway_id );
		}

		return $this->giveaway;
	}

	/**
	 * Get remaining days
	 *
	 * @return float|int
	 */
	protected function get_remaining_days() {
		$giveaway = $this->get_giveaway();
		$schedule = $giveaway->get_schedule();
		$end_time = $schedule->get_end_timestamp();
		$diff     = $end_time - time();

		if ( $diff > 0 ) {
			return ceil( $diff / DAY_IN_SECONDS );
		}

		return 0;
	}

	/**
	 * Process the Email.
	 * This is method is used for custom email types to send their custom emails.
	 * This method should utilize the send() method.
	 *
	 * @since 2.18.0
	 *
	 * @return mixed
	 */
	public function process() {
		$subscriber = $this->get_subscriber();

		if ( ! $subscriber ) {
			return new \WP_Error( 'no-subscriber', __( 'There is no subscriber registered with that ID', 'giveasap' ) );
		}

		$settings = giveasap_get_settings();

		$subscriber_email = isset( $settings['reminder_email'] ) ? $settings['reminder_email'] : __( 'Hi {{USER_EMAIL}},<br/>The giveaway "{{TITLE}}" is ending in {{DAYS}} days.<br/>Your currently have {{ENTRIES_TOTAL}} entries and if you want to earn more, you can check the giveaway at {{ENTRY_LINK}}', 'giveasap' );
		$email_subject    = isset( $settings['reminder_email_subject'] ) ? $settings['reminder_email_subject'] : __( 'Hello, {{TITLE}} ends in {{DAYS}} days', 'giveasap' );

		if ( ! $subscriber_email ) {
			return new \WP_Error( 'no-email-text', __( 'There is no Reminder Email set.', 'giveasap' ) );
		}

		if ( ! $email_subject ) {
			return false;
		}

		$email     = $subscriber->get_email();
		$days      = $this->get_remaining_days();
		$title     = html_entity_decode( get_the_title( $subscriber->get_giveaway_id() ) );
		$permalink = $subscriber->get_subscribed_from_page();
		if ( ! $permalink ) {
			$giveaway  = $subscriber->get_giveaway();
			$permalink = $giveaway ? $giveaway->get_main_page_link() : get_permalink( $subscriber->get_giveaway_id() );
		}
		$entry_link       = add_query_arg( 'ref', $subscriber->get_ref_id(), $permalink );
		$share_link       = add_query_arg( 'share', $subscriber->get_id(), $permalink );
		$email_subject    = str_replace( '{{TITLE}}', $title, $email_subject );
		$email_subject    = str_replace( '{{DAYS}}', $days, $email_subject );
		$subscriber_email = str_replace( '{{TITLE}}', $title, $subscriber_email );
		$subscriber_email = str_replace( '{{DAYS}}', $days, $subscriber_email );
		$subscriber_email = str_replace( '{{ENTRIES_TOTAL}}', $subscriber->get_entries(), $subscriber_email );
		$subscriber_email = str_replace( '{{USER_EMAIL}}', $email, $subscriber_email );
		$subscriber_email = str_replace( '{{ENTRY_LINK}}', $entry_link, $subscriber_email );
		$subscriber_email = str_replace( '{{SHARE_LINK}}', $share_link, $subscriber_email );

		$form_fields = giveasap_get_form_fields( $subscriber->get_giveaway_id() );
		if ( $form_fields ) {
			foreach ( $form_fields as $field_key => $field_config ) {
				$form_value = giveasap_get_meta( $subscriber->get_id(), '_' . $field_key, true );
				if ( ! $form_value ) {
					$form_value = '';
				}

				$email_subject = str_replace( '{{FORM_' . strtoupper( $field_key ) . '}}', $form_value, $email_subject );

				$subscriber_email = str_replace( '{{FORM_' . strtoupper( $field_key ) . '}}', $form_value, $subscriber_email );
			}
		}

		return $this->send( $email, $email_subject, $subscriber_email );
	}

    public function can_send() {
        $subscriber   = $this->get_subscriber();
        $unsubscribed = $subscriber->is_unsubscribed_from_emails();

        if ( $unsubscribed ) {
            return false;
        }

        return parent::can_send();
    }
}
