<?php
/**
 * The main class.
 *
 * @package HBFWC
 */

namespace HBFWC;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use HBFWC\Utils\DB;

/**
 * Class HBFWC\Plugin.
 */
class Plugin {
	/**
	 * Current plugin version.
	 *
	 * @var string
	 */
	public $version;

	/**
	 * Current pro version.
	 *
	 * @var string
	 */
	public $pro_v;

	/**
	 * Database class.
	 *
	 * @var DB
	 */
	public $db;

	/**
	 * Plugin file.
	 *
	 * @var string
	 */
	public $plugin_file;

	/**
	 * Class constructor.
	 *
	 * @param string $version     Plugin version.
	 * @param string $plugin_file Path to main plugin file.
	 */
	public function __construct( $version, $plugin_file ) {
		$this->version     = $version;
		$this->plugin_file = $plugin_file;
	}

	/**
	 * Init.
	 */
	public function init() {
		$this->pro_v = apply_filters( 'hbfwc_pro_version', false, $this->version );
		Shortcodes::init();
		Blocks::init();
		\hbfwc_product_type_register();
		add_filter( 'plugin_action_links_' . $this->get_basename(), array( $this, 'plugin_action_links' ) );

		do_action( 'hbfwc_init', $this );
	}

	/**
	 * Get the plugin url.
	 *
	 * @return string
	 */
	public function plugin_url(): string {
		return untrailingslashit( plugins_url( '/', $this->plugin_file ) );
	}

	/**
	 * Get the plugin path.
	 *
	 * @return string
	 */
	public function plugin_path(): string {
		return untrailingslashit( plugin_dir_path( $this->plugin_file ) );
	}

	/**
	 * Call required files.
	 */
	private function includes() {
		include_once dirname( $this->plugin_file ) . '/includes/class-upgrade.php';
		include_once dirname( $this->plugin_file ) . '/includes/utils/class-constants.php';
		include_once dirname( $this->plugin_file ) . '/includes/utils/class-db.php';
		include_once dirname( $this->plugin_file ) . '/includes/utils/class-data-cleaner.php';
		include_once dirname( $this->plugin_file ) . '/includes/utils/class-custom-taxonomies.php';
		include_once dirname( $this->plugin_file ) . '/includes/admin/class-booking-addon-metabox.php';
		include_once dirname( $this->plugin_file ) . '/includes/utils/class-custom-post-types.php';
		include_once dirname( $this->plugin_file ) . '/includes/wc/class-wc-hooks.php';
		include_once dirname( $this->plugin_file ) . '/includes/wc/class-form-handler.php';
		include_once dirname( $this->plugin_file ) . '/includes/class-frontend-scripts.php';
		include_once dirname( $this->plugin_file ) . '/includes/class-hbfwc-ajax.php';
		include_once dirname( $this->plugin_file ) . '/includes/class-shortcodes.php';
		include_once dirname( $this->plugin_file ) . '/includes/class-blocks.php';
		include_once dirname( $this->plugin_file ) . '/includes/class-hbfwc-shortcode-loop.php';

		include_once dirname( $this->plugin_file ) . '/includes/hbfwc-functions.php';
		include_once dirname( $this->plugin_file ) . '/includes/hbfwc-formatting-functions.php';
		include_once dirname( $this->plugin_file ) . '/includes/hbfwc-template-functions.php';
		include_once dirname( $this->plugin_file ) . '/includes/hbfwc-template-hooks.php';

		$this->admin_includes();

		do_action( 'hbfwc_include_internal_files' );
	}

	/**
	 * Integration init.
	 */
	public function init_integration() {
		\HBFWC\WC\WC_Hooks::run();
		add_filter( 'woocommerce_get_settings_pages', [ $this, 'register_settings_page' ] );
	}

	public function register_settings_page( $settings ) {
		$settings[] = include_once dirname( $this->plugin_file ) . '/includes/admin/class-hbfwc-wc-settings-tab.php';
		return $settings;
	}

	/**
	 * Files for admin.
	 */
	private function admin_includes() {
		if ( ! is_admin() ) {
			return false;
		}

		include_once dirname( $this->plugin_file ) . '/includes/admin/class-hbfwc-admin.php';
	}

	/**
	 * Get plugin basename.
	 *
	 * @return string
	 */
	public function get_basename() {
		return \plugin_basename( $this->plugin_file );
	}

	/**
	 * Show action links on the plugin screen.
	 *
	 * @param mixed $links Plugin Action links.
	 *
	 * @return array
	 */
	public function plugin_action_links( $links ) {
		$url = admin_url( 'admin.php?page=wc-settings&tab=hbfwc' );
		
		$action_links = array(
			'settings' => '<a href="' . esc_url( $url ) . '" aria-label="' . esc_attr__( 'View Hotel Booking for WooCommerce settings', 'hotel-booking-for-woocommerce' ) . '">' . esc_html__( 'Settings', 'hotel-booking-for-woocommerce' ) . '</a>',
		);

		return array_merge( $action_links, $links );
	}

	/**
	 * Setup and includes required files.
	 */
	public function run() {
		$this->includes();

		// Before plugin init hook.
		do_action( 'hbfwc_before_init' );

		$this->db = new DB();
		$upgrade  = new Upgrade( $this->version, $this->db );
		register_activation_hook( $this->plugin_file, array( $upgrade, 'run' ) );

		// Plugin init hook.
		add_action( 'init', array( $this, 'init' ) );

		add_action( 'plugins_loaded', array( $this, 'init_integration' ) );

		// After plugin init hook.
		do_action( 'hbfwc_after_init' );
	}
}
