<?php

namespace Simple_Giveaways;

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

class SG_Email_Notify_On_Admin extends SG_Email {

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

	/**
	 * Get the Subscriber data.
	 *
	 * @return array|null|string
	 */
	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_user_by_id( $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 = array();
		$subscriber    = $this->get_subscriber();
		$form_fields   = giveasap_get_form_fields( $subscriber->post_id );
		$meta          = giveasap_get_meta( $subscriber->id );
		if ( $meta && $form_fields ) {
			foreach ( $form_fields as $field_slug => $field ) {
				if ( ! $field['label'] ) {
					continue;
				}

				if ( 'user_email' === $field_slug ) {
					continue;
				}

				if ( 'user_name' === $field_slug ) {
					$value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';

					if ( ! $value ) {
						$value = isset( $meta['_name'] ) ? $meta['_name'] : '';
					}
				} else {
					$value = isset( $meta[ '_' . $field_slug ] ) ? $meta[ '_' . $field_slug ] : '';
				}

				if ( $value && is_array( $value ) ) {
					$field_value = implode( ', ', $value );
				} else {
					$field_value = $value;
				}

				$entry_details[ $field_slug ] = array(
					'title' => $field['label'],
					'value' => $field_value,
				);

			}
		}

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

		ob_start();

		if ( 'plain' !== $this->get_content_type() ) {
			?>
			<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 echo wp_kses_post( $detail['value'] ); ?>
						</td>
					</tr>
					<?php
				}
				?>
			</table>
			<?php
		} else {
			echo "\n\n";

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

				echo esc_html( $detail['title'] ) . ': ' . esc_html( $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();
		$notify_body = '';
		$posted_data = $this->data['posted_data'];
		$admin_email = get_option( 'admin_email', '' );
		if ( ! $admin_email ) {
			return false;
		}

		if ( $admin_email ) {
			$notify_body = '<p>' . sprintf( __( 'A new user %1$s subscribed to your giveaway %2$s', 'giveasap' ), $posted_data['sg_form']['user_email'], $this->data['title'] ) . '</p>';
		}

		return $this->send( $admin_email, __( 'A new user subscribed to your giveaway', 'giveasap' ), $notify_body );
	}
}
