<?php
/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://belovdigital.agency
 * @since      1.0.0
 *
 * @package    ultimate-pregnancy-ovulation-calculator
 * @subpackage ultimate-pregnancy-ovulation-calculator/includes
 */

/**
 * The core plugin class.
 */
class ULPC {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @var      ULPC_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

	/**
	 * Plugin options.
	 *
	 * @since    1.0.0
	 * @var      array    $plugin_options  The plugin options.
	 */
	protected $plugin_options;

	/**
	 * The current version of the plugin.
	 *
	 * @since    1.0.0
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		if ( defined( 'ULPC_VERSION' ) ) {
			$this->version = ULPC_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name    = ULPC_PLUGIN_NAME;
		$this->plugin_options = get_option( 'ulpc_options' );

		$this->load_dependencies();
		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - ULPC_Loader. Orchestrates the hooks of the plugin.
	 * - ULPC_Admin. Defines all hooks for the admin area.
	 * - ULPC_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 */
	private function load_dependencies() {

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ulpc-loader.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ulpc-i18n.php';

		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-ulpc-admin.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-ulpc-public.php';

		$this->loader = new ULPC_Loader();

	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Plugin_Name_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.3
	 */
	private function set_locale() {

		$plugin_i18n = new ULPC_I18n();

		$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );

	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 */
	private function define_admin_hooks() {

		$plugin_admin = new ULPC_Admin( $this->get_plugin_name(), $this->get_version() );
		$plugin_file  = ULPC_PLUGIN_FILE;

		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'add_settings_page' );
		$this->loader->add_action( 'admin_menu', $plugin_admin, 'go_pro_menu' );
		$this->loader->add_action( 'admin_init', $plugin_admin, 'register_settings' );
		$this->loader->add_filter( 'plugin_action_links_' . $plugin_file, $plugin_admin, 'plugin_settings_link' );
		$this->loader->add_action( 'wp_ajax_change_color_schema', $this, 'ajax_change_color_schema' );
		$this->loader->add_action( 'wp_ajax_nopriv_change_color_schema', $this, 'ajax_change_color_schema' );
	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 */
	private function define_public_hooks() {

		$plugin_public = new ULPC_Public( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
		$this->loader->add_action( 'wp_ajax_ulpc_get_baby_size', $plugin_public, 'ajax_get_baby_size' );
		$this->loader->add_action( 'wp_ajax_nopriv_ulpc_get_baby_size', $plugin_public, 'ajax_get_baby_size' );
		$this->loader->add_action( 'wp_ajax_ulpc_get_pregnancy_results', $plugin_public, 'ajax_get_pregnancy_results' );
		$this->loader->add_action( 'wp_ajax_nopriv_ulpc_get_pregnancy_results', $plugin_public, 'ajax_get_pregnancy_results' );
		$this->loader->add_action( 'wp_ajax_ulpc_get_ovulation_results', $plugin_public, 'ajax_get_ovulation_results' );
		$this->loader->add_action( 'wp_ajax_nopriv_ulpc_get_ovulation_results', $plugin_public, 'ajax_get_ovulation_results' );
		$this->loader->add_action( 'wp_ajax_ulpc_get_ovulation_visual_data', $plugin_public, 'ajax_get_ovulation_visual_data' );
		$this->loader->add_action( 'wp_ajax_nopriv_ulpc_get_ovulation_visual_data', $plugin_public, 'ajax_get_ovulation_visual_data' );
		add_shortcode( 'ultimate-pregnancy-ovulation', array( $plugin_public, 'ulpc_shortcode_callback' ) );

	}

	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    ULPC_Loader    Orchestrates the hooks of the plugin.
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}


	/**
	 * Get brightness adjustment coefficient based on the initial color.
	 *
	 * @param string $hex_code Supported formats: `#FFF`, `#FFFFFF`, `FFF`, `FFFFFF`.
	 * @param float  $adjust_percent A number between -1 and 1. E.g. 0.3 = 30% lighter; -0.4 = 40% darker.
	 * @return float Coefficient for brightness adjustment.
	 */
	public static function adjust_brightness_by_percent( $hex_code, $adjust_percent ) {
		$hex_code = ltrim( $hex_code, '#' );
		if ( 3 === strlen( $hex_code ) ) {
			$hex_code = $hex_code[0] . $hex_code[0] . $hex_code[1] . $hex_code[1] . $hex_code[2] . $hex_code[2];
		}

		$rgb = array_map( 'hexdec', str_split( $hex_code, 2 ) );

		// Calculate the brightness using the formula: 0.299*R + 0.587*G + 0.114*B.
		$brightness = ( 0.299 * $rgb[0] + 0.587 * $rgb[1] + 0.114 * $rgb[2] ) / 255;

		// Determine if color should be lightened or darkened.
		$adjust_percent = $brightness < 0.57 ? abs( $adjust_percent ) : -abs( $adjust_percent );

		foreach ( $rgb as &$color ) {
			if ( $adjust_percent > 0 ) {
				// Lightening.
				$color = round( $color + ( 255 - $color ) * $adjust_percent );
			} else {
				// Darkening.
				$color = round( $color * ( 1 + $adjust_percent ) );
			}

			// Ensure values stay within valid range.
			$color = max( 0, min( 255, $color ) );

			// Convert to hex.
			$color = str_pad( dechex( $color ), 2, '0', STR_PAD_LEFT );
		}

		return '#' . implode( '', $rgb );
	}

	/**
	 * Get Calculator option by name.
	 *
	 * @param   string $name    Option name.
	 *
	 * @since    1.0.0
	 */
	public function get_option( $name ) {
		$default_options = $this->get_default_values();

		if ( isset( $this->plugin_options[ $name ] ) ) {

			if ( 'empty' === $this->plugin_options[ $name ] ) {
				return '';
			}

			return $this->plugin_options[ $name ];
		}

		return isset( $default_options[ $name ] ) ? $default_options[ $name ] : '';
	}

	/**
	 * Calculator default values.
	 *
	 * @since  1.0.0
	 */
	public function get_default_values() {
		$values = array(
			'title_show'        => 'show',
			'title_text'        => __( 'Pregnancy & Ovulation Calculator', 'ultimate-pregnancy-ovulation-calculator' ),
			'primary_color'     => '#325878',
			'secondary_color'   => '#4989BE',
			'style_theme'       => 'minimal',
			'font_size'         => '16px',
			'date_format'       => 'dd MM yy',
			'calculator_type'   => 'pregnancy',
			'calculator_method' => 'last_period',
		);

		return $values;
	}

	/**
	 * Calculator date formats.
	 *
	 * @since    1.0.0
	 */
	public function get_default_date_formats() {
		$date_formats = array(
			'dd MM yy' => 'dd MM yy - ' . gmdate( 'd F Y' ),
			'mm/dd/yy' => 'mm/dd/yy - ' . gmdate( 'm/d/Y' ),
			'dd/mm/yy' => 'dd/mm/yy - ' . gmdate( 'd/m/Y' ),
			'yy-mm-dd' => 'yy-mm-dd - ' . gmdate( 'Y-m-d' ),
			'dd.mm.yy' => 'dd.mm.yy - ' . gmdate( 'd.m.Y' ),
		);
		return $date_formats;
	}

	/**
	 * Calculator layouts colors.
	 *
	 * @param   string $main_color        Supported formats: `#FFF`, `#FFFFFF`.
	 * @param   string $primary_color     Supported formats: `#FFF`, `#FFFFFF`.
	 *
	 * @since    1.0.0
	 */
	public function get_default_colors( $main_color = '#3E57ED', $primary_color = '#141414' ) {

		$layouts_colors = array(
			'minimal' => array(
				'primary-color'          => array(
					'name'          => __( 'Primary color', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $primary_color,
				),
				'primary-color-300'      => array(
					'name'          => __( 'Primary color 300', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $primary_color, 0.3 ),
				),
				'primary-color-500'      => array(
					'name'          => __( 'Primary color 500', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $primary_color, 0.5 ),
				),
				'primary-color-700'      => array(
					'name'          => __( 'Primary color 700', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $primary_color, 0.75 ),
				),
				'primary-color-800'      => array(
					'name'          => __( 'Primary color 700', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $primary_color, 0.87 ),
				),
				'primary-color-900'      => array(
					'name'          => __( 'Primary color 900', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $primary_color, 0.95 ),
				),
				'primary-inverted-color' => array(
					'name'          => __( 'Primary Inverted color', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $primary_color, 1 ),
				),
				'accent-color'           => array(
					'name'          => __( 'Accent color', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $main_color,
				),
				'accent-color-100'       => array(
					'name'          => __( 'Accent color 100', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $main_color, 0.1 ),
				),
				'accent-color-300'       => array(
					'name'          => __( 'Accent color 300', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $main_color, 0.3 ),
				),
				'accent-color-800'       => array(
					'name'          => __( 'Accent color 800', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $main_color, 0.8 ),
				),
				'accent-inverted-color'  => array(
					'name'          => __( 'Accent Inverted color', 'ultimate-pregnancy-ovulation-calculator' ),
					'default_color' => $this->adjust_brightness_by_percent( $main_color, 1 ),
				),

			),
		);

		return $layouts_colors;
	}

	/**
	 * Calculator font sizes.
	 *
	 * @since    1.0.0
	 */
	public function get_font_sizes() {
		$font_sizes = array(
			array(
				'name'  => esc_html__( 'Small', 'ultimate-pregnancy-ovulation-calculator' ),
				'value' => '14px',
			),
			array(
				'name'  => esc_html__( 'Normal', 'ultimate-pregnancy-ovulation-calculator' ),
				'value' => '16px',
			),
			array(
				'name'  => esc_html__( 'Large', 'ultimate-pregnancy-ovulation-calculator' ),
				'value' => '18px',
			),
			array(
				'name'  => esc_html__( 'Extra Large', 'ultimate-pregnancy-ovulation-calculator' ),
				'value' => '20px',
			),
		);

		return $font_sizes;
	}


	/**
	 * Displays the tooltip.
	 *
	 * @since    1.0.0
	 *
	 * @param array $args Tooltip args.
	 */
	public function display_tooltip( $args ) {
		$tooltip_text = $args['text'];
		?>
		<span class="ulpc-tooltip">
			<svg class="ulpc-tooltip-icon" style="width:18px;height:18px">
					<use xlink:href="<?php echo esc_attr( ULPC_PLUGIN_URL . 'public/images/help.svg#help' ); ?>"></use>
			</svg>
			<div class="ulpc-tooltip-text"><?php echo esc_html( $tooltip_text ); ?></div>		
		</span>
		<?php
	}

	/**
	 * Displays the help icon.
	 *
	 * @since    1.0.0
	 *
	 * @param string $help_id Help element ID.
	 */
	public function display_help_open( $help_id ) {
		?>
		<span class="ulpc-note" data-id="<?php echo esc_attr( $help_id ); ?>">
			<svg class="ulpc-note__open" style="width:16px;height:16px">
					<use xlink:href="<?php echo esc_attr( ULPC_PLUGIN_URL . 'public/images/info.svg#info' ); ?>"></use>
			</svg>
		</span>
		<?php
	}

	/**
	 * Displays the help text wrapper.
	 *
	 * @since    1.0.0
	 *
	 * @param array $args Help args.
	 */
	public function display_help_text( $args ) {
		$help_id    = isset( $args['id'] ) ? $args['id'] : '';
		$help_class = isset( $args['class'] ) ? $args['class'] : '';
		$help_text  = isset( $args['text'] ) ? $args['text'] : '';
		?>
		<div class="ulpc-note__content <?php echo esc_attr( $help_class ); ?>" id="<?php echo esc_attr( $help_id ); ?>" style="display:none;">
			<svg class="ulpc-note__close" style="width:10px;height:10px">
					<use xlink:href="<?php echo esc_attr( ULPC_PLUGIN_URL . 'public/images/close.svg#close' ); ?>"></use>
			</svg>
			<div class="ulpc-note__wrapper">
				<?php echo esc_html( $help_text ); ?>
			</div>
		</div>
		<?php
	}

	/**
	 * Displays the info text block.
	 *
	 * @since    1.0.0
	 *
	 * @param array $args Info block args.
	 */
	public function display_info_text( $args ) {
		$info_class = isset( $args['class'] ) ? $args['class'] : '';
		$info_text  = isset( $args['text'] ) ? $args['text'] : '';
		?>
		<div class="ulpc-info <?php echo esc_attr( $info_class ); ?>">
			<svg class="ulpc-info__icon">
					<use xlink:href="<?php echo esc_attr( ULPC_PLUGIN_URL . 'public/images/info.svg#info' ); ?>"></use>
			</svg>
			<div class="ulpc-info__content">
				<?php echo esc_html( $info_text ); ?>
			</div>
		</div>
		<?php
	}

	/**
	 * Get default calculator types list.
	 *
	 * @since    1.0.0
	 */
	public function get_calculator_types() {
		$calculator_types = array(
			'pregnancy' => 'Pregnancy Calculator',
			'ovulation' => 'Ovulation Calculator',
		);
		return $calculator_types;
	}


	/**
	 * Get Calculator method types.
	 *
	 * @since    1.0.0
	 */
	public function get_calculator_methods() {
		$calculator_methods = array(
			'due_date'        => 'Due Date',
			'last_period'     => 'Last Period',
			'conception_date' => 'Conception (soon)',
			'ultrasound'      => 'Ultrasound (soon)',
			'ivf_transfer'    => 'IVF Transfer (soon)',
		);
		return $calculator_methods;
	}

	/**
	 * Get ovulation visual data.
	 *
	 * @since    1.0.0
	 */
	public function get_ovulation_visual_data() {
		$ovulation_visual_data = array(
			'luteal'         => array(
				'name'        => 'Luteal Phase',
				'description' => 'Low probability of getting pregnant',
				'image'       => ULPC_PLUGIN_URL . 'public/images/phases/luteal.png',
			),
			'ovulation'      => array(
				'name'        => 'Ovulation',
				'description' => 'High probability of getting pregnant',
				'image'       => ULPC_PLUGIN_URL . 'public/images/phases/ovulation.png',
			),
			'menstrual'      => array(
				'name'        => 'Menstruation',
				'description' => 'Low probability of getting pregnant',
				'image'       => ULPC_PLUGIN_URL . 'public/images/phases/menstrual.png',
			),
			'fertile'        => array(
				'name'        => 'Fertile',
				'description' => 'There is a chance of getting pregnant',
				'image'       => ULPC_PLUGIN_URL . 'public/images/phases/fertile.png',
			),
			'pregnancy-test' => array(
				'name'        => 'Test Date',
				'description' => 'There is a best day to take a pregnancy test',
				'image'       => ULPC_PLUGIN_URL . 'public/images/phases/test.png',
			),
		);
		return $ovulation_visual_data;
	}

	/**
	 * Get ovulation visual data by phase.
	 *
	 * @since    1.0.0
	 * @param    string $phase    The phase (luteal, follicular, ovulation, menstrual).
	 * @return   array  Ovulation visual data.
	 */
	public function get_ovulation_visual_data_by_phase( $phase = '' ) {
		$ovulation_visual_data = $this->get_ovulation_visual_data();
		return $ovulation_visual_data[ $phase ];
	}


	/**
	 * Get all baby sizes.
	 *
	 * @since    1.0.0
	 * @param    string $method    The calculation method (ivf_transfer, last_period, etc.).
	 * @return   array  Baby sizes data.
	 */
	public function get_baby_sizes( $method = 'due_date' ) {
		$baby_sizes = array(
			'week_1'  => array(
				'name'        => 'Early Development',
				'description' => 'ivf_transfer' === $method ?
					'Early embryo development stage. For IVF, this represents the initial embryo growth' :
					'Early development stage. Your body prepares for ovulation.',
				'image'       => ULPC_PLUGIN_URL . 'public/images/future-pregnancy.jpg',
			),
			'week_2'  => array(
				'name'        => 'Cell Division',
				'description' => 'ivf_transfer' === $method ?
					'Rapid cell division occurring. This is when embryo continues developing before or after transfer' :
					'Rapid cell division occurring. This is when fertilization may happen.',
				'image'       => ULPC_PLUGIN_URL . 'public/images/future-pregnancy.jpg',
			),
			'week_3'  => array(
				'name'        => 'Implantation',
				'description' => 'ivf_transfer' === $method ?
					'Embryo attaches to uterine lining after transfer. Embryo size: 0.1mm' :
					'Embryo attaches to uterine lining. Pregnancy hormones begin production. Embryo size: 0.1mm',
				'image'       => ULPC_PLUGIN_URL . 'public/images/future-pregnancy.jpg',
			),
			'week_4'  => array(
				'name'        => 'Poppy Seed',
				'description' => 'Pregnancy tests can now detect hCG hormone. Embryo size: 0.2mm',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/poppy-seed.jpg',
			),
			'week_5'  => array(
				'name'        => 'Sesame Seed',
				'description' => 'Embryo size: 1.5mm. Neural tube forming, which becomes brain and spinal cord',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/sesame-seed.jpg',
			),
			'week_6'  => array(
				'name'        => 'Sweet Pea',
				'description' => 'Your embryo is approximately 6mm long and starting to develop facial features',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/sweet-pea.jpg',
			),
			'week_7'  => array(
				'name'        => 'Blueberry',
				'description' => 'Your baby is around 13mm long and developing arm and leg buds',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/blueberry.jpg',
			),
			'week_8'  => array(
				'name'        => 'Raspberry',
				'description' => 'Your baby measures about 16mm long and all essential organs are beginning to form',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/raspberry.jpg',
			),
			'week_9'  => array(
				'name'        => 'Grape',
				'description' => 'Your baby is approximately 23mm long and now officially called a fetus',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/grape.jpg',
			),
			'week_10' => array(
				'name'        => 'Prune',
				'description' => 'Your baby measures about 31mm from crown to rump and is developing more defined features',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/prune.jpg',
			),
			'week_11' => array(
				'name'        => 'Fig',
				'description' => 'Your baby is approximately 41mm long and starting to move around actively',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/fig.jpg',
			),
			'week_12' => array(
				'name'        => 'Lime',
				'description' => 'Your baby measures about 5.4cm long and is fully formed with all organs in place',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/lime.jpg',
			),
			'week_13' => array(
				'name'        => 'Peach',
				'description' => 'Your baby is approximately 7.4cm long and has unique fingerprints forming',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/peach.jpg',
			),
			'week_14' => array(
				'name'        => 'Lemon',
				'description' => 'Your baby measures about 8.7cm long and can now make facial expressions',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/lemon.jpg',
			),
			'week_15' => array(
				'name'        => 'Orange',
				'description' => 'Your baby is approximately 10.1cm long and can sense light through closed eyelids',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/orange.jpg',
			),
			'week_16' => array(
				'name'        => 'Avocado',
				'description' => 'Your baby measures about 11.6cm long and weighs approximately 100 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/avocado.jpg',
			),
			'week_17' => array(
				'name'        => 'Grapefruit',
				'description' => 'Your baby is approximately 13cm long and developing adipose (fat) tissue',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/grapefruit.jpg',
			),
			'week_18' => array(
				'name'        => 'Bell Pepper',
				'description' => 'Your baby measures about 14.2cm long and weighs around 190 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/bell-pepper.jpg',
			),
			'week_19' => array(
				'name'        => 'Mango',
				'description' => 'Your baby is approximately 15.3cm long and developing a protective coating called vernix',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/mango.jpg',
			),
			'week_20' => array(
				'name'        => 'Banana',
				'description' => 'Your baby measures about 16.4cm long and weighs around 300 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/banana.jpg',
			),
			'week_21' => array(
				'name'        => 'Сorn',
				'description' => 'Your baby is approximately 26.7cm long (head to heel) and can now swallow',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/corn.jpg',
			),
			'week_22' => array(
				'name'        => 'Papaya',
				'description' => 'Your baby measures about 27.8cm long and weighs around 430 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/papaya.jpg',
			),
			'week_23' => array(
				'name'        => 'Daikon',
				'description' => 'Your baby is approximately 28.9cm long and developing rapid eye movements',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/daikon.jpg',
			),
			'week_24' => array(
				'name'        => 'Cauliflower',
				'description' => 'Your baby measures about 30cm long and weighs around 600 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/cauliflower.jpg',
			),
			'week_25' => array(
				'name'        => 'Scallion',
				'description' => 'Your baby is approximately 34.6cm long and weighs about 660 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/scallion.jpg',
			),
			'week_26' => array(
				'name'        => 'Jicama',
				'description' => 'Your baby measures about 35.6cm long and weighs around 760 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/jicama.jpg',
			),
			'week_27' => array(
				'name'        => 'Big Rutabaga',
				'description' => 'Your baby is approximately 36.6cm long and weighs about 875 grams',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/rutabaga.jpg',
			),
			'week_28' => array(
				'name'        => 'Eggplant',
				'description' => 'Your baby measures about 37.6cm long and weighs around 1kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/eggplant.jpg',
			),
			'week_29' => array(
				'name'        => 'Butternut Squash',
				'description' => 'Your baby is approximately 38.6cm long and weighs about 1.15kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/butternut.jpg',
			),
			'week_30' => array(
				'name'        => 'Acorn Squash',
				'description' => 'Your baby measures about 39.9cm long and weighs around 1.3kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/acorn-squash.jpg',
			),
			'week_31' => array(
				'name'        => 'Coconut',
				'description' => 'Your baby is approximately 41.1cm long and weighs about 1.5kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/coconut.jpg',
			),
			'week_32' => array(
				'name'        => 'Rhubarb',
				'description' => 'Your baby measures about 42.4cm long and weighs around 1.7kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/rhubarb.jpg',
			),
			'week_33' => array(
				'name'        => 'Pineapple',
				'description' => 'Your baby is approximately 43.7cm long and weighs about 1.9kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/pineapple.jpg',
			),
			'week_34' => array(
				'name'        => 'Cantaloupe',
				'description' => 'Your baby measures about 45cm long and weighs around 2.1kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/cantaloupe.jpg',
			),
			'week_35' => array(
				'name'        => 'Honeydew Melon',
				'description' => 'Your baby is approximately 46.2cm long and weighs about 2.4kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/honeydew.jpg',
			),
			'week_36' => array(
				'name'        => 'Romaine Lettuce',
				'description' => 'Your baby measures about 47.4cm long and weighs around 2.6kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/lettuce.jpg',
			),
			'week_37' => array(
				'name'        => 'Winter Melon',
				'description' => 'Your baby is approximately 48.6cm long and weighs about 2.9kg. Your baby is now considered full term!',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/winter-melon.jpg',
			),
			'week_38' => array(
				'name'        => 'Pumpkin',
				'description' => 'Your baby measures about 49.8cm long and weighs around 3.1kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/pumpkin.jpg',
			),
			'week_39' => array(
				'name'        => 'Watermelon',
				'description' => 'Your baby is approximately 50.7cm long and weighs about 3.3kg',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/watermelon.jpg',
			),
			'week_40' => array(
				'name'        => 'Jackfruit',
				'description' => 'Your baby measures about 51.2cm long and weighs around 3.4kg. Ready to meet the world!',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/jackfruit.jpg',
			),
			'week_41' => array(
				'name'        => 'Jackfruit',
				'description' => 'Your baby is approximately 51.7cm long and weighs about 3.6kg. Your baby is fully developed and ready for birth',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/jackfruit.jpg',
			),
			'week_42' => array(
				'name'        => 'Jackfruit',
				'description' => 'Your baby measures about 52.3cm long and weighs around 3.7kg. You are now post-term',
				'image'       => ULPC_PLUGIN_URL . 'public/images/baby-size/jackfruit.jpg',
			),
		);

		return $baby_sizes;
	}

	/**
	 * Get baby size by weeks.
	 *
	 * @since    1.0.0
	 * @param int    $week   Week number.
	 * @param string $method The calculation method (ivf_transfer, last_period, etc.).
	 * @return array Baby size data for the specified week.
	 */
	public function get_baby_size_by_week( $week, $method = 'last_period' ) {
		$baby_sizes = $this->get_baby_sizes( $method );

		$baby_size = isset( $baby_sizes[ 'week_' . $week ] ) ? $baby_sizes[ 'week_' . $week ] : array();
		return $baby_size;
	}

	/**
	 * Format date.
	 *
	 * @param string $date   Date string in ISO format.
	 * @param string $format Date format.
	 *
	 * @return string Formatted date.
	 */
	public function format_date( $date, $format ) {
		// Create DateTime object with explicit UTC timezone.
		$date_obj = new DateTime( $date, new DateTimeZone( 'UTC' ) );

		// Convert format from jQuery UI Datepicker to PHP format.
		$format = str_replace(
			array( 'dd', 'mm', 'MM', 'yy' ),
			array( 'd', 'm', 'F', 'Y' ),
			$format
		);

		// Ensure we're using the correct timezone for display.
		$user_timezone = wp_timezone();
		$date_obj->setTimezone( $user_timezone );

		return $date_obj->format( $format );
	}

	/**
	 * Change color schema.
	 *
	 * @since 1.0.0
	 */
	public function ajax_change_color_schema() {
		// Verify nonce.
		if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'ulpc-nonce' ) ) {
			wp_send_json_error( __( 'Nonce verification failed', 'ultimate-pregnancy-ovulation-calculator' ) );
			wp_die();
		}

		$color_schema = isset( $_POST['ulpc_primary_color'] ) ? sanitize_text_field( wp_unslash( $_POST['ulpc_primary_color'] ) ) : '#325878';

		$new_default_colors = $this->get_default_colors( $color_schema );

		echo wp_json_encode( $new_default_colors );
		wp_die();
	}

}
