<?php
/**
 * Native Gutenberg blocks and an Elementor widget.
 */
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class YATOON_Editor_Integrations {
    private static $elementor_registered = false;
    private static $surfaces = array(
        'booking' => array( 'title' => 'Yatoon Booking', 'shortcode' => 'yatoon_booking', 'icon' => 'calendar-alt' ),
        'service-menu' => array( 'title' => 'Yatoon Service Menu', 'shortcode' => 'yatoon_service_menu', 'icon' => 'index-card' ),
        'staff-portal' => array( 'title' => 'Yatoon Staff Portal', 'shortcode' => 'yatoon_staff_portal', 'icon' => 'groups' ),
        'customer-portal' => array( 'title' => 'Yatoon Customer Portal', 'shortcode' => 'yatoon_customer_portal', 'icon' => 'id' ),
    );

    public static function init() {
        add_action( 'init', array( __CLASS__, 'register_blocks' ) );
        add_action( 'elementor/widgets/register', array( __CLASS__, 'register_elementor_widget' ) );
        add_action( 'elementor/widgets/widgets_registered', array( __CLASS__, 'register_elementor_widget_legacy' ) );
    }

    public static function register_blocks() {
        if ( ! function_exists( 'register_block_type' ) ) {
            return;
        }
        foreach ( self::$surfaces as $slug => $surface ) {
            register_block_type(
                'yatoon/' . $slug,
                array(
                    'api_version' => 2,
                    'title' => $surface['title'],
                    'description' => 'Display the ' . $surface['title'] . ' experience.',
                    'category' => 'widgets',
                    'icon' => $surface['icon'],
                    'supports' => array( 'html' => false, 'align' => array( 'wide', 'full' ) ),
                    'render_callback' => function() use ( $surface ) {
                        return do_shortcode( '[' . $surface['shortcode'] . ']' );
                    },
                )
            );
        }
    }

    public static function register_elementor_widget( $widgets_manager ) {
        if ( self::$elementor_registered || ! class_exists( '\Elementor\Widget_Base' ) ) {
            return;
        }
        $widget = new class extends \Elementor\Widget_Base {
            public function get_name() {
                return 'yatoon-surface';
            }
            public function get_title() {
                return 'Yatoon Booking';
            }
            public function get_icon() {
                return 'eicon-calendar';
            }
            public function get_categories() {
                return array( 'general' );
            }
            protected function register_controls() {
                $this->start_controls_section( 'content', array( 'label' => 'Yatoon' ) );
                $this->add_control(
                    'surface',
                    array(
                        'label' => 'Display',
                        'type' => \Elementor\Controls_Manager::SELECT,
                        'default' => 'yatoon_booking',
                        'options' => array(
                            'yatoon_booking' => 'Booking form',
                            'yatoon_service_menu' => 'Service menu',
                            'yatoon_staff_portal' => 'Staff portal',
                            'yatoon_customer_portal' => 'Customer portal',
                        ),
                    )
                );
                $this->end_controls_section();
            }
            protected function render() {
                $settings = $this->get_settings_for_display();
                $allowed = array( 'yatoon_booking', 'yatoon_service_menu', 'yatoon_staff_portal', 'yatoon_customer_portal' );
                $shortcode = in_array( $settings['surface'] ?? '', $allowed, true ) ? $settings['surface'] : 'yatoon_booking';
                echo do_shortcode( '[' . $shortcode . ']' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
            }
        };
        $widgets_manager->register( $widget );
        self::$elementor_registered = true;
    }

    public static function register_elementor_widget_legacy() {
        if ( did_action( 'elementor/widgets/register' ) || ! class_exists( '\Elementor\Plugin' ) ) {
            return;
        }
        self::register_elementor_widget( \Elementor\Plugin::instance()->widgets_manager );
    }
}
