<?php
// One Card Tarot Game Shortcode
defined('ABSPATH') or die('No!');

// Include configuration file using defined constant
include_once TAROTXHOROSCOPE_PLUGIN_DIR . 'api/config.php';

// AJAX handlers with plugin-specific prefix
add_action('wp_ajax_tarotxhoroscope_reset_onecard_game_and_chat', 'tarotxhoroscope_reset_onecard_game_and_chat_handler');
add_action('wp_ajax_nopriv_tarotxhoroscope_reset_onecard_game_and_chat', 'tarotxhoroscope_reset_onecard_game_and_chat_handler');

function tarotxhoroscope_reset_onecard_game_and_chat_handler()
{
    // Verify nonce for security
    check_ajax_referer('onecard_nonce', 'security');
    $output = tarotxhoroscope_onecard_shortcode();

    // Define allowed HTML tags and attributes for secure output
    $allowed_tags = array(
        'div' => array(
            'id' => array(),
            'class' => array(),
            'style' => array(),
            'onclick' => array(),
            'data-game-type' => array(),
            'data-ws-url' => array(),
            'data-token-url' => array()
        ),
        'h2' => array(
            'id' => array(),
            'style' => array()
        ),
        'img' => array(
            'decoding' => array(),
            'src' => array(),
            'class' => array(),
            'alt' => array(),
            'id' => array()
        ),
        'p' => array(
            'id' => array(),
            'style' => array()
        ),
        'button' => array(
            'id' => array(),
            'onclick' => array(),
            'style' => array(),
            'type' => array(),
            'class' => array()
        ),
        'form' => array(
            'class' => array(),
            'action' => array(),
            'method' => array()
        ),
        'input' => array(
            'type' => array(),
            'name' => array(),
            'placeholder' => array(),
            'style' => array(),
            'class' => array(),
            'id' => array()
        ),
        'style' => array(
            'type' => array()
        ),
        'script' => array(
            'type' => array(),
            'src' => array(),
            'defer' => array(),
            'async' => array()
        ),
        'link' => array(
            'rel' => array(),
            'type' => array(),
            'href' => array()
        ),
        'meta' => array(
            'name' => array(),
            'content' => array()
        ),
        'br' => array(),
        'span' => array(
            'class' => array(),
            'style' => array()
        ),
    );

    echo wp_kses($output, $allowed_tags);
    wp_die(); // Terminate AJAX request properly
}

function tarotxhoroscope_onecard_shortcode()
{
    global $tarotxhoroscope_oneCardChatUrl, $tarotxhoroscope_chatTokenUrl;

    // Fetch settings with plugin-specific option names
    $settings = get_option('tarotxhoroscope_one_card_settings', array());
    $chatbox_settings = get_option('tarotxhoroscope_chatbox_settings', array());

    $is_chat_enabled = isset($settings['enable_chat']) ? $settings['enable_chat'] : false;
    $enable_card_meaning = isset($settings['enable_card_meaning']) ? $settings['enable_card_meaning'] : false;
    $version = '1.0.4';

    // Enqueue styles and scripts using plugin URL constant
    wp_enqueue_style('onecard-styles', TAROTXHOROSCOPE_PLUGIN_URL . 'games/css/onecardstyles.css', array(), $version);
    wp_enqueue_script('onecard-js', TAROTXHOROSCOPE_PLUGIN_URL . 'games/js/onecard.js', array('jquery'), $version, true);

    // Localize script with necessary data
    wp_localize_script('onecard-js', 'onecardData', [
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('onecard_nonce'),
        'image_base' => TAROTXHOROSCOPE_PLUGIN_URL . 'games/images/tarotcards/'
    ]);

    if ($is_chat_enabled) {
        wp_enqueue_style('chat-styles', TAROTXHOROSCOPE_PLUGIN_URL . 'games/css/chatstyles.css', array(), $version);
        wp_enqueue_script('tah-chatbox-js', TAROTXHOROSCOPE_PLUGIN_URL . 'games/js/chatbox.js', array('jquery'), $version, true);

        wp_localize_script('tah-chatbox-js', 'chatboxSettings', array(
            'messageCap' => isset($chatbox_settings['message_cap']) ? intval($chatbox_settings['message_cap']) : 1,
            'userMessageBgColor' => isset($chatbox_settings['tah_user_message_bg_color']) ? esc_attr($chatbox_settings['tah_user_message_bg_color']) : '#ffffff',
            'userMessageFontColor' => isset($chatbox_settings['user_message_font_color']) ? esc_attr($chatbox_settings['user_message_font_color']) : '#000000',
            'userMessageFontSize' => isset($chatbox_settings['user_message_font_size']) ? esc_attr($chatbox_settings['user_message_font_size']) : '16px',
            'userMessageFontFamily' => isset($chatbox_settings['user_message_font_family']) ? esc_attr($chatbox_settings['user_message_font_family']) : 'Arial',
            'responseMessageBgColor' => isset($chatbox_settings['tah_bot_message_bg_color']) ? esc_attr($chatbox_settings['tah_bot_message_bg_color']) : '#ffffff',
            'responseMessageFontColor' => isset($chatbox_settings['bot_message_font_color']) ? esc_attr($chatbox_settings['bot_message_font_color']) : '#000000',
            'responseMessageFontSize' => isset($chatbox_settings['bot_message_font_size']) ? esc_attr($chatbox_settings['bot_message_font_size']) : '16px',
            'responseMessageFontFamily' => isset($chatbox_settings['bot_message_font_family']) ? esc_attr($chatbox_settings['bot_message_font_family']) : 'Arial',
            'tahChatWelcomeMessage' => isset($chatbox_settings['tah_chat_welcome_message']) ? esc_attr($chatbox_settings['tah_chat_welcome_message']) : 'Hello, please ask me your question.',
            'token_nonce' => wp_create_nonce('chat_token_nonce')
        ));
    }

    // Define variables with proper escaping
    $card_back_image = esc_url($settings['card_back_image'] ?? TAROTXHOROSCOPE_PLUGIN_URL . 'games/images/cardbacks/TarotandHoroscope-cardbg1.png');
    $button_bg_color = esc_attr($settings['button_bg_color'] ?? '#246ae2');
    $button_text_color = esc_attr($settings['button_text_color'] ?? '#ffffff');
    $button_font_size = esc_attr($settings['button_font_size'] ?? '16px');
    $round_edges = esc_attr($settings['round_edges'] ?? 6);
    $number_of_cards = esc_attr($settings['number_of_cards'] ?? 8);
    $game_width = esc_attr($settings['game_width'] ?? '100%');

    ob_start();
?>
    <div id="card-container" class="card-container" style="width: <?php echo esc_attr($game_width); ?>;">
        <?php for ($i = 0; $i < $number_of_cards; $i++) : ?>
            <div class="card" onclick="drawCards()">
                <img decoding="async" src="<?php echo esc_url($card_back_image); ?>" class="front" alt="Card Image" />
            </div>
        <?php endfor; ?>
        <div class="selected-card-details fade-in" style="display: none;">
            <h2 id="oneCardName" style="font-family: <?php echo esc_attr($settings['card_name_font_family']); ?>; font-size: <?php echo esc_attr($settings['card_name_font_size']); ?>; color: <?php echo esc_attr($settings['card_name_text_color']); ?>;"></h2>
            <img id="oneCardPicture" src="" alt="Selected Card Image" />
            <?php if ($enable_card_meaning) : ?>
                <p id="oneCardMeaning" style="font-family: <?php echo esc_attr($settings['card_meaning_font_family']); ?>; font-size: <?php echo esc_attr($settings['card_meaning_font_size']); ?>; color: <?php echo esc_attr($settings['card_meaning_text_color']); ?>;"></p>
            <?php endif; ?>
            <?php if ($is_chat_enabled) : ?>
                <div class="tah_chat-wrapper" data-game-type="onecard" data-ws-url="<?php echo esc_attr($tarotxhoroscope_oneCardChatUrl); ?>" data-token-url="<?php echo esc_attr(admin_url('admin-ajax.php?action=tarotxhoroscope_chat_token_api_call')); ?>" style="width: <?php echo esc_attr($game_width); ?>; <?php echo isset($chatbox_settings['tah_chat_border_enable']) && $chatbox_settings['tah_chat_border_enable'] ? "border: " . esc_attr($chatbox_settings['tah_chat_border_width']) . " solid " . esc_attr($chatbox_settings['tah_chat_border_color']) . "; border-radius: " . esc_attr($chatbox_settings['tah_chat_border_radius']) . ";" : ""; ?>">
                    <div id="overlay">
                        <button id="startChatButton">Start Chat</button>
                    </div>
                    <div class="tah_messages">
                        <div id="loading-space"></div>
                    </div>
                    <form class="tah_form">
                        <input type="hidden" name="security" value="<?php echo esc_attr(wp_create_nonce('chat_token_nonce')); ?>" />
                        <input type="text" name="message" placeholder="<?php echo esc_attr($chatbox_settings['input_placeholder_text'] ?? 'Type your message here...'); ?>" style="color: <?php echo esc_attr($chatbox_settings['input_text_color'] ?? '#403a3a'); ?>; font-family: <?php echo esc_attr($chatbox_settings['input_and_button_font_family'] ?? 'Arial'); ?>;" />
                        <button type="submit" style="background-color: <?php echo esc_attr($chatbox_settings['submit_button_color'] ?? '#3498db'); ?>; color: <?php echo esc_attr($chatbox_settings['submit_button_text_color'] ?? '#ffffff'); ?>; font-family: <?php echo esc_attr($chatbox_settings['input_and_button_font_family'] ?? 'Arial'); ?>;"><?php echo esc_attr($chatbox_settings['submit_button_text'] ?? 'Submit'); ?></button>
                    </form>
                    <p id="readingFinished" style="font-size:16px; display:none;"></p>
                </div>
            <?php endif; ?>
            <button id="drawAgainButton" onclick="resetGame()" class="draw-again-button" style="background-color: <?php echo esc_attr($button_bg_color); ?>; color: <?php echo esc_attr($button_text_color); ?>; font-size: <?php echo esc_attr($button_font_size); ?>; padding:10px 20px; border-radius: <?php echo esc_attr($round_edges); ?>px;">Draw Again</button>
        </div>
    </div>
<?php
    return ob_get_clean();
}

// Register the shortcode
add_shortcode('tarotxhoroscope_onecard', 'tarotxhoroscope_onecard_shortcode');
