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

class Qode_Badges_For_WooCommerce_Preview {
	private static $instance;

	public $plugin_slug = 'qode-badges-for-woocommerce';

	public $plugin_name = 'Qode Badges for WooCommerce';


	public function __construct() {

		// Include scripts for preview.
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

		add_action( 'add_meta_boxes', array( $this, 'add_meta_box_preview' ) );
	}

	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	public function enqueue_scripts() {

		// check if page is qbfw_badge page.
		if ( function_exists( 'get_current_screen' ) && str_contains( get_current_screen()->id, 'qbfw_badge' ) ) {
			wp_enqueue_script( 'qode-badges-for-woocommerce-preview', QODE_BADGES_FOR_WOOCOMMERCE_INC_URL_PATH . '/preview/assets/js/preview.min.js', array( 'jquery' ), QODE_BADGES_FOR_WOOCOMMERCE_VERSION, false );
			wp_enqueue_style( 'qode-badges-for-woocommerce-preview', QODE_BADGES_FOR_WOOCOMMERCE_INC_URL_PATH . '/preview/assets/css/preview.min.css', array(), QODE_BADGES_FOR_WOOCOMMERCE_VERSION );
			wp_enqueue_style( 'qode-badges-for-woocommerce-preview-badge', QODE_BADGES_FOR_WOOCOMMERCE_ASSETS_URL_PATH . '/css/main.min.css', array(), QODE_BADGES_FOR_WOOCOMMERCE_VERSION );

			wp_add_inline_style( 'qode-badges-for-woocommerce-preview', $this->inline_preview_style() );

			$global = apply_filters(
				'qode_badges_for_woocommerce_filter_localize_preview_plugin_script',
				array(
					// default CSS styles from WordPress.
					'adminBarHeight' => is_admin_bar_showing() ? ( wp_is_mobile() ? 46 : 32 ) : 0,
				)
			);

			wp_localize_script(
				'qode-badges-for-woocommerce-preview',
				'qodeBadgesForWooCommercePreview',
				array(
					'vars' => $global,
				)
			);
		}
	}

	private function inline_preview_style() {
		return apply_filters( 'qode_badges_for_woocommerce_badges_filter_preview_style', '' );
	}

	public function print_deactivation_form() {
		$params['plugin_slug'] = str_replace( '-', '_', $this->plugin_slug );
		$params['plugin_name'] = $this->plugin_name;

		qode_badges_for_woocommerce_framework_template_part( QODE_BADGES_FOR_WOOCOMMERCE_ADMIN_PATH . '/inc', 'admin-notice', 'templates/admin-deactivation-form', '', $params );
	}

	public function add_meta_box_preview() {
		add_meta_box(
			'qbfw-badge-preview',
			esc_html__( 'Badge Preview', 'qode-badges-for-woocommerce' ),
			array( $this, 'preview_content' ),
			'qbfw_badge',
			'side',
			'default'
		);
	}

	public function preview_content( $post ) {
		$params = array(
			'badge_id' => $post->ID,
		);

		qode_badges_for_woocommerce_template_part( 'preview', 'templates/preview', '', $params );
	}
}

Qode_Badges_For_WooCommerce_Preview::get_instance();
