<?php
/**
 * Usage metabox (shortcode copy vs site-wide auto-embed).
 *
 * @package Spin_Wheel
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Variables in this template are scoped to the including render method (not truly
// global); the plugin's established "swp" prefix is part of its data schema and hook API.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals

$swp_auto_embed_id = absint(get_option(SWP_OPTION_AUTO_EMBED_WHEEL_ID, 0));
$swp_is_auto_embed = ($swp_auto_embed_id === (int) $post->ID);
$swp_usage_value   = $swp_is_auto_embed ? 'auto_embed' : 'shortcode';
?>
<div class="swp-usage-metabox">
    <div class="swp-usage-metabox__field">
        <label for="swp_usage_mode" class="swp-usage-metabox__label">
            <?php esc_html_e('Display method', 'spin-wheel'); ?>
        </label>
        <select name="swp_usage_mode" id="swp_usage_mode" class="widefat swp-usage-metabox__select">
            <option value="shortcode" <?php selected($swp_usage_value, 'shortcode'); ?>>
                <?php esc_html_e('Shortcode', 'spin-wheel'); ?>
            </option>
            <option value="auto_embed" <?php selected($swp_usage_value, 'auto_embed'); ?>>
                <?php esc_html_e('Auto Embed', 'spin-wheel'); ?>
            </option>
        </select>
    </div>

    <div id="swp-usage-panel-shortcode"
         class="swp-usage-metabox__panel"
         role="region"
         aria-labelledby="swp_usage_mode"
         <?php echo $swp_usage_value === 'auto_embed' ? 'hidden' : ''; ?>>
        <p class="swp-usage-metabox__lead" id="swp-usage-shortcode-desc">
            <?php esc_html_e('Use this shortcode to display the wheel on any page or post:', 'spin-wheel'); ?>
        </p>
        <div class="swp-shortcode-box">
            <input type="text"
                   value="[spin_wheel id=&quot;<?php echo (int) $post->ID; ?>&quot;]"
                   readonly
                   class="large-text swp-shortcode-input"
                   id="swp_usage_shortcode_field"
                   onclick="this.select();"
                   aria-describedby="swp-usage-shortcode-desc swp-usage-block-desc"
                   autocomplete="off">
            <button type="button"
                    class="button swp-copy-shortcode"
                    data-clipboard-text='[spin_wheel id="<?php echo (int) $post->ID; ?>"]'>
                <?php esc_html_e('Copy', 'spin-wheel'); ?>
            </button>
        </div>
        <p class="description swp-usage-metabox__note" id="swp-usage-block-desc">
            <?php esc_html_e('You can also add this wheel using the Gutenberg block "Spin Wheel" in the block editor.', 'spin-wheel'); ?>
        </p>
    </div>

    <div id="swp-usage-panel-auto"
         class="swp-usage-metabox__panel swp-usage-metabox__panel--embed"
         role="region"
         aria-labelledby="swp_usage_mode"
         <?php echo $swp_usage_value === 'shortcode' ? 'hidden' : ''; ?>>
        <p class="swp-usage-metabox__lead">
            <?php esc_html_e('This wheel is injected automatically at the end of every front-end page.', 'spin-wheel'); ?>
        </p>
        <ul class="swp-usage-metabox__list">
            <li><?php esc_html_e('No shortcode or block is required on individual pages.', 'spin-wheel'); ?></li>
            <li><?php esc_html_e('Only one wheel can be auto-embedded site-wide; saving another wheel as Auto Embed replaces this choice.', 'spin-wheel'); ?></li>
            <li><?php esc_html_e('Display rules for this wheel (devices, pages, etc.) still apply.', 'spin-wheel'); ?></li>
        </ul>
        <?php if ($post->post_status !== 'publish') : ?>
            <p class="swp-usage-metabox__notice" role="status">
                <?php esc_html_e('Publish this wheel before Auto Embed can appear on the site.', 'spin-wheel'); ?>
            </p>
        <?php endif; ?>
    </div>
</div>
