<?php
/**
 * Blocks registration class.
 *
 * @package Hugeicons
 */

namespace Hugeicons;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Handles registration of all Hugeicons Gutenberg blocks.
 */
class Blocks {

    /**
     * List of block names to register.
     *
     * @var array
     */
    private $blocks = array(
        'icon',
        'icon-list',
        'button-icon',
        'icon-box',
        'faq',
    );

    /**
     * License instance.
     *
     * @var License
     */
    private $license;

    /**
     * Constructor.
     *
     * @param License $license The license instance.
     */
    public function __construct( $license = null ) {
        $this->license = $license ?? new License();
    }

    /**
     * Initialize blocks.
     *
     * @return void
     */
    public function init() {
        add_action( 'init', array( $this, 'register_blocks' ) );
        add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) );
    }

    /**
     * Register all blocks.
     *
     * @return void
     */
    public function register_blocks() {
        // Check if build directory exists.
        if ( ! file_exists( HUGEICONS_PLUGIN_DIR . 'build' ) ) {
            return;
        }

        // Register each block.
        foreach ( $this->blocks as $block ) {
            $block_dir = HUGEICONS_PLUGIN_DIR . 'build/blocks/' . $block;
            
            if ( file_exists( $block_dir . '/block.json' ) ) {
                register_block_type( $block_dir );
            }
        }
    }

    /**
     * Enqueue editor-specific assets.
     *
     * @return void
     */
    public function enqueue_editor_assets() {
        $asset_file = HUGEICONS_PLUGIN_DIR . 'build/index.asset.php';

        if ( ! file_exists( $asset_file ) ) {
            return;
        }

        $asset = include $asset_file;

        wp_enqueue_script(
            'hugeicons-editor',
            HUGEICONS_PLUGIN_URL . 'build/index.js',
            $asset['dependencies'],
            $asset['version'],
            true
        );

        wp_enqueue_style(
            'hugeicons-editor',
            HUGEICONS_PLUGIN_URL . 'build/index.css',
            array(),
            $asset['version']
        );

        // Localize script with API data and license status.
        wp_localize_script(
            'hugeicons-editor',
            'hugeiconsData',
            array(
                'apiUrl'     => rest_url( 'hugeicons/v1' ),
                'nonce'      => wp_create_nonce( 'wp_rest' ),
                'iconApiUrl' => HUGEICONS_API_ICON_URL,
                'styles'     => $this->get_icon_styles(),
                'isPro'      => $this->license->is_pro_active(),
                'freeStyle'  => License::get_free_style(),
                'settingsUrl' => admin_url( 'options-general.php?page=hugeicons-settings' ),
            )
        );
    }

    /**
     * Enqueue frontend assets.
     *
     * @return void
     */
    public function enqueue_frontend_assets() {
        // Check if any of our blocks are present on the page.
        if ( ! has_block( 'hugeicons/icon' ) &&
             ! has_block( 'hugeicons/icon-list' ) &&
             ! has_block( 'hugeicons/button-icon' ) &&
             ! has_block( 'hugeicons/icon-box' ) &&
             ! has_block( 'hugeicons/faq' ) ) {
            return;
        }

        $asset_file = HUGEICONS_PLUGIN_DIR . 'build/style-index.css';

        if ( file_exists( $asset_file ) ) {
            wp_enqueue_style(
                'hugeicons-frontend',
                HUGEICONS_PLUGIN_URL . 'build/style-index.css',
                array(),
                HUGEICONS_VERSION
            );
        }

        // Enqueue FAQ view script if FAQ block is present.
        if ( has_block( 'hugeicons/faq' ) ) {
            $view_asset = HUGEICONS_PLUGIN_DIR . 'build/blocks/faq/view.asset.php';
            
            if ( file_exists( $view_asset ) ) {
                $asset = include $view_asset;
                
                wp_enqueue_script(
                    'hugeicons-faq-view',
                    HUGEICONS_PLUGIN_URL . 'build/blocks/faq/view.js',
                    $asset['dependencies'],
                    $asset['version'],
                    true
                );
            }
        }
    }

    /**
     * Get available icon styles with pro indicators.
     *
     * @return array
     */
    private function get_icon_styles() {
        $free_style = License::get_free_style();

        return array(
            array(
                'value' => 'stroke-rounded',
                'label' => 'Stroke Rounded',
                'group' => 'Rounded',
                'isPro' => 'stroke-rounded' !== $free_style,
            ),
            array(
                'value' => 'solid-rounded',
                'label' => 'Solid Rounded',
                'group' => 'Rounded',
                'isPro' => 'solid-rounded' !== $free_style,
            ),
            array(
                'value' => 'duotone-rounded',
                'label' => 'Duotone Rounded',
                'group' => 'Rounded',
                'isPro' => 'duotone-rounded' !== $free_style,
            ),
            array(
                'value' => 'twotone-rounded',
                'label' => 'Twotone Rounded',
                'group' => 'Rounded',
                'isPro' => 'twotone-rounded' !== $free_style,
            ),
            array(
                'value' => 'bulk-rounded',
                'label' => 'Bulk Rounded',
                'group' => 'Rounded',
                'isPro' => 'bulk-rounded' !== $free_style,
            ),
            array(
                'value' => 'stroke-standard',
                'label' => 'Stroke Standard',
                'group' => 'Standard',
                'isPro' => 'stroke-standard' !== $free_style,
            ),
            array(
                'value' => 'solid-standard',
                'label' => 'Solid Standard',
                'group' => 'Standard',
                'isPro' => 'solid-standard' !== $free_style,
            ),
            array(
                'value' => 'duotone-standard',
                'label' => 'Duotone Standard',
                'group' => 'Standard',
                'isPro' => 'duotone-standard' !== $free_style,
            ),
            array(
                'value' => 'stroke-sharp',
                'label' => 'Stroke Sharp',
                'group' => 'Sharp',
                'isPro' => 'stroke-sharp' !== $free_style,
            ),
            array(
                'value' => 'solid-sharp',
                'label' => 'Solid Sharp',
                'group' => 'Sharp',
                'isPro' => 'solid-sharp' !== $free_style,
            ),
        );
    }
}
