<?php
defined('ABSPATH') || exit;
class Termin_Kalender_Block_List
{
    protected $manager;
    private static $instance;

    public function __construct()
    {
        $this->manager = Termin_Kalender_Manager::get_instance();
        add_action('init', [$this, 'register_block']);
    }

    public static function get_instance()
    {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    //---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
    public function register_block()
    {
        $terminKalenderData = $this->manager->get_termin_kalender_data();
        //----------------
        wp_register_script('termin_kalender_namespace', Termin_Kalender_Plugin::$plugin_url . 'js/termin_kalender_namespace.js', ['jquery'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        //------------------
        // JS for Simple List
        wp_register_script(
            'gutenberg_block_simple_list',
            Termin_Kalender_Plugin::$plugin_url . 'js/gutenberg_block_simple_list.js',
            ['termin_kalender_namespace', 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor'],
            Termin_Kalender_Plugin::TER_KAL_TK_VERSION,
            true
        );
        //wp_enqueue_script('gutenberg_block_simple_list');
        wp_set_script_translations('gutenberg_block_simple_list', 'termin-kalender', Termin_Kalender_Plugin::$plugin_dir_path . 'languages');

        // JS for Calendar Block  //'termin_kalender_namespace',
        wp_register_script(
            'gutenberg_block_calendar',
            Termin_Kalender_Plugin::$plugin_url . 'js/gutenberg_block_calendar.js',
            ['termin_kalender_namespace', 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor'],
            Termin_Kalender_Plugin::TER_KAL_TK_VERSION,
            true
        );
        //wp_enqueue_script('gutenberg_block_calendar');
        // Localize data for both
        wp_localize_script('gutenberg_block_calendar', 'terminKalenderData', [
            'TER_KAL_PLUGIN_URL' => Termin_Kalender_Plugin::$plugin_url,
            'TERMIN_KALENDER_KATEGORIEN' => $terminKalenderData['TERMIN_KALENDER_KATEGORIEN'] ?? [],
        ]);
        wp_set_script_translations('gutenberg_block_calendar', 'termin-kalender', Termin_Kalender_Plugin::$plugin_dir_path . 'languages');


        register_block_type('termin-kalender/kalender-block', [
            'render_callback' => [$this, 'render_block_calendar'],
            'editor_script' => 'gutenberg_block_calendar', // localize script in ADMIN register_enqueue
            'script' => 'gutenberg_block_calendar_frontend', // FRONTSIDE js rendering
            'style' => 'terminkalender_calendar_css', // frontside css
/*            'attributes' => [
                'show_time' => [
                    'type' => 'string',
                    'default' => 'start_end'
                ],
                'hidden_categories' => [
                    'type' => 'array',
                    'default' => array()
                ],
                'hide_category_buttons' => [
                    'type' => 'boolean',
                    'default' => false
                ]
            ],*/
        ]);


        register_block_type('termin-kalender/simplelist', [
            'render_callback' => [$this, 'render_block_simplelist'], // Call the method to render the block
            'editor_script' => 'gutenberg_block_simple_list', // Admin
            'script' => 'gutenberg_block_simple_list_frontend', // frontside js rendering
            'style' => 'terminkalender_simplelist_css', // frontside css
        ]);

        add_shortcode('termin-kalender-calendar', [$this, 'render_block_calendar']);
        add_shortcode('my-termin-kalender', [$this, 'render_block_calendar']); // legacy shortcode
        add_shortcode('termin-kalender-fullpage', [$this, 'render_block_calendar']); // legacy shortcode
        add_shortcode('termin-kalender-simple-list', [$this, 'render_block_simplelist']);

    }
    //---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
    public function render_block_simplelist($attributes)
    {
        $instance_id = $this->manager->get_next_block_instance_id();
        $this->enqueue_frontend_assets();
        //--------------------------------------------
        wp_enqueue_style('terminkalender_simplelist_css', Termin_Kalender_Plugin::$plugin_url . 'css/termin_kalender_simplelist.css', ['termin_kalender'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, 'all');
        wp_enqueue_script('gutenberg_block_simple_list_frontend', Termin_Kalender_Plugin::$plugin_url . 'js/gutenberg_block_simple_list_frontend.js', ['termin_kalender_namespace', 'jquery', 'fullcalendar_js', 'fullcalendar_locales', 'rrule_js', 'terminkalender_fullcalendar-rrule'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        static $localized_simple = false;
        if (!$localized_simple) {
            if (method_exists($this->manager, 'enqueue_frontend_data')) {
                $this->manager->enqueue_frontend_data();
            } else {
                $terminKalenderData = $this->manager->get_termin_kalender_data();
                wp_localize_script('gutenberg_block_simple_list_frontend', 'terminKalenderData', $terminKalenderData);
            }
            $localized_simple = true;
        }
        // wp_set_script_translations('gutenberg_block_simple_list_frontend', 'termin-kalender', Termin_Kalender_Plugin::$plugin_dir_path . 'languages');
        //--------------------------------------------
        $show_time = isset($attributes['show_time']) ? esc_attr($attributes['show_time']) : 'start_end';
        ob_start();
        ?>
        <div class="ter-kal-block-list" id="ter-kal-block-list<?php echo esc_attr($instance_id); ?>">
            <div id="eventlist_<?php echo esc_attr($instance_id); ?>" class="eventlist simple_list ter-kal-content"
                data-show_time="<?php echo esc_attr($show_time); ?>" data-instance="<?php echo esc_attr($instance_id); ?>">
                <p class="ter_kal_info ter_kal_info_<?php echo esc_attr($instance_id); ?>"></p>
                <hr>
                <h4 class="eventlist_no_content"><?php echo esc_html__('Loading data, please wait', 'termin-kalender'); ?></h4>
                <hr>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }

    //---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
    public function render_block_calendar($attributes)
    {
        $instance_id = $this->manager->get_next_block_instance_id();
        $this->enqueue_frontend_assets();
        //--------------------------------------------
        wp_enqueue_style('termin_kalender_ui_dialog', Termin_Kalender_Plugin::$plugin_url . 'css/termin_kalender_ui_dialog.css', [], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, 'all');
        wp_enqueue_style('termin_kalender_html5_dialog', Termin_Kalender_Plugin::$plugin_url . 'css/termin_kalender_html5_dialog.css', [], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, 'all');
        wp_enqueue_style('terminkalender_calendar_css', Termin_Kalender_Plugin::$plugin_url . 'css/termin_kalender_calendar.css', ['termin_kalender'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, 'all');
        wp_enqueue_script('common_front_cal_button', Termin_Kalender_Plugin::$plugin_url . 'js/common_front_cal_button.js', ['termin_kalender_namespace', 'jquery'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        wp_enqueue_script('gutenberg_block_calendar_frontend', Termin_Kalender_Plugin::$plugin_url . 'js/gutenberg_block_calendar_frontend.js', ['common_front_cal_button', 'termin_kalender_namespace', 'jquery', 'fullcalendar_js', 'rrule_js', 'terminkalender_fullcalendar-rrule'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        //--------------------------------------------
        static $localized_calendar = false;
        if (!$localized_calendar) {
            if (method_exists($this->manager, 'enqueue_frontend_data')) {
                $this->manager->enqueue_frontend_data();
            } else {
                $terminKalenderData = $this->manager->get_termin_kalender_data();
                wp_localize_script('gutenberg_block_calendar_frontend', 'terminKalenderData', $terminKalenderData);
            }
            $localized_calendar = true;
        }
        // wp_set_script_translations('gutenberg_block_calendar_frontend', 'termin-kalender', Termin_Kalender_Plugin::$plugin_dir_path . 'languages');
        $show_time = isset($attributes['show_time']) ? esc_attr($attributes['show_time']) : 'start_end';
        // ter_kal_CHANGE: Get hidden categories from block attributes
        $hidden_categories = isset($attributes['hidden_categories']) ? $attributes['hidden_categories'] : array();
        
        // ter_kal_CHANGE: Parse shortcode attribute string to array
        if (is_string($hidden_categories)) {
            $hidden_categories = array_filter(array_map('trim', explode(',', $hidden_categories)));
            $hidden_categories = array_values($hidden_categories);
        }
        
        $hidden_categories_json = wp_json_encode($hidden_categories);
        $hide_buttons = isset($attributes['hide_category_buttons']) ? $attributes['hide_category_buttons'] : false;
        
        // ter_kal_CHANGE: Parse shortcode boolean string
        if (is_string($hide_buttons)) {
            $hide_buttons = filter_var($hide_buttons, FILTER_VALIDATE_BOOLEAN);
        }
        
        ob_start();
        ?>
        <div id="ter-kal-block-kalender<?php echo esc_attr($instance_id); ?>" class="ter-kal-block-kalender">
            <div class="frontend_kalender wrap ter-kal-content" data-show_time="<?php echo esc_html($show_time); ?>"
                data-instance="<?php echo esc_attr($instance_id); ?>"
                data-hide-buttons="<?php echo esc_attr($hide_buttons ? '1' : '0'); ?>"
                data-hidden-categories="<?php echo esc_attr($hidden_categories_json); ?>">
                <div id="kategoriezeile_<?php echo esc_attr($instance_id); ?>" class="kategoriezeile"></div>
                <div id="ter_kal_simple_calendar_<?php echo esc_attr($instance_id); ?>" class="ter_kal_simple_calendar"></div>
                <dialog id="event-dialog-<?php echo esc_attr($instance_id); ?>" class="ter_kal_ui_dialog ter_kal_calendar_frontend">
                    <div class="ter_kal_dialog_header">
                        <h3 class="ter_kal_dialog_title"></h3>
                        <button type="button" class="ter_kal_dialog_close" title="<?php esc_attr_e('Close', 'termin-kalender'); ?>">
                            <span class="dashicons dashicons-no-alt"></span>
                        </button>
                    </div>
                    <div class="ter_kal_dialog_content">
                        <h3 id="event-title-<?php echo esc_attr($instance_id); ?>" class="event-title"></h3>
                        <hr>
                        <div id="event-time-<?php echo esc_attr($instance_id); ?>" class="event-time"></div>
                        <hr>
                        <div id="event-details-html-<?php echo esc_attr($instance_id); ?>" class="regular-text event-details-html"></div>
                        <div id="event-location_link-<?php echo esc_attr($instance_id); ?>"></div>
                    </div>
                    <div class="ter_kal_dialog_footer" id="event-footer-<?php echo esc_attr($instance_id); ?>"></div>
                </dialog>
            </div>
        </div>
        <?php
        return ob_get_clean();
    } // end  ter_kal_render_frontend_calendar
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
    public function enqueue_frontend_assets()
    {
        //-------------------------------------------------------------------------
        wp_enqueue_script('termin_kalender_namespace', Termin_Kalender_Plugin::$plugin_url . 'js/termin_kalender_namespace.js', ['jquery'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        //-------------------------------------------------------------------------
        require_once Termin_Kalender_Plugin::$plugin_dir_path . 'includes/enqueue_jquery_base.php';
        wp_enqueue_style('termin_kalender', Termin_Kalender_Plugin::$plugin_url . 'css/termin_kalender.css', [], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, 'all');

        wp_enqueue_script('fullcalendar_js', Termin_Kalender_Plugin::$plugin_url . 'dist/index.global.min.js', ['jquery', 'moment'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        wp_enqueue_script('fullcalendar_locales', Termin_Kalender_Plugin::$plugin_url . 'dist/locales-all.global.min.js', ['fullcalendar_js'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        wp_enqueue_script('rrule_js', Termin_Kalender_Plugin::$plugin_url . 'dist/rrule.js', [], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        //enqueue ?
        wp_register_script('terminkalender_fullcalendar-rrule', Termin_Kalender_Plugin::$plugin_url . 'dist/@fullcalendar/rrule/index.global.min.js', ['fullcalendar_js', 'rrule_js'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
        wp_enqueue_script('termin_kalender_common', Termin_Kalender_Plugin::$plugin_url . 'js/common.js', ['termin_kalender_namespace', 'jquery', 'fullcalendar_js', 'fullcalendar_locales', 'rrule_js', 'terminkalender_fullcalendar-rrule'], Termin_Kalender_Plugin::TER_KAL_TK_VERSION, true);
    }
    //---------------------------------------------------------------------------------------------------
} // end class Termin_Kalender_Block_List
//---------------------------------------------------------------------------------------------------

