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

class IDVE_Blocks {

    private $blocks = array();

    public function __construct() {
        $this->load_default_blocks();
    }

    private function load_default_blocks() {
        $dir   = IDVE_PLUGIN_DIR . 'includes/blocks/';
        $files = array(
            'class-idve-block-text.php',
            'class-idve-block-heading.php',
            'class-idve-block-image.php',
            'class-idve-block-button.php',
            'class-idve-block-spacer.php',
            'class-idve-block-video.php',
            'class-idve-block-html.php',
            'class-idve-block-divider.php',
            'class-idve-block-audio.php',
            'class-idve-block-group.php',
            'class-idve-block-icon.php',
        );
        foreach ( $files as $file ) {
            require_once $dir . $file;
        }

        IDVE_Block_Text::register( $this );
        IDVE_Block_Heading::register( $this );
        IDVE_Block_Image::register( $this );
        IDVE_Block_Button::register( $this );
        IDVE_Block_Spacer::register( $this );
        IDVE_Block_Video::register( $this );
        IDVE_Block_HTML::register( $this );
        IDVE_Block_Divider::register( $this );
        IDVE_Block_Audio::register( $this );
        IDVE_Block_Group::register( $this );
        IDVE_Block_Icon::register( $this );
    }

    public function register( $type, $config ) {
        $defaults = array(
            'label'    => $type,
            'icon'     => 'admin-generic',
            'category' => 'content',
            'fields'   => array(),
            'render'   => null,
            'import'   => null,
        );
        $this->blocks[ $type ] = wp_parse_args( $config, $defaults );
        return true;
    }

    public function get( $type ) {
        return isset( $this->blocks[ $type ] ) ? $this->blocks[ $type ] : null;
    }

    public function get_all() {
        return $this->blocks;
    }

    public function render( $type, $data, $settings ) {
        $block = $this->get( $type );
        if ( $block && is_callable( $block['render'] ) ) {
            return call_user_func( $block['render'], $data, $settings );
        }
        return '';
    }

    public function get_editor_config() {
        $config = array();
        foreach ( $this->blocks as $type => $block ) {
            $js_fields = array();
            foreach ( $block['fields'] as $field_name => $field_def ) {
                $js_field = array(
                    'name'         => $field_name,
                    'type'         => isset( $field_def['type'] ) ? $field_def['type'] : 'text',
                    'label'        => isset( $field_def['label'] ) ? $field_def['label'] : $field_name,
                    'defaultValue' => isset( $field_def['default'] ) ? $field_def['default'] : '',
                );
                foreach ( array( 'options', 'min', 'max', 'step', 'tooltip' ) as $opt ) {
                    if ( isset( $field_def[ $opt ] ) ) {
                        $js_field[ $opt ] = $field_def[ $opt ];
                    }
                }
                $js_fields[] = $js_field;
            }

            $icon = $block['icon'];
            if ( strpos( $icon, 'dashicons-' ) !== 0 ) {
                $icon = 'dashicons-' . $icon;
            }

            $config[ $type ] = array(
                'label'    => $block['label'],
                'icon'     => $icon,
                'category' => $block['category'],
                'fields'   => $js_fields,
            );
        }
        return $config;
    }
}
