<?php

namespace SmartSlotAppointmentScheduler;

if (! defined('ABSPATH')) {
	exit;
}

if (!class_exists('\SmartSlotAppointmentScheduler\Get_Sub_Menu_fields')) {
	/**
	 * Class to get the values of the fields in the settings page.
	 */
	class Get_Sub_Menu_fields {
		static $settings = [];

		public static function init() {
			self::get_fields();

			return self::$settings;
		}

		public static function get_fields() {
			$prefix = 'booking_schedule_' ;

			self::$settings = [
				'schedule_time_slot_interval'         => get_option("{$prefix}time_slot_interval"),
				'schedule_business_hour_start_time'   => get_option("{$prefix}business_hour_start_time"),
				'schedule_business_hour_end_time'     => get_option("{$prefix}business_hour_end_time"),
				'schedule_business_holidays'          => self::get_holidays(),
				'schedule_on_business_days'           => get_option("{$prefix}on_business_days"),
				'schedule_success_url'                => get_option("{$prefix}success_url"),
				'schedule_token_length'               => get_option("{$prefix}token_length"),
				'schedule_appointment_token_prefix'	  => get_option("{$prefix}appintment_token_prefix"),
			];
		}

		public static function get_holidays() {
			$holidays = get_option("booking_schedule_business_holidays", []);
			if (!is_array($holidays)) {
				return [];
			}
			return array_values(array_filter($holidays));
		}

		/**
		 * @param 'string'
		 *
		 * function to change the string in date array
		 *
		 * @return 'array'
		 */
		public static function change_format($datestring) {

			if (empty($datestring)) {
				return [];
			}

			$holiday_dates = explode(", ", $datestring);
			$formatted_dates = [];

			foreach ($holiday_dates as $holiday_date) {
				$date = \DateTime::createFromFormat("d/m/Y", trim($holiday_date));
				if ($date) {
					$formatted_dates[] = $date->format("Y-m-d");
				}
			}
			return $formatted_dates;
		}
	}
}
