<?php

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

/**
 * WPGV FPDF Isolation Class
 * Prevents FPDF font conflicts with other plugins
 */
class WPGV_FPDF_Isolation
{

    private static $instance = null;
    private $fpdf_path = null;
    private $font_path = null;
    private $pdf_instance = null;

    /**
     * Singleton pattern
     */
    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor - setup isolated paths
     */
    private function __construct()
    {
        // Set isolated paths
        $this->fpdf_path = WPGIFT__PLUGIN_DIR . '/library/fpdf_isolated/';
        $this->font_path = WPGIFT__PLUGIN_DIR . '/library/fpdf_isolated/font/';

        // Load FPDF only when needed
        $this->load_fpdf();
    }

    /**
     * Load FPDF library with isolated font path
     */
    private function load_fpdf()
    {
        // Define isolated FPDF font path
        if (!defined('WPGV_FPDF_FONTPATH')) {
            define('WPGV_FPDF_FONTPATH', $this->font_path);
        }

        // Load FPDF classes
        if (!class_exists('WPGV_PDF')) {
            require_once $this->fpdf_path . 'fpdf.php';
            require_once $this->fpdf_path . 'rotation.php';
            require_once WPGIFT__PLUGIN_DIR . '/classes/fpdf.php';
        }
    }

    /**
     * Create isolated PDF instance
     */
    public function create_pdf($orientation = 'P', $unit = 'pt', $format = 'A4')
    {
        // Don't set global FPDF_FONTPATH to avoid conflicts with other plugins
        // Instead, use internal font path handling

        // Create PDF instance using WPGV_PDF which includes Code128 method
        $this->pdf_instance = new WPGV_PDF($orientation, $unit, $format);

        // Note: WPGV_PDF will use WPGV_FPDF_FONTPATH internally
        // No need to set font path manually as it's handled in the constructor

        return $this->pdf_instance;
    }

    /**
     * Get isolated font path
     */
    public function get_font_path()
    {
        return $this->font_path;
    }

    /**
     * Check if font file exists in isolated path
     */
    public function font_exists($font_file)
    {
        return file_exists($this->font_path . $font_file);
    }

    /**
     * Add font to PDF instance with isolated path
     */
    public function add_font($pdf_instance, $family, $style = '', $file = '')
    {
        if ($pdf_instance && method_exists($pdf_instance, 'AddFont')) {
            // FPDF expects only the font definition filename (no directory separators).
            // Ensure we pass only the basename (e.g. 'arial.php') so FPDF will load it
            // from the configured font path (WPGV_FPDF_FONTPATH).
            if ($file) {
                $file = basename($file);
            }
            $pdf_instance->AddFont($family, $style, $file);
        }
    }

    /**
     * Clean up resources
     */
    public function cleanup()
    {
        $this->pdf_instance = null;
    }
}

/**
 * Wrapper function to create isolated PDF
 */
function wpgv_create_isolated_pdf($orientation = 'P', $unit = 'pt', $format = 'A4')
{
    $isolation = WPGV_FPDF_Isolation::get_instance();
    return $isolation->create_pdf($orientation, $unit, $format);
}

/**
 * Wrapper function to add font with isolation
 */
function wpgv_add_isolated_font($pdf_instance, $family, $style = '', $file = '')
{
    $isolation = WPGV_FPDF_Isolation::get_instance();
    return $isolation->add_font($pdf_instance, $family, $style, $file);
}
