<?php
if (!defined('ABSPATH')) {
	exit;
}

if (!defined('APBGF_LIVE_MODE')) {
	define('APBGF_LIVE_MODE', false); // TRUE OR FALSE
}

if (!defined('APBGF_FILE')) {
	define('APBGF_FILE', __FILE__);
}

if (!defined('APBGF_VERSION')) {
	define('APBGF_VERSION', '1.3');
}

if (!defined('APBGF_SLUG')) {
	define('APBGF_SLUG', 'appointment-and-booking-for-gravity-forms');
}

if (!defined('APBGF_URL')) {
	define('APBGF_URL', plugin_dir_url(__FILE__));
}

if (!defined('APBGF_PATH')) {
	define('APBGF_PATH', plugin_dir_path(__FILE__));
}

if (!defined('APBGF_FONT_URL')) {
	define('APBGF_FONT_URL', APBGF_URL . 'assets/fonts/');
}

if (!defined('APBGF_IMAGE_URL')) {
	define('APBGF_IMAGE_URL', APBGF_URL . 'assets/images/');
}

if (!defined('APBGF_ADMIN_JS_URL')) {
	define('APBGF_ADMIN_JS_URL', APBGF_URL . 'assets/admin/js/');
}

if (!defined('APBGF_COMMON_URL')) {
	define('APBGF_COMMON_URL', APBGF_URL . 'assets/common/');
}

if (!defined('APBGF_ADMIN_CSS_URL')) {
	define('APBGF_ADMIN_CSS_URL', APBGF_URL . 'assets/admin/css/');
}

if (!defined('APBGF_JS_URL')) {
	define('APBGF_JS_URL', APBGF_URL . 'assets/frontend/js/');
}

if (!defined('APBGF_CSS_URL')) {
	define('APBGF_CSS_URL', APBGF_URL . 'assets/frontend/css/');
}

if (!defined('APBGF_ADMIN_VIEW_PATH')) {
	define('APBGF_ADMIN_VIEW_PATH', APBGF_PATH . 'views/admin/');
}

if (!defined('APBGF_VIEW_PATH')) {
	define('APBGF_VIEW_PATH', APBGF_PATH . 'views/frontend/');
}

/**
 * Main class
 */
if (!class_exists('APBGF_Main')) {


	/**
	 * Restaurant main class
	 */
	class APBGF_Main
	{

		/**
		 * Instance
		 *
		 * @var $instance
		 */
		private static $instance = null;

		private static $classes = null;

		public static $apbgf_currency = array();

		/**
		 * Class Constructor
		 */
		private function __construct()
		{

			/**
			 * Check for plugin dependencies
			 */
			require_once APBGF_PATH . 'includes/classes/class-apbgf-dependencies.php';

			if (APBGF_Dependencies::active_check('gravityforms/gravityforms.php')) {

				self::$classes = array(
					'admin',
					'setting',
					'email',
					'appointment',
					'location',
					'service',
					'staff',
					'customer',
					'calendar',
					'report'
				);

				add_action('init', array($this, 'apbgf_init'), 2);
				add_action('init', array($this, 'load_textdomain'), 1);
				add_action('init', array($this, 'apbgf_reminder_email_cron_schedule'));
				add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_action_links'));

			} else {
				add_action('admin_notices', array($this, 'inactive_plugin_notice'));
			}
		}

		public function apbgf_reminder_email_cron_schedule()
		{
			if (!wp_next_scheduled('apbgf_plugin_cron_hook_for_email')) {
				$timezone = get_option('timezone_string');
				if (!empty($timezone)) {
					// Convert timezone for timestamp using WordPress functions
					$datetime = new DateTime('today midnight', new DateTimeZone($timezone));
					$timestamp = $datetime->getTimestamp();
				} else {
					// Fallback to default WordPress timezone offset
					$timestamp = strtotime('today midnight') + (get_option('gmt_offset') * HOUR_IN_SECONDS);
				}
				wp_schedule_event($timestamp, 'daily', 'apbgf_plugin_cron_hook_for_email');
			}

			add_action('apbgf_plugin_cron_hook_for_email', array($this, 'apbgf_cron_callback'));
		}

		public function apbgf_cron_callback()
		{
			APBGF_Email::send_reminder_notification('template_9', 'staff'); # template_9
			APBGF_Email::send_reminder_notification('template_10', 'customer'); # template_10
		}

		public function apbgf_init()
		{

			self::apbgf_includes();
			self::includes();
		}

		public function add_action_links($actions)
		{
			$mylinks = array(
				'<a href="' . admin_url('admin.php?page=gfb') . '">' . esc_html__('Dashboard', 'appointment-and-booking-for-gravity-forms') . '</a>'
			);
			$actions = array_merge($actions, $mylinks);
			return $actions;
		}

		/**
		 * Plugin Domain loaded
		 */
		public function load_textdomain()
		{
			load_plugin_textdomain('appointment-and-booking-for-gravity-forms', false, dirname(plugin_basename(__FILE__)) . '/languages/');
		}

		/**
		 * Inactive plugin notice.
		 */
		public function inactive_plugin_notice()
		{
			$class = 'notice notice-error';
			$headline = esc_html__('Gravity Booking requires Gravity Form to be install & active.', 'appointment-and-booking-for-gravity-forms');
			$message = esc_html__('Go to the plugins page to activate Gravity Form plugin', 'appointment-and-booking-for-gravity-forms');
			printf('<div class="%1$s"><h2>%2$s</h2><p>%3$s</p></div>', esc_attr($class), esc_html($headline), esc_html($message));
		}

		/**
		 * Files to includes
		 */
		private static function includes()
		{

			require_once APBGF_PATH . 'includes/helper/apbgf-functions.php';

			if (is_array(self::$classes) && count(self::$classes) > 0) {
				foreach (self::$classes as $class) {
					require_once APBGF_PATH . '/includes/classes/class-apbgf-' . $class . '.php';
				}
			}

			// Wizard
			require_once APBGF_PATH . '/includes/classes/class-apbgf-wizard.php';

			/* Load Woocommerce GF Product Addon */
			require_once APBGF_PATH . '/includes/compatibility/woocommerce-gravity-form-addon.php';


			/* update data on first load */
			if (false == get_option('apbgf_update_staff_data', false)) {

				$all_staffs = get_posts(array(
					'fields' => 'ids',
					'post_type' => 'apbgf-staff',
					'numberposts' => -1,
					'status' => 'publish',
				));

				if (is_array($all_staffs) && count($all_staffs) > 0) {
					foreach ($all_staffs as $staff_id) {
						APBGF_Staff::get_staff_data_by_id($staff_id);
					}
				}

				update_option('apbgf_update_staff_data', true);
			}
		}

		/**
		 * Files to includes GFB Related files
		 */
		private static function apbgf_includes()
		{

			require_once APBGF_PATH . 'includes/gravity-form/class-apbgf-form-setting.php';
			require_once APBGF_PATH . 'includes/gravity-form/apbgf-fields/class-apbgf-appointment-calendar-field.php';
		}

		/**
		 * Singleton Class Instatnce.
		 */
		public static function get_instance()
		{
			if (null === self::$instance) {
				self::$instance = new APBGF_Main();
			}

			return self::$instance;
		}
	}

	APBGF_Main::get_instance();

}
