<?php
/**
 * Plugin Name:       Bright Deposits for WooCommerce
 * Plugin URI:        https://wordpress.org/plugins/bright-deposits-for-woocommerce/
 * Description:       Enable customers to pay for products using a deposit or a partial payment.
 * Version:           1.0
 * Author:            Bright Plugins
 * Author URI:        https://brightplugins.com/
 * Text Domain:       bright-deposits-for-woocommerce
 * Domain Path:       /languages
 * Requires at least: 5.5
 * Tested up to: 6.9
 * WC tested up to: 10.3.7
 * WC requires at least: 4.0
 * Requires Plugins: woocommerce
 * Requires PHP: 7.4
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 */

defined( 'ABSPATH' ) || exit;

require_once __DIR__ . '/vendor/autoload.php';

define( 'BDFWBP_VERSION', '1.0' );
define( 'BDFWBP_PLUGIN_NAME', 'Bright Deposits for WooCommerce' );
define( 'BDFWBP_REDUX_VERSION', '4.4.18' );
define( 'BDFWBP_FILE', __FILE__ );
define( 'BDFWBP_PATH', __DIR__ );
define( 'BDFWBP_BASE_FILE', plugin_basename( __FILE__ ) );
define( 'BDFWBP_URL', plugins_url( '', BDFWBP_FILE ) );
define( 'BDFWBP_ASSETS', BDFWBP_URL . '/assets' );
define( 'BDFWBP_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
define( 'BDFWBP_TEMPLATE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );

use Bright_Deposits_For_WooCommerce\Bootstrap;
use Bright_Deposits_For_WooCommerce\Helpers\Helper;

/**
 * Class BrightDeposits
 * @package Bright_Deposits_For_WooCommerce
 * @since 1.0.0
 */
final class BrightDeposits {

	private function __construct() {
		register_activation_hook( __FILE__, [$this, 'activationPlugin'] );
		register_deactivation_hook( __FILE__, [$this, 'deactivationPlugin'] );
		add_action( 'woocommerce_loaded', [$this, 'initPlugin'] );
	}

	/**
	 * Plugin activation hook
	 * @since 1.0.0
	 * @return void
	 */
	public function activationPlugin(): void {
		$installed = get_option( 'bdfwbp_installed' );

		if ( !$installed ) {
			update_option( 'bdfwbp_installed', time() );
		}

		do_action( 'bdfwbp_on_activation' );
	}

	/**
	 * Plugin deactivation hook
	 * @since 1.0.0
	 * @return void
	 */
	public function deactivationPlugin(): void {
		do_action( 'bdfwbp_on_deactivation' );
	}

	/**
	 * Initialize the plugin.
	 * This method is called when the plugin is loaded
	 * @since 1.0.0
	 * @return void
	 */
	public function initPlugin(): void {
		/**
		 * Check if WooCommerce is active
		 * If it is, then initialize the plugin
		 * If not, then display an error message
		 */
		if ( apply_filters( 'bdfwbp_isWoocommerceInstalled', Helper::isWoocommerceInstalled() ) ) {
			add_action( 'before_woocommerce_init', function () {
				if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
					\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
				}
			} );

			
			Bootstrap::instance();

			/**
			 * Action after the plugin is initialized
			 * @since 1.0.0
			 */
			do_action( 'bdfwbp_after_init_plugin' );

		} else {
			add_action( 'admin_notices', function () {
				$class   = 'notice notice-error';
				$message = __( 'Oops! looks like WooCommerce is disabled. Please, enable it in order to use Bright Deposits for WooCommerce.', 'bright-deposits-for-woocommerce' );
				printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
			} );
		}
	}

	/**
	 * Initialize the plugin
	 * @since 1.0.0
	 * @return BrightDeposits
	 */
	public static function init() {
		static $instance = false;
		if ( !$instance ) {
			$instance = new self();
		}
		return $instance;
	}
}

/**
 * Bootstrap function (prefixed to avoid collisions with other plugins/themes).
 *
 * @since 1.0.0
 * @return BrightDeposits
 */
function bdfwbp_start() {
	return BrightDeposits::init();
}

bdfwbp_start();
