<?php
/**
 * Certificate Settings Class
 *
 * Handles the settings for certificate generation.
 *
 * @package HVAC_Community_Events
 * @subpackage Certificates
 */

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

/**
 * Certificate Settings class.
 *
 * Provides settings for customizing certificates.
 *
 * @since 1.0.0
 */
class HVAC_Certificate_Settings {

    /**
     * The single instance of the class.
     *
     * @var HVAC_Certificate_Settings
     */
    protected static $_instance = null;

    /**
     * Main HVAC_Certificate_Settings Instance.
     *
     * Ensures only one instance of HVAC_Certificate_Settings is loaded or can be loaded.
     *
     * @return HVAC_Certificate_Settings - Main instance.
     */
    public static function instance() {
        if (is_null(self::$_instance)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * Constructor.
     */
    public function __construct() {
        // Initialize default settings if not already set
        $this->maybe_initialize_settings();
    }

    /**
     * Initialize default certificate settings if they don't exist.
     */
    public function maybe_initialize_settings() {
        // Certificate counter for unique numbers
        if (false === get_option('hvac_certificate_counter')) {
            add_option('hvac_certificate_counter', 0);
        }
        
        // Certificate number prefix
        if (false === get_option('hvac_certificate_prefix')) {
            add_option('hvac_certificate_prefix', 'HVAC-');
        }
        
        // Certificate storage path (relative to wp-content/uploads/)
        if (false === get_option('hvac_certificate_storage_path')) {
            add_option('hvac_certificate_storage_path', 'hvac-certificates');
        }
        
        // Certificate paper size
        if (false === get_option('hvac_certificate_paper_size')) {
            add_option('hvac_certificate_paper_size', 'LETTER'); // LETTER, A4, etc.
        }
        
        // Certificate orientation
        if (false === get_option('hvac_certificate_orientation')) {
            add_option('hvac_certificate_orientation', 'L'); // L for landscape, P for portrait
        }
        
        // Certificate background color
        if (false === get_option('hvac_certificate_bg_color')) {
            add_option('hvac_certificate_bg_color', '#ffffff');
        }
        
        // Certificate border color
        if (false === get_option('hvac_certificate_border_color')) {
            add_option('hvac_certificate_border_color', '#0074be');
        }
        
        // Certificate title text
        if (false === get_option('hvac_certificate_title_text')) {
            add_option('hvac_certificate_title_text', 'CERTIFICATE OF COMPLETION');
        }
        
        // Certificate title color
        if (false === get_option('hvac_certificate_title_color')) {
            add_option('hvac_certificate_title_color', '#0074be');
        }
        
        // Certificate body text color
        if (false === get_option('hvac_certificate_text_color')) {
            add_option('hvac_certificate_text_color', '#333333');
        }
        
        // Certificate completion text
        if (false === get_option('hvac_certificate_completion_text')) {
            add_option('hvac_certificate_completion_text', 'This certificate is awarded to {attendee_name} for successfully completing {event_name} on {event_date}.');
        }
    }

    /**
     * Get all certificate settings.
     *
     * @return array All certificate settings.
     */
    public function get_all_settings() {
        return array(
            'counter' => get_option('hvac_certificate_counter', 0),
            'prefix' => get_option('hvac_certificate_prefix', 'HVAC-'),
            'storage_path' => get_option('hvac_certificate_storage_path', 'hvac-certificates'),
            'paper_size' => get_option('hvac_certificate_paper_size', 'LETTER'),
            'orientation' => get_option('hvac_certificate_orientation', 'L'),
            'bg_color' => get_option('hvac_certificate_bg_color', '#ffffff'),
            'border_color' => get_option('hvac_certificate_border_color', '#0074be'),
            'title_text' => get_option('hvac_certificate_title_text', 'CERTIFICATE OF COMPLETION'),
            'title_color' => get_option('hvac_certificate_title_color', '#0074be'),
            'text_color' => get_option('hvac_certificate_text_color', '#333333'),
            'completion_text' => get_option('hvac_certificate_completion_text', 'This certificate is awarded to {attendee_name} for successfully completing {event_name} on {event_date}.')
        );
    }

    /**
     * Update a certificate setting.
     *
     * @param string $setting The setting key.
     * @param mixed  $value   The setting value.
     * 
     * @return bool True if successful, false otherwise.
     */
    public function update_setting($setting, $value) {
        $option_name = 'hvac_certificate_' . $setting;
        
        return update_option($option_name, $value);
    }

    /**
     * Get a certificate setting.
     *
     * @param string $setting     The setting key.
     * @param mixed  $default     Optional default value.
     * 
     * @return mixed The setting value or default.
     */
    public function get_setting($setting, $default = '') {
        $option_name = 'hvac_certificate_' . $setting;
        
        return get_option($option_name, $default);
    }

    /**
     * Get available certificate placeholders.
     *
     * @return array Placeholders and their descriptions.
     */
    public function get_placeholders() {
        return array(
            '{attendee_name}' => 'The full name of the attendee',
            '{event_name}' => 'The name of the event',
            '{event_date}' => 'The date when the event occurred',
            '{organization_name}' => 'The name of the training organization',
            '{instructor_name}' => 'The name of the instructor',
            '{venue_name}' => 'The name of the venue',
            '{certificate_number}' => 'The unique certificate number',
            '{issue_date}' => 'The date when the certificate was issued'
        );
    }

    /**
     * Replace placeholders in text with actual values.
     *
     * @param string $text   The text with placeholders.
     * @param array  $data   The data to replace placeholders with.
     * 
     * @return string The text with placeholders replaced.
     */
    public function replace_placeholders($text, $data) {
        $placeholders = array_keys($this->get_placeholders());
        $replacements = array();
        
        foreach ($placeholders as $placeholder) {
            $key = str_replace(array('{', '}'), '', $placeholder);
            $replacements[] = isset($data[$key]) ? $data[$key] : '';
        }
        
        return str_replace($placeholders, $replacements, $text);
    }
}