<?php
/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://belovdigital.agency
 * @since      1.0.0
 *
 * @package    ultimate-pregnancy-ovulation-calculator
 * @subpackage ultimate-pregnancy-ovulation-calculator/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 */
class ULPC_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string $plugin_name       The name of the plugin.
	 * @param      string $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version     = $version;

	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {
		wp_register_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/ulpc-public.min.css', array(), $this->version, 'all' );
	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {
		wp_register_script( $this->plugin_name . '-vendor', plugin_dir_url( __FILE__ ) . 'js/ulpc-vendor.min.js', array( 'jquery' ), $this->version, true );
		wp_register_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/ulpc-public.min.js', array( 'jquery' ), $this->version, true );

		wp_localize_script(
			$this->plugin_name,
			'ulpc_vars',
			array(
				'ajaxurl' => admin_url( 'admin-ajax.php' ),
				'i18n'    => array(
					'fertileWindow'              => __( 'Fertile Window', 'ultimate-pregnancy-ovulation-calculator' ),
					'ovulationDay'               => __( 'Ovulation Day', 'ultimate-pregnancy-ovulation-calculator' ),
					'pregnancyTestDay'           => __( 'Pregnancy Test Day', 'ultimate-pregnancy-ovulation-calculator' ),
					'period'                     => __( 'Period', 'ultimate-pregnancy-ovulation-calculator' ),
					'emptyDueDate'               => __( 'Please select the Due date', 'ultimate-pregnancy-ovulation-calculator' ),
					'emptyLastPeriodDate'        => __( 'Please select the Last Period date', 'ultimate-pregnancy-ovulation-calculator' ),
					'emptyConceptionDate'        => __( 'Please select the Conception date', 'ultimate-pregnancy-ovulation-calculator' ),
					'emptyUltrasoundDate'        => __( 'Please select the Ultrasound date', 'ultimate-pregnancy-ovulation-calculator' ),
					'invalidUltrasoundWeeksDays' => __( 'Invalid Ultrasound weeks or days', 'ultimate-pregnancy-ovulation-calculator' ),
					'emptyIvfTransferDate'       => __( 'Please select the IVF Transfer date', 'ultimate-pregnancy-ovulation-calculator' ),
					'emptyCycleLength'           => __( 'Please enter the cycle length', 'ultimate-pregnancy-ovulation-calculator' ),
				),
			)
		);
	}

	/**
	 * Shortcode callback function.
	 *
	 * @since    1.0.0
	 * @return   string    html markup of calculator.
	 */
	public function ulpc_shortcode_callback() {
		wp_enqueue_script( 'jquery-ui-core' );
		wp_enqueue_script( 'jquery-ui-datepicker' );

		wp_enqueue_style( $this->plugin_name );
		wp_enqueue_script( $this->plugin_name . '-vendor' );
		wp_enqueue_script( $this->plugin_name );

		ob_start();
		include 'partials/public-display.php';
		return ob_get_clean();
	}


	/**
	 * Get ovulation results via AJAX
	 *
	 * @since    1.0.0
	 */
	public function ajax_get_ovulation_results() {
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ulpc-nonce' ) ) {
			wp_send_json_error( __( 'Nonce verification failed', 'ultimate-pregnancy-ovulation-calculator' ) );
			die();
		}

		$ulpc = new ULPC();

		$result          = isset( $_POST['result'] ) ? sanitize_text_field( wp_unslash( $_POST['result'] ) ) : '';
		$calculator_data = isset( $_POST['calculator_data'] ) ? sanitize_text_field( wp_unslash( $_POST['calculator_data'] ) ) : '';
		$result          = json_decode( $result, true );
		$calculator_data = json_decode( $calculator_data, true );

		$template = 'ovulation.php';

		// Load template and get content.
		ob_start();
		include ULPC_PLUGIN_PATH . 'public/partials/results/' . $template;
		$html = ob_get_clean();

		wp_send_json_success(
			array(
				'html' => $html,
			)
		);
	}


	/**
	 * Get ovulation visual data via AJAX
	 *
	 * @since    1.0.0
	 */
	public function ajax_get_ovulation_visual_data() {

		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ulpc-nonce' ) ) {
			wp_send_json_error( __( 'Nonce verification failed', 'ultimate-pregnancy-ovulation-calculator' ) );
			die();
		}

		$calculator_id = isset( $_POST['calculator_id'] ) ? sanitize_text_field( wp_unslash( $_POST['calculator_id'] ) ) : '';

		$current_date  = isset( $_POST['current_date'] ) ? sanitize_text_field( wp_unslash( $_POST['current_date'] ) ) : '';
		$current_phase = isset( $_POST['current_phase'] ) ? sanitize_text_field( wp_unslash( $_POST['current_phase'] ) ) : '';
		$cycle_day     = isset( $_POST['cycle_day'] ) ? sanitize_text_field( wp_unslash( $_POST['cycle_day'] ) ) : '';

		$is_custom_font = false;
		if ( $calculator_id ) {
			$ulpc                = new ULPC( $calculator_id );
			$disable_custom_font = $ulpc->get_option( 'disable_custom_font' );

			if ( 'disable' !== $disable_custom_font ) {
				$is_custom_font = true;
			}
		}

		$ovulation_visual_data = $ulpc->get_ovulation_visual_data_by_phase( $current_phase );

		ob_start();
		include ULPC_PLUGIN_PATH . 'public/partials/results/ovulation-visual-data.php';
		$html = ob_get_clean();

		wp_send_json_success(
			array(
				'html' => $html,
			)
		);
	}


	/**
	 * Get pregnancy results via AJAX
	 *
	 * @since    1.0.0
	 */
	public function ajax_get_pregnancy_results() {
		$ulpc = new ULPC();

		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ulpc-nonce' ) ) {
			wp_send_json_error( __( 'Nonce verification failed', 'ultimate-pregnancy-ovulation-calculator' ) );
			die();
		}

		$result          = isset( $_POST['result'] ) ? sanitize_text_field( wp_unslash( $_POST['result'] ) ) : '';
		$calculator_data = isset( $_POST['calculator_data'] ) ? sanitize_text_field( wp_unslash( $_POST['calculator_data'] ) ) : '';
		$result          = json_decode( $result, true );
		$calculator_data = json_decode( $calculator_data, true );

		// Default template.
		$template = 'happy-birthday.php';

		// First priority: Check for future pregnancy.
		if ( isset( $result['futurePregnancy'] ) && true === $result['futurePregnancy'] ) {
			$template = 'future-pregnancy.php';
		} else {
			// Second priority: Check for current pregnancy.
			if ( isset( $result['pregnant'] ) && $result['pregnant'] ) {
				if ( $result['pregnantDays'] <= 0 ) {
					$template = 'future-pregnancy.php';
				} else {
					$template = 'pregnancy.php';
				}
			} elseif ( isset( $result['babyAge'] ) ) {
				$baby_age_weeks = isset( $result['babyAge']['weeks'] ) ? intval( $result['babyAge']['weeks'] ) : 0;
				$baby_age_days  = isset( $result['babyAge']['days'] ) ? intval( $result['babyAge']['days'] ) : 0;

				if ( $baby_age_weeks + $baby_age_days > 0 ) {
					$template = 'baby-born.php';
				} else {
					$template = 'happy-birthday.php';
				}
			}
		}

		$disable_custom_font = $ulpc->get_option( 'disable_custom_font' );
		$is_custom_font      = false;
		if ( 'disable' !== $disable_custom_font ) {
			$is_custom_font = true;
		}

		// Load template and get content.
		ob_start();
		include ULPC_PLUGIN_PATH . 'public/partials/results/' . $template;
		$html = ob_get_clean();

		wp_send_json_success(
			array(
				'html' => $html,
			)
		);
	}

	/**
	 * Get baby size by week via AJAX
	 *
	 * @since    1.0.0
	 */
	public function ajax_get_baby_size() {
		$ulpc = new ULPC();

		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'ulpc-nonce' ) ) {
			wp_send_json_error( __( 'Nonce verification failed', 'ultimate-pregnancy-ovulation-calculator' ) );
			die();
		}

		// Get week number.
		$week = isset( $_POST['week'] ) ? intval( $_POST['week'] ) : 0;

		if ( $week < 4 || $week > 40 ) {
			wp_send_json_error( __( 'Invalid week number', 'ultimate-pregnancy-ovulation-calculator' ) );
			wp_die();
		}

		// Get calculation method.
		$method = isset( $_POST['method'] ) ? sanitize_text_field( wp_unslash( $_POST['method'] ) ) : 'due_date';

		// Get baby size data.
		$baby_size = $ulpc->get_baby_size_by_week( $week, $method );

		if ( empty( $baby_size ) ) {
			wp_send_json_error( __( 'Baby size data not found', 'ultimate-pregnancy-ovulation-calculator' ) );
			wp_die();
		}

		wp_send_json_success( $baby_size );
		wp_die();
	}

}
