<?php

namespace EDPW\Core\Controllers;

use EDPW\Core\Helpers\EmailTemplate;
use EDPW\Core\Helpers\Util;

defined( 'ABSPATH' ) || exit;

class WordPress {
	/**
	 * Sends a notification on user registration.
	 *
	 * @param array $email_data The email data.
	 * @param object $user The user object.
	 *
	 * @return array
	 */
	public static function notifyOnUserRegistration( $email_data, $user ) {
		if ( ! is_array( $email_data ) || empty( $email_data['message'] ) || ! $user instanceof \WP_User ) {
			return $email_data;
		}
		$key           = get_password_reset_key( $user );
		$email         = (object) [
			'id'         => 'wp_new_user_register_email',
			'user_email' => $user->user_email,
			'user_login' => $user->user_login,
			'key'        => $key,
			'locale'     => get_user_locale( $user ),
			'object'     => []
		];
		$email->object = $user;
		$content       = EmailTemplate::getCustomRenderedTemplate( [
			'email'         => $email,
			'type'          => 'wp_new_user_register_email',
			'email_heading' => __( 'New user registered', 'email-editor-plus' ),
		] );
		if ( ! empty( $content ) ) {
			if ( is_array( $email_data['headers'] ) ) {
				$email_data['headers'][] = 'Content-Type: text/html; charset=UTF-8';
			} else {
				$email_data['headers'] = 'Content-Type: text/html; charset=UTF-8';
			}
			$email_data['message'] = $content;
		}

		return $email_data;
	}

	/**
	 * Retrieve the notification email for password reset.
	 *
	 * @param array $email_data The email data array.
	 * @param string $key The password reset key.
	 * @param string $user_login The user login name.
	 * @param object $user The user object.
	 *
	 * @return array
	 */
	public static function forgotPasswordNotificationEmail( $email_data, $key, $user_login, $user ) {
		if ( ! is_array( $email_data ) || empty( $email_data['message'] ) ) {
			return $email_data;
		}
		$email         = (object) [
			'id'         => 'wp_retrieve_password_email',
			'user_email' => $user->user_email,
			'user_login' => $user->user_login,
			'reset_key'  => $key,
			'locale'     => get_user_locale( $user ),
			'object'     => []
		];
		$email->object = $user;
		$content       = EmailTemplate::getCustomRenderedTemplate( [
			'email'         => $email,
			'type'          => 'wp_retrieve_password_email',
			'email_heading' => __( 'Password Reset Request', 'email-editor-plus' ),
		] );
		if ( ! empty( $content ) ) {
			if ( is_array( $email_data['headers'] ) ) {
				$email_data['headers'][] = 'Content-Type: text/html; charset=UTF-8';
			} else {
				$email_data['headers'] = 'Content-Type: text/html; charset=UTF-8';
			}
			$email_data['message'] = $content;
		}

		return $email_data;
	}

	/**
	 * Sends a notification on password change.
	 *
	 * @param array $email_data The email data.
	 * @param array $user_data The user data.
	 *
	 * @return array
	 */
	public static function notifyOnPasswordChange( $email_data, $user_data ) {
		if ( ! is_array( $email_data ) || empty( $email_data['message'] ) || empty( $user_data['user_email'] ) ) {
			return $email_data;
		}
		$user          = get_user_by_email( $user_data['user_email'] );
		$email         = (object) [
			'id'         => 'wp_password_change_email',
			'user_email' => $user->user_email,
			'user_login' => $user->user_login,
			'object'     => []
		];
		$email->object = $user;
		$content       = EmailTemplate::getCustomRenderedTemplate( [
			'email'         => $email,
			'type'          => 'wp_password_change_email',
			'email_heading' => 'Password Changed',
		] );
		if ( ! empty( $content ) ) {
			if ( is_array( $email_data['headers'] ) ) {
				$email_data['headers'][] = 'Content-Type: text/html; charset=UTF-8';
			} else {
				$email_data['headers'] = 'Content-Type: text/html; charset=UTF-8';
			}
			$email_data['message'] = $content;
		}

		return $email_data;
	}

	/**
	 * Notify on email change.
	 *
	 * @param array $email_data The email data.
	 * @param array $user_data The user data.
	 *
	 * @return array
	 */
	public static function notifyOnEmailChange( $email_data, $user_data ) {
		if ( ! is_array( $email_data ) || empty( $email_data['message'] ) || empty( $user_data['user_email'] ) ) {
			return $email_data;
		}

		$user          = get_user_by( 'id', $user_data['ID'] );
		$email         = (object) [
			'id'         => 'wp_email_change_email',
			'user_email' => $user->user_email,
			'user_login' => $user->user_login,
			'object'     => []
		];
		$email->object = $user;
		$content       = EmailTemplate::getCustomRenderedTemplate( [
			'email'         => $email,
			'type'          => 'wp_email_change_email',
			'email_heading' => 'Email Changed',
		] );
		if ( ! empty( $content ) ) {
			if ( is_array( $email_data['headers'] ) ) {
				$email_data['headers'][] = 'Content-Type: text/html; charset=UTF-8';
			} else {
				$email_data['headers'] = 'Content-Type: text/html; charset=UTF-8';
			}
			$email_data['message'] = $content;
		}

		return $email_data;
	}
}