<?php
/**
 * PRO Layout Loader Base Class
 * 
 * This class provides a base for PRO layout functionality
 * and prevents fatal errors when PRO classes are missing.
 * Only loads if the class doesn't already exist.
 *
 * @package Product_Display
 */

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

// Only define the class if it doesn't already exist (PRO plugin may have its own)
if ( ! class_exists( 'PRODDISP_PRO_Layout_Loader' ) ) {

	/**
	 * Class PRODDISP_PRO_Layout_Loader
	 * 
	 * Fallback base class for PRO layout functionality
	 */
	class PRODDISP_PRO_Layout_Loader {
	
	/**
	 * Ensure base classes are loaded
	 * 
	 * @return void
	 */
	public static function ensure_base_loaded() {
		// Ensure PRODDISP_PLUGIN_DIR is defined
		if ( ! defined( 'PRODDISP_PLUGIN_DIR' ) ) {
			// Try to determine the plugin directory
			$plugin_dir = dirname( dirname( dirname( __FILE__ ) ) ) . '/';
			define( 'PRODDISP_PLUGIN_DIR', $plugin_dir );
		}
		
		// Ensure base layout class is loaded
		if ( ! class_exists( 'PRODDISP_Layout_Base' ) ) {
			$base_file = PRODDISP_PLUGIN_DIR . 'inc/base/class-proddisp-layout-base.php';
			if ( file_exists( $base_file ) ) {
				require_once $base_file;
			}
		}
		
		// Ensure loader class is available
		if ( ! class_exists( 'PRODDISP_Loader' ) ) {
			$loader_file = PRODDISP_PLUGIN_DIR . 'inc/class-proddisp-loader.php';
			if ( file_exists( $loader_file ) ) {
				require_once $loader_file;
			}
		}
	}
	
	/**
	 * Initialize the PRO layout loader
	 */
	public function __construct() {
		// Ensure base classes are loaded
		self::ensure_base_loaded();
	}
	
	/**
	 * Check if PRO features are available
	 * 
	 * @return bool
	 */
	public static function is_pro_available() {
		return class_exists('PRODDISP_Loader') && method_exists('PRODDISP_Loader', 'is_pro_active') && PRODDISP_Loader::is_pro_active();
	}
	
	/**
	 * Get PRO layout instance
	 * 
	 * @param string $layout_type Layout type
	 * @param array $settings Layout settings
	 * @return mixed Layout instance or false
	 */
	public static function get_layout_instance( $layout_type, $settings = [] ) {
		if ( ! self::is_pro_available() ) {
			return false;
		}
		
		$class_name = 'PRODDISP_PRO_' . ucfirst( $layout_type );
		
		if ( class_exists( $class_name ) ) {
			return new $class_name( $settings );
		}
		
		return false;
	}
	
	/**
	 * Render PRO layout
	 * 
	 * @param string $layout_type Layout type
	 * @param array $settings Layout settings
	 * @param array $products Products array
	 * @return string HTML output
	 */
	public static function render_layout( $layout_type, $settings, $products ) {
		$instance = self::get_layout_instance( $layout_type, $settings );
		
		if ( $instance && method_exists( $instance, 'render' ) ) {
			return $instance->render( $products );
		}
		
		return '';
	}
	
	/**
	 * Load required dependencies
	 * 
	 * @return void
	 */
	public static function load_dependencies() {
		self::ensure_base_loaded();
		
		// Load any additional dependencies
		$dependencies = [
			'inc/class-proddisp-shortcode.php',
			'inc/features/class-proddisp-compare.php',
			'inc/features/class-proddisp-quickview.php',
			'inc/features/class-proddisp-wishlist.php'
		];
		
		foreach ( $dependencies as $dependency ) {
			$file_path = PRODDISP_PLUGIN_DIR . $dependency;
			if ( file_exists( $file_path ) && ! class_exists( basename( $dependency, '.php' ) ) ) {
				require_once $file_path;
			}
		}
	}
	
	/**
	 * Initialize PRO layout system
	 * 
	 * @return void
	 */
	public static function init() {
		self::ensure_base_loaded();
		self::load_dependencies();
	}
	
	/**
	 * Get PRO plugin version
	 * 
	 * @return string
	 */
	public static function get_version() {
		return defined( 'PRODDISP_PRO_VERSION' ) ? PRODDISP_PRO_VERSION : '1.0.0';
	}
}

} // End class_exists check