<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class CAFI_Font_Manager {

	private static $instance = null;
	private $fonts_dir;
	// Only listing fonts physically present in admin/assets/fonts
	private $supported_fonts = [
		'Arvo'       => [ 'weights' => ['Regular', 'Bold'] ],
		'Montserrat' => [ 'weights' => ['Regular', 'Bold'] ],
		'Open Sans'  => [ 'weights' => ['Regular', 'Bold'] ],
		'Oswald'     => [ 'weights' => ['Regular', 'Bold'] ],
		'Poppins'    => [ 'weights' => ['Regular', 'Bold'] ],
		'Raleway'    => [ 'weights' => ['Regular', 'Bold'] ],
		'Roboto'     => [ 'weights' => ['Regular', 'Bold'] ],
		'Spectral'   => [ 'weights' => ['Regular', 'Bold'] ],
		'Teko'       => [ 'weights' => ['Regular', 'Bold'] ],
		'Work Sans'  => [ 'weights' => ['Regular', 'Bold'] ],
	];

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

	private function __construct() {
		// Fonts are now located in the plugin's admin/assets/fonts directory
		$this->fonts_dir = CAFI_PLUGIN_DIR . 'admin/assets/fonts/';
	}

	/**
	 * Get list of available fonts (System + Local Google Fonts)
	 */
	public function get_all_fonts() {
		$fonts = [
			'Arial'   => 'System',
			'Georgia' => 'System',
			'Verdana' => 'System',
		];
		
		foreach ( $this->supported_fonts as $name => $data ) {
			$fonts[$name] = 'Google Fonts';
		}
		
		return $fonts;
	}

	/**
	 * Get supported fonts data
	 */
	public function get_fonts_data() {
        return $this->supported_fonts;
    }

	/**
	 * Get the local filesystem path to the font file.
	 */
	public function get_font_path( $font_name, $weight = 'Regular' ) {
		// Normalize name
        $font_name = trim( $font_name );
        
		// Handle System Fonts (Arial is bundled, others might rely on OS or simple fallback)
		if ( in_array( $font_name, ['Arial', 'Georgia', 'Verdana'] ) ) {
            $filename = strtolower($font_name) . '.ttf'; // e.g. arial.ttf
            if ( file_exists( $this->fonts_dir . $filename ) ) {
                return $this->fonts_dir . $filename;
            }
            // If not found (Georgia/Verdana might not be bundled), return false so GD uses default
            return false; 
		}

		// Check if it's a supported Font
		if ( ! array_key_exists( $font_name, $this->supported_fonts ) ) {
            // Fallback: check if file exists anyway or try Regular
            return $this->check_local_file( $font_name, $weight );
		}

		return $this->check_local_file( $font_name, $weight );
	}

    /**
     * Helper to check if file exists locally
     */
    private function check_local_file( $font_name, $weight ) {
        $filename = $this->get_filename( $font_name, $weight );
		$local_path = $this->fonts_dir . $filename;

		if ( file_exists( $local_path ) ) {
			return $local_path;
		}

        // Fallback to Regular if requested weight is missing
        if ( $weight !== 'Regular' ) {
            $filename_reg = $this->get_filename( $font_name, 'Regular' );
            if ( file_exists( $this->fonts_dir . $filename_reg ) ) {
                return $this->fonts_dir . $filename_reg;
            }
        }

        return false;
    }

	private function get_filename( $font_name, $weight = 'Regular' ) {
        // Files are named like: Roboto-Bold.ttf, Roboto-Regular.ttf
        // Remove spaces from font name and weight
		return str_replace( ' ', '', $font_name ) . '-' . str_replace( ' ', '', $weight ) . '.ttf';
	}
}
