<?php
/**
 * Plugin Name:      QuickView – Instant Product Preview
 * Description:      Easily add an AJAX-powered product quick view modal to your WooCommerce shop pages. Customers can preview product details without leaving the current page.
 * Author:           Kirtikumar Solanki
 * Author URI:       https://profiles.wordpress.org/solankisoftware/
 * Version:          1.0.1
 * Text Domain:      quickview-instant-product-preview
 * Domain Path:      /languages
 * Requires Plugins: woocommerce
 * Requires at least: 5.0
 * Requires PHP:     7.0
 * License:          GPLv2 or later
 */

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

// Define plugin version
if ( ! defined( 'QVIPP_VERSION' ) ) {
    define( 'QVIPP_VERSION', '1.0.1' );
}
if ( ! defined( 'QVIPP_PLUGIN_URL' ) ) {
    define( 'QVIPP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'QVIPP_PLUGIN_PATH' ) ) {
    define( 'QVIPP_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}

/**
 * Activation hook.
 * Sets up default options when the plugin is activated.
 */
register_activation_hook( __FILE__, 'qvipp_quick_view_activate' );

function qvipp_quick_view_activate() {

	// Save the install date.
	add_option( 'qvipp_view_install_date', current_time( 'mysql' ), '', 'yes' );

	// Default functional options.
	$functional_defaults = array(
		'qvipp_enable_quick_view' => sanitize_text_field( '1' ), // Enable quick view by default.
		'qvipp_enable_mobile'     => sanitize_text_field( '0' ), // Disabled on mobile by default.
		'qvipp_button_lable'      => sanitize_text_field( 'Quick View' ), // Button label.
	);
	add_option( 'qvipp_options', $functional_defaults, '', 'yes' );

	// Default style options.
	$style_defaults = array(
		'qvipp_modal_bg'      => sanitize_text_field( '#ffffff' ),
		'qvipp_close_btn'     => sanitize_text_field( '#95979c' ),
		'qvipp_close_btn_bg'  => sanitize_text_field( '#4C6298' ),
		'qvipp_navigation_bg' => sanitize_text_field( 'rgba(255, 255, 255, 0.2)' ),
		'qvipp_navigation_txt'=> sanitize_text_field( '#ffffff' ),
	);
	add_option( 'qvipp_styles', $style_defaults, '', 'yes' );
}

/**
 * Deactivation hook.
 * Use this to clean up options if needed.
 */
register_deactivation_hook( __FILE__, 'qvipp_quick_view_deactivate' );

function qvipp_quick_view_deactivate() {
	// Uncomment the following lines if you want to remove options on deactivation.
	// delete_option( 'qvipp_styles' );
	// delete_option( 'qvipp_options' );
	// delete_option( 'qvipp_view_install_date' );
}

/**
 * Load admin and frontend files only when WooCommerce is active.
 */
add_action( 'plugins_loaded', 'qvipp_load_files' );
function qvipp_load_files() {

	// Check if WooCommerce is active.
	if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ), true ) ) {

		// Load admin and frontend classes.
		require_once plugin_dir_path( __FILE__ ) . 'admin/admin.php';
		require_once plugin_dir_path( __FILE__ ) . 'public/frontend.php';

		// Get plugin URL and options.
		$qvipp_plugin_dir_url = plugin_dir_url( __FILE__ );
		$options             = get_option( 'qvipp_options' );

		// Instantiate your backend class.
		//$backend = new qvipp_Backend( $qvipp_plugin_dir_url );
		$backend = new QVIPP_Backend(
		    plugin_dir_url( __FILE__ ),  // URL
		    plugin_basename( __FILE__ )  // basename for filter
		);

		// Determine if mobile quick view is enabled.
		$enable_mobile = ( isset( $options['qvipp_enable_mobile'] ) && '1' === $options['qvipp_enable_mobile'] );

		/**
		 * Load frontend functionality:
		 * - On mobile only if enabled in options.
		 * - Always on desktop if enabled in options.
		 */
		if ( $backend->qvipp_mobile_detect() ) {

			if ( $enable_mobile && ! empty( $options['qvipp_enable_quick_view'] ) ) {
				new QVIPP_Frontend( $qvipp_plugin_dir_url );
			}
		} else {

			if ( ! empty( $options['qvipp_enable_quick_view'] ) ) {
				new QVIPP_Frontend( $qvipp_plugin_dir_url );
			}
		}
	}
}
