<?php
/**
 * Modern Wheel Editor Template
 *
 * @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

// Get wheel data
$wheel_id = $post->ID;

// Get global settings from Settings page
$global_settings = get_option('swp_settings', []);

// Get wheel-specific data with global defaults
$wheel_type = get_post_meta($wheel_id, '_swp_wheel_type', true) ?: 'big_wheel';
$wheel_mode = get_post_meta($wheel_id, '_swp_wheel_mode', true) ?: ($global_settings['default_wheel_mode'] ?? 'instant');
$settings = get_post_meta($wheel_id, '_swp_settings', true) ?: [];
$prizes = get_post_meta($wheel_id, '_swp_prizes', true) ?: [];
$styles = get_post_meta($wheel_id, '_swp_styles', true) ?: [];
$form_settings = get_post_meta($wheel_id, '_swp_form_settings', true) ?: [];

// Set defaults
if (empty($prizes)) {
    $prizes = [
        ['label' => __('Prize 1', 'spin-wheel'), 'code' => 'PRIZE1', 'probability' => 25, 'is_winning' => 1, 'color' => '#ff6b6b'],
        ['label' => __('Prize 2', 'spin-wheel'), 'code' => 'PRIZE2', 'probability' => 25, 'is_winning' => 1, 'color' => '#4ecdc4'],
        ['label' => __('Prize 3', 'spin-wheel'), 'code' => 'PRIZE3', 'probability' => 25, 'is_winning' => 1, 'color' => '#ffd93d'],
        ['label' => __('Try Again', 'spin-wheel'), 'code' => '', 'probability' => 25, 'is_winning' => 0, 'color' => '#95e1d3'],
    ];
}

if (empty($styles)) {
    $styles = [
        'width' => $global_settings['default_width'] ?? 460,
        'height' => $global_settings['default_height'] ?? 460,
        'background_color' => '#ffffff',
        'button_text' => $global_settings['default_button_text'] ?? 'SPIN',
        'button_color' => '#667eea',
        'button_text_color' => '#ffffff',
    ];
}

if (empty($form_settings)) {
    $form_settings = [
        'title' => __('Spin to Win!', 'spin-wheel'),
        'description' => __('Enter your details and spin the wheel for a chance to win amazing prizes!', 'spin-wheel'),
        'name_label' => __('Your Name', 'spin-wheel'),
        'email_label' => __('Your Email', 'spin-wheel'),
        'phone_label' => __('Phone Number', 'spin-wheel'),
        'show_name' => 1,
        'show_email' => 1,
        'show_phone' => 1,
        'require_name' => 1,
        'require_email' => 1,
        'require_phone' => 0,
        'enable_terms_checkbox' => 0,
        'terms_label' => __('I agree to the terms and conditions.', 'spin-wheel'),
        'button_text' => $global_settings['default_submit_button_text'] ?? 'Spin Now!',
    ];
} else {
    // Apply global default if button_text is not set
    if (empty($form_settings['button_text'])) {
        $form_settings['button_text'] = $global_settings['default_submit_button_text'] ?? 'Spin Now!';
    }
}

$form_settings = swp_normalize_form_settings($form_settings);

// Apply global defaults to settings if not already set
if (!isset($settings['one_entry_per_user'])) {
    $settings['one_entry_per_user'] = $global_settings['one_entry_per_user'] ?? 1;
}
if (!isset($settings['enable_winner_notifications'])) {
    $settings['enable_winner_notifications'] = $global_settings['enable_winner_notifications'] ?? 1;
}
if (!isset($settings['enable_confetti'])) {
    $settings['enable_confetti'] = $global_settings['enable_confetti'] ?? 1;
}
if (!isset($settings['enable_animations'])) {
    $settings['enable_animations'] = $global_settings['enable_animations'] ?? 1;
}

wp_nonce_field('swp_save_metabox', 'swp_metabox_nonce');
?>

<div class="swp-modern-editor">
    <!-- Two Column Layout -->
    <div class="swp-editor-container">

        <!-- Left Panel: Settings -->
        <div class="swp-editor-left">

            <!-- Tab Navigation -->
            <div class="swp-tabs-nav">
                <button type="button" class="swp-tab-btn active" data-tab="general">
                    <span class="dashicons dashicons-admin-settings"></span>
                    <?php esc_html_e('General', 'spin-wheel'); ?>
                </button>
                <button type="button" class="swp-tab-btn" data-tab="prizes">
                    <span class="dashicons dashicons-awards"></span>
                    <?php esc_html_e('Prizes', 'spin-wheel'); ?>
                </button>
                <button type="button" class="swp-tab-btn" data-tab="display">
                    <span class="dashicons dashicons-visibility"></span>
                    <?php esc_html_e('Display Rules', 'spin-wheel'); ?>
                </button>
                <button type="button" class="swp-tab-btn" data-tab="design">
                    <span class="dashicons dashicons-art"></span>
                    <?php esc_html_e('Design', 'spin-wheel'); ?>
                </button>
                <button type="button" class="swp-tab-btn" data-tab="form">
                    <span class="dashicons dashicons-forms"></span>
                    <?php esc_html_e('Form', 'spin-wheel'); ?>
                </button>
            </div>

            <!-- Tab Content -->
            <div class="swp-tabs-content">

                <!-- General Settings Tab -->
                <div class="swp-tab-content active" id="tab-general">
                    <?php include SPIN_WHEEL_PATH . 'admin/templates/tabs/tab-general.php'; ?>
                </div>

                <!-- Prizes Tab -->
                <div class="swp-tab-content" id="tab-prizes">
                    <?php include SPIN_WHEEL_PATH . 'admin/templates/tabs/tab-prizes.php'; ?>
                </div>

                <!-- Display Rules Tab -->
                <div class="swp-tab-content" id="tab-display">
                    <?php include SPIN_WHEEL_PATH . 'admin/templates/tabs/tab-display.php'; ?>
                </div>

                <!-- Design Tab -->
                <div class="swp-tab-content" id="tab-design">
                    <?php include SPIN_WHEEL_PATH . 'admin/templates/tabs/tab-design.php'; ?>
                </div>

                <!-- Form Tab -->
                <div class="swp-tab-content" id="tab-form">
                    <?php include SPIN_WHEEL_PATH . 'admin/templates/tabs/tab-form.php'; ?>
                </div>

            </div>
        </div>

    </div>
    
    <!-- Preview Toggle Button -->
    <button type="button" class="swp-preview-toggle" id="swp-preview-toggle">
        <span class="dashicons dashicons-visibility"></span>
        <span class="swp-toggle-text"><?php esc_html_e('Preview', 'spin-wheel'); ?></span>
    </button>
</div>

<!-- Offcanvas Preview Sidebar -->
<div class="swp-preview-offcanvas" id="swp-preview-offcanvas">
    <div class="swp-preview-offcanvas-overlay" id="swp-preview-overlay"></div>
    
    <div class="swp-preview-offcanvas-panel">
        <div class="swp-preview-header">
            <h3>
                <span class="dashicons dashicons-visibility"></span>
                <?php esc_html_e('Live Preview', 'spin-wheel'); ?>
            </h3>
            <div class="swp-preview-actions">
                <button type="button" class="button swp-refresh-preview">
                    <span class="dashicons dashicons-update"></span>
                    <?php esc_html_e('Refresh', 'spin-wheel'); ?>
                </button>
                <button type="button" class="swp-preview-close" id="swp-preview-close">
                    <span class="dashicons dashicons-no-alt"></span>
                </button>
            </div>
        </div>

        <div class="swp-preview-content" id="swp-live-preview">
            <div class="swp-preview-loading">
                <span class="spinner is-active"></span>
                <p><?php esc_html_e('Loading preview...', 'spin-wheel'); ?></p>
            </div>
        </div>

        <div class="swp-preview-info">
            <p class="description">
                <?php esc_html_e('This is a live preview of your spin wheel. Changes are reflected in real-time.', 'spin-wheel'); ?>
            </p>
        </div>
    </div>
</div>