<?php
/**
 * Simple Giveaways Registered Emails that is sent when a user registers.
 */

class SG_Email_Subscriber extends SG_Email {

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

	/**
	 * @param $subscriber
	 */
	public function set_subscriber( $subscriber ) {
	    if ( is_a( $subscriber, 'SG_Subscriber' ) ) {
		    $this->subscriber = $subscriber;
	    }
    }

	/**
	 * 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 = sg_get_subscriber( $subscriber_id );
		}

		return $this->subscriber;
	}

	/**
	 * Get the content for the registered email.
	 *
	 * @param $content
	 *
	 * @return string
	 */
	public function get_content( $content ) {

		$content .= $this->get_entry_details();

		return parent::get_content( $content );
	}

	/**
     * Return if we should show the email links or not.
     *
	 * @return bool
	 */
	public function hide_email_links() {
		$settings = giveasap_get_settings();

		return isset( $settings['email_hide_links'] ) ? (bool) $settings['email_hide_links'] : false;
    }

	/**
	 * Return all the details about the entry in a table.
	 *
	 * @return string
	 */
	public function get_entry_details() {
		$entry_details = apply_filters( 'sg_email_subscriber_details', array(
			'entry_link' => array(
				'title' => __( 'Entry Link', 'giveasap' ),
				'value' => $this->data['entry_link'],
                'type'  => 'link',
			),
			'activate_link' => array(
				'title' => __( 'Activate Link', 'giveasap' ),
				'value' => $this->data['activate_link'],
				'type'  => 'link',
			),
			'share_link' => array(
				'title' => __( 'Share Link', 'giveasap' ),
				'value' => $this->data['share_link'],
				'type'  => 'link',
			),
		));

		if ( ! giveasap_is_activation_required() && isset( $entry_details['activate_link'] ) ) {
			unset( $entry_details['activate_link'] );
		}

        if ( $this->hide_email_links() ) {
		    if ( isset( $entry_details['entry_link'] ) ) {
			    unset( $entry_details['entry_link'] );
            }

	        if ( isset( $entry_details['activate_link'] ) ) {
		        unset( $entry_details['activate_link'] );
	        }

	        if ( isset( $entry_details['share_link'] ) ) {
		        unset( $entry_details['share_link'] );
	        }
        } else {
		    $subscriber = $this->get_subscriber();
		    if ( $subscriber && 'active' === $subscriber->get_status() ) {
			    if ( isset( $entry_details['activate_link'] ) ) {
				    unset( $entry_details['activate_link'] );
			    }
            }
        }

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

		ob_start();

		if ( 'plain' !== $this->get_content_type() ) {

			if ( isset( $entry_details['activate_link'] ) ) {
				echo '<a class="sg-button button" href="' . $entry_details['activate_link']['value'] . '">' . __( 'Activate', 'giveasap' ) . '</a>';
			}
			?>
            <table class="sg-email-table">
				<?php
				foreach ( $entry_details as $detail ) {
					if ( ! $detail['value'] ) {
						continue;
					}
					?>
                    <tr>
                        <th>
							<?php echo esc_html( $detail['title'] ); ?>
                        </th>
                        <td>
                            <?php if ( isset( $detail['type'] ) && 'link' === $detail['type'] ) { ?>
                                <a href="<?php echo esc_url( $detail['value'] ); ?>">
                            <?php } ?>
							<?php echo wp_kses_post( $detail['value'] ); ?>
	                        <?php if ( isset( $detail['type'] ) && 'link' === $detail['type'] ) { ?>
                                </a>
	                        <?php } ?>
                        </td>
                    </tr>
					<?php
				}
				?>
            </table>
			<?php
		} else {
			echo "\n\n";

			foreach ( $entry_details as $detail ) {
				if ( ! $detail['value'] ) {
					continue;
				}

				echo esc_html( $detail['title'] ) . ': ' . $detail['value'] . "\n\n";
			}

		}
		return ob_get_clean();
	}

	/**
	 * 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['subscriber_email'] ) ? $settings['subscriber_email']  : '';
		$email_subject    = isset( $settings['subscriber_email_subject'] ) ? $settings['subscriber_email_subject'] : __( 'Thank you, {{TITLE}} for entering in this giveaway', 'giveasap' );

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

		if( ! $email_subject ) {
			return false;
		}

		$email            = $subscriber->get_email();
		$title            = html_entity_decode( get_the_title( $subscriber->get_giveaway_id() ) );
		$email_subject    = str_replace('{{TITLE}}', $title, $email_subject );
		$subscriber_email = $this->get_email_content( $subscriber_email, $subscriber );

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

	/**
	 * @param string $subscriber_email
	 * @param SG_Subscriber $subscriber
	 *
	 * @return string
	 */
	public function get_email_content( $subscriber_email, $subscriber ) {
		$email     = $subscriber->get_email();
		$title     = html_entity_decode( get_the_title( $subscriber->get_giveaway_id() ) );
		$permalink = $subscriber->get_subscribed_from_page();
		if ( ! $permalink ) {
			$permalink = 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 );
		$activate_link    = add_query_arg( 'activate', '1', $entry_link );
		$subscriber_email = str_replace('{{TITLE}}', $title, $subscriber_email );
		$subscriber_email = str_replace('{{USER_EMAIL}}', $email, $subscriber_email );
		$subscriber_email = str_replace('{{ACTIVATE_LINK}}', $activate_link, $subscriber_email );
		$subscriber_email = str_replace('{{ENTRY_LINK}}', $entry_link, $subscriber_email );
		$subscriber_email = str_replace('{{SHARE_LINK}}', $share_link, $subscriber_email );

		$this->data['entry_link']    = $entry_link;
		$this->data['share_link']    = $share_link;
		$this->data['activate_link'] = $activate_link;

		$form_fields = sg_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 = '';
                }

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

		$subscriber_email = wpautop( $subscriber_email );

		return $subscriber_email;
	}
}