<?php
/**
 * Main plugin orchestrator.
 *
 * @package DreamFast_CF
 */

namespace DreamFast\CommerceFloat;

defined( 'ABSPATH' ) || exit;

require_once DREAMFAST_CF_PATH . 'includes/integrations/class-freemius-bridge.php';

$dreamfast_cf_premium_package = function_exists( 'dreamfast_cf_is_premium_package' ) && dreamfast_cf_is_premium_package();
if ( $dreamfast_cf_premium_package && is_readable( DREAMFAST_CF_PATH . 'includes/class-features__premium_only.php' ) ) {
	require_once DREAMFAST_CF_PATH . 'includes/class-features__premium_only.php';
} else {
	require_once DREAMFAST_CF_PATH . 'includes/class-features.php';
}

$dreamfast_cf_premium_code_available = is_readable( DREAMFAST_CF_PATH . 'includes/class-settings__premium_only.php' )
	&& is_readable( DREAMFAST_CF_PATH . 'includes/class-frontend__premium_only.php' );
$dreamfast_cf_use_premium_code       = $dreamfast_cf_premium_code_available && Features::is_premium_package();

if ( $dreamfast_cf_use_premium_code ) {
	require_once DREAMFAST_CF_PATH . 'includes/class-settings__premium_only.php';
	require_once DREAMFAST_CF_PATH . 'includes/class-frontend__premium_only.php';
	require_once DREAMFAST_CF_PATH . 'includes/class-analytics__premium_only.php';
} else {
	require_once DREAMFAST_CF_PATH . 'includes/class-settings.php';
	require_once DREAMFAST_CF_PATH . 'includes/class-frontend.php';
}

/**
 * Class Plugin
 */
class Plugin {

	/**
	 * Singleton instance.
	 *
	 * @var Plugin|null
	 */
	private static $instance = null;

	/**
	 * Get singleton instance.
	 *
	 * @return Plugin
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Initialize hooks.
	 */
	public function init() {
		Freemius_Bridge::init();
		Features::maybe_migrate();

		Settings::instance()->init();
		Frontend::instance()->init();
		if ( class_exists( '\DreamFast\CommerceFloat\Analytics' ) ) {
			Analytics::instance()->init();
		}

		if ( ! $this->woocommerce_active() ) {
			add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
		}
	}

	/**
	 * Whether WooCommerce is active.
	 *
	 * @return bool
	 */
	private function woocommerce_active() {
		return class_exists( 'WooCommerce' );
	}

	/**
	 * Admin notice when WooCommerce is missing (sticky cart requires WC).
	 */
	public function woocommerce_missing_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		printf(
			'<div class="notice notice-warning"><p>%s</p></div>',
			esc_html__( 'DreamFast Commerce Float: the sticky cart bar requires WooCommerce. The contact widget will continue to work.', 'dreamfast-commerce-float' )
		);
	}

	/**
	 * Activation hook.
	 */
	public static function activate() {
		Features::maybe_migrate();

		if ( false === get_option( Settings::OPTION_KEY, false ) ) {
			add_option( Settings::OPTION_KEY, Settings::get_defaults() );
		}

		if ( class_exists( '\DreamFast\CommerceFloat\Analytics' ) && false === get_option( Analytics::OPTION_KEY, false ) ) {
			add_option( Analytics::OPTION_KEY, Analytics::get_defaults() );
		}
	}
}
