<?php
/**
 * Template Loader Class
 *
 * @package Easy_Invoice
 * @subpackage Includes
 */

namespace EasyInvoice;

/**
 * Template Loader Class
 * 
 * Handles loading of custom templates for the Easy Invoice plugin.
 */
class TemplateLoader {
    
    /**
     * Initialize the template loader
     */
    public function init() {
        add_filter('single_template', [$this, 'loadSingleQuoteTemplate']);
        add_filter('single_template', [$this, 'loadSingleInvoiceTemplate']);
        add_filter('template_include', [$this, 'loadCustomTemplates']);
    }
    
    /**
     * Load single quote template
     *
     * @param string $template The template path
     * @return string Modified template path
     */
    public function loadSingleQuoteTemplate($template) {
        global $post;
        
        if ($post && $post->post_type === \EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE) {
            $custom_template = EASY_INVOICE_PLUGIN_DIR . 'templates/quotes/single.php';
            
            if (file_exists($custom_template)) {
                return $custom_template;
            } else {
                // Single quote template not found
            }
        }
        
        return $template;
    }
    
    /**
     * Load single invoice template
     *
     * @param string $template The template path
     * @return string Modified template path
     */
    public function loadSingleInvoiceTemplate($template) {
        global $post;
        
        if ($post && $post->post_type === \EasyInvoice\Constants\PostTypes::EASY_INVOICE_POST_TYPE) {
            $custom_template = EASY_INVOICE_PLUGIN_DIR . 'templates/invoices/single.php';
            
            if (file_exists($custom_template)) {
                return $custom_template;
            } else {
                // Single invoice template not found
            }
        }
        
        return $template;
    }
    
    /**
     * Load custom templates for Easy Invoice pages
     *
     * @param string $template The template path
     * @return string Modified template path
     */
    public function loadCustomTemplates($template) {
        global $post;
        
        // Handle single quote pages
        if (is_singular(\EasyInvoice\Constants\PostTypes::EASY_INVOICE_QUOTE_POST_TYPE)) {
            $custom_template = EASY_INVOICE_PLUGIN_DIR . 'templates/quotes/single.php';
            
            
            if (file_exists($custom_template)) {
                return $custom_template;
            } else {
                // Single quote template not found
            }
        }
        
        // Handle single invoice pages
        if (is_singular(\EasyInvoice\Constants\PostTypes::EASY_INVOICE_POST_TYPE)) {
            $custom_template = EASY_INVOICE_PLUGIN_DIR . 'templates/invoices/single.php';
            
            if (file_exists($custom_template)) {
                return $custom_template;
            } else {
                // Single invoice template not found
            }
        }
        
        return $template;
    }
} 