<?php

if (!defined('ABSPATH')) {
    exit;
}

class List2Play_Servers_Block {
    
    private $api;
    private $renderer;
    
    public function __construct($api, $renderer) {
        $this->api = $api;
        $this->renderer = $renderer;
    }
    
    /**
     * Register the block
     */
    public function register() {
        // Register block using block.json
        $block_json_path = LIST2PLAY_SERVERS_PLUGIN_DIR . 'blocks/list2play-servers/block.json';
        
        if (file_exists($block_json_path)) {
            // Use block.json registration (WordPress 5.8+)
            register_block_type($block_json_path, [
                'render_callback' => [$this, 'render_block'],
            ]);
        } else {
            // Fallback: manual registration
            wp_register_script(
                'list2play-servers-block-editor',
                LIST2PLAY_SERVERS_PLUGIN_URL . 'blocks/list2play-servers/block-editor.js',
                ['wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n'],
                LIST2PLAY_SERVERS_VERSION,
                true
            );
            
            wp_register_style(
                'list2play-servers-block-editor',
                LIST2PLAY_SERVERS_PLUGIN_URL . 'blocks/list2play-servers/block-editor.css',
                [],
                LIST2PLAY_SERVERS_VERSION
            );
            
            register_block_type('list2play/servers', [
                'editor_script' => 'list2play-servers-block-editor',
                'editor_style' => 'list2play-servers-block-editor',
                'render_callback' => [$this, 'render_block'],
            ]);
        }
        
        // Localize script if registered
        if (wp_script_is('list2play-servers-block-editor', 'registered')) {
            wp_localize_script('list2play-servers-block-editor', 'list2playBlock', [
                'apiBaseUrl' => get_option('list2play_api_base_url', 'https://list2play.net/api'),
            ]);
        }
    }
    
    /**
     * Server-side block rendering
     */
    public function render_block($attributes) {
        // Get default per_page from settings
        $default_per_page = get_option('list2play_default_per_page', 20);
        
        // Block always uses selected servers from admin settings
        // Get selected server IDs from admin settings
        $selected_ids = get_option('list2play_selected_server_ids', []);
        
        // Merge with defaults (no mode - always uses selected servers)
        // Use admin settings as defaults for show_stats, show_votes, show_vote_button
        $default_show_stats = get_option('list2play_show_stats', '1') === '1' ? 'true' : 'false';
        $default_show_votes = get_option('list2play_show_votes', '1') === '1' ? 'true' : 'false';
        $default_show_vote_button = get_option('list2play_show_vote_button', '1') === '1' ? 'true' : 'false';
        $default_style_preset = sanitize_key((string) get_option('list2play_default_style_preset', 'default'));
        if ($default_style_preset === '') {
            $default_style_preset = 'default';
        }
        
        $atts = wp_parse_args($attributes, [
            'server_ids' => !empty($selected_ids) ? implode(',', $selected_ids) : '',
            'game_type' => '',
            'category' => '',
            'country' => '',
            'status' => '',
            'sort' => 'votes',
            'direction' => 'desc',
            'per_page' => $default_per_page,
            'show_stats' => $default_show_stats,
            'show_votes' => $default_show_votes,
            'show_vote_button' => $default_show_vote_button,
            'style_preset' => $default_style_preset,
        ]);
        
        // Convert booleans to strings for shortcode compatibility
        if (is_bool($atts['show_stats'])) {
            $atts['show_stats'] = $atts['show_stats'] ? 'true' : 'false';
        }
        if (is_bool($atts['show_votes'])) {
            $atts['show_votes'] = $atts['show_votes'] ? 'true' : 'false';
        }
        if (is_bool($atts['show_vote_button'])) {
            $atts['show_vote_button'] = $atts['show_vote_button'] ? 'true' : 'false';
        }
        
        // Use shortcode class to render with mode='selected' (reuse existing logic)
        $atts['mode'] = 'selected';
        $shortcode = new List2Play_Servers_Shortcode($this->api, $this->renderer);
        return $shortcode->render($atts);
    }
}

