<?php

/**
 * Blynex Theme Class
 *
 * This file contains the main theme class that initializes the Blynex theme.
 * It handles theme setup, file loading, and registration of various WordPress features.
 *
 * @package Blynex
 * @since 1.0.0
 */
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Blynex_Theme Class
 *
 * This class handles the theme setup and includes necessary files.
 * It follows the singleton pattern to ensure only one instance exists.
 */
class Blynex_Theme
{

    /**
     * Instance of this class.
     *
     * @var Blynex_Theme
     */
    private static $instance = null;

    /**
     * Get the singleton instance of this class.
     *
     * @return Blynex_Theme
     */
    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor - sets up the theme.
     *
     * Initializes the theme by loading required files and setting up hooks.
     */
    private function __construct()
    {
        $this->load_includes();
        add_action('after_setup_theme', [$this, 'setup_theme']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_theme_styles']);
        add_action('init', [$this, 'register_block_bindings']);
        add_action('init', [$this, 'register_pattern_categories']);
        add_action('init', [$this, 'register_blocks']);
        add_filter('block_categories_all', [$this, 'register_block_categories'], 10, 2);
    }

    /**
     * Load necessary files for the theme.
     *
     * Includes all required PHP files from the includes directory.
     */
    private function load_includes()
    {
        $files = [
            'block-styles.php',      // Registers custom block styles
            'class-enqueue.php',     // Handles asset loading
            'theme-settings.php',    // Handles theme settings
            'class-zoloblocks-installer.php', // Handles ZoloBlocks plugin installation
            'class-admin-notice.php',        // Handles admin notices
            'features/scroll-top.php',     // Handles scroll to top button
            'features/contact-form-handler.php', // Contact form submission handler
            'hover-effect-frontend.php',   // Handles frontend hover effects
        ];

        foreach ($files as $file) {
            $path = get_template_directory() . '/includes/' . $file;
            if (file_exists($path)) {
                require_once $path;
            }
        }
    }

    /**
     * Set up theme features and support.
     *
     * Registers theme support for various WordPress features.
     */
    public function setup_theme()
    {
        // Load theme text domain for translations
        load_theme_textdomain('blynex', get_template_directory() . '/languages');

        // Add support for post formats
        add_theme_support(
            'post-formats',
            ['aside', 'audio', 'chat', 'gallery', 'image', 'link', 'quote', 'status', 'video']
        );

        // Add WooCommerce support
        add_theme_support('woocommerce');
        
        // Add WooCommerce block styles
        add_theme_support('wc-product-gallery-zoom');
        add_theme_support('wc-product-gallery-lightbox');
        add_theme_support('wc-product-gallery-slider');
        
        // Add support for WooCommerce block templates
        add_theme_support('woocommerce-block-templates');
    }

    /**
     * Enqueue theme styles.
     *
     * Loads the main stylesheet for the theme.
     */
    public function enqueue_theme_styles()
    {
        wp_enqueue_style(
            'blynex-style',
            get_parent_theme_file_uri('style.css'),
            [],
            wp_get_theme()->get('Version')
        );
    }

    /**
     * Register block bindings for dynamic content.
     *
     * Sets up custom block bindings for the theme.
     */
    public function register_block_bindings()
    {
        register_block_bindings_source(
            'blynex/format',
            [
                'label'              => _x('Post format name', 'Label for the block binding placeholder in the editor', 'blynex'),
                'get_value_callback' => [$this, 'format_binding_callback'],
            ]
        );
    }

    /**
     * Callback for the post format binding.
     *
     * Returns the formatted post format name.
     *
     * @return string|null The post format name or null if standard format.
     */
    public function format_binding_callback()
    {
        $post_format_slug = get_post_format();
        if ($post_format_slug && 'standard' !== $post_format_slug) {
            return get_post_format_string($post_format_slug);
        }
    }

    /**
     * Register block pattern categories.
     *
     * Creates custom pattern categories for the theme.
     */
    public function register_pattern_categories()
    {
        register_block_pattern_category(
            'blynex_page',
            [
                'label'       => __('Pages', 'blynex'),
                'description' => __('A collection of full page layouts.', 'blynex'),
            ]
        );

        register_block_pattern_category(
            'blynex_post-format',
            [
                'label'       => __('Post formats', 'blynex'),
                'description' => __('A collection of post format patterns.', 'blynex'),
            ]
        );
    }

    /**
     * Register custom blocks.
     *
     * This method registers custom blocks using block.json metadata.
     */
    public function register_blocks()
    {
        // Register the Responsive Navigation Menu block
        $responsive_nav_menu_block_path = get_template_directory() . '/build/features/responsive-nav-menu';
        if (file_exists($responsive_nav_menu_block_path . '/block.json')) {
            register_block_type_from_metadata($responsive_nav_menu_block_path);
        }
        
        // Register the Card Widget block
        $card_widget_block_path = get_template_directory() . '/build/features/card-widget';
        if (file_exists($card_widget_block_path . '/block.json')) {
            register_block_type_from_metadata($card_widget_block_path);
        }

        // Register the Contact Form Widget block
        $contact_form_widget_block_path = get_template_directory() . '/build/features/contact-form-widget';
        if (file_exists($contact_form_widget_block_path . '/block.json')) {
            register_block_type_from_metadata($contact_form_widget_block_path);
        }

        // Register the Live Clock/Date block
        $live_clock_date_block_path = get_template_directory() . '/build/features/live-clock-date';
        if (file_exists($live_clock_date_block_path . '/block.json')) {
            register_block_type_from_metadata($live_clock_date_block_path);
        }

        // Register the Lottie Animation block
        $lottie_animation_block_path = get_template_directory() . '/build/features/lottie-animation';
        if (file_exists($lottie_animation_block_path . '/block.json')) {
            register_block_type_from_metadata($lottie_animation_block_path);
        }

        // Register the Scroll Progress Indicator block
        $scroll_progress_block_path = get_template_directory() . '/build/features/scroll-progress-indicator';
        if (file_exists($scroll_progress_block_path . '/block.json')) {
            register_block_type_from_metadata($scroll_progress_block_path);
        }

        // Register the Interactive Hotspot Image block
        $interactive_hotspot_block_path = get_template_directory() . '/build/features/interactive-hotspot-image';
        if (file_exists($interactive_hotspot_block_path . '/block.json')) {
            register_block_type_from_metadata($interactive_hotspot_block_path);
        }

        // Register the Advanced Icon Button block
        $advanced_icon_button_path = get_template_directory() . '/build/features/advanced-icon-button';
        if (file_exists($advanced_icon_button_path . '/block.json')) {
            register_block_type_from_metadata($advanced_icon_button_path);
        }

        // Register the Animated Counter block
        $animated_counter_block_path = get_template_directory() . '/build/features/animated-counter-block';
        if (file_exists($animated_counter_block_path . '/block.json')) {
            register_block_type_from_metadata($animated_counter_block_path);
        }

        // Register the 3D Tilt Card block
        $three_d_tilt_card_block_path = get_template_directory() . '/build/features/3d-tilt-card-block';
        if (file_exists($three_d_tilt_card_block_path . '/block.json')) {
            register_block_type_from_metadata($three_d_tilt_card_block_path);
        }

        // Register the Button Group block
        $button_group_block_path = get_template_directory() . '/build/features/button-group-block';
        if (file_exists($button_group_block_path . '/block.json')) {
            register_block_type_from_metadata($button_group_block_path);
        }

        // Register the Button Item block
        $button_item_block_path = get_template_directory() . '/build/features/button-group-block/button-item';
        if (file_exists($button_item_block_path . '/block.json')) {
            register_block_type_from_metadata($button_item_block_path);
        }
    }

    /**
     * Register custom block categories.
     * 
     * Adds a custom category for blynex blocks.
     * 
     * @param array $categories Array of block categories.
     * @param object $post Post object.
     * @return array Modified array of block categories.
     */
    public function register_block_categories($categories, $post)
    {
        return array_merge(
            [
                [
                    'slug'  => 'blynex-blocks',
                    'title' => __('blynex Blocks', 'blynex'),
                    'icon'  => 'admin-customizer',
                ],
            ],
            $categories
        );
    }
}
