<?php

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

/**
 * Provide a public-facing view for the plugin
 *
 * This file is used to markup the public-facing aspects of the plugin.
 *
 * @link       https://webequipe.com
 * @since      1.0.0
 *
 * @package    Webequipe_Spin_Win_Wheel
 * @subpackage Webequipe_Spin_Win_Wheel/public/partials/spin-part
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

global $webequipe_sww_spin_shortcode_id, $webequipe_sww_spin_default_id, $webequipe_sww_spin_default_min;
$webequipe_sww_spin_spin = webequipe_sww_get_spin($webequipe_sww_spin_shortcode_id);
$webequipe_sww_spin_spin_id = $webequipe_sww_spin_spin->id;
$webequipe_sww_spin_spin_labels = json_decode($webequipe_sww_spin_spin->spins, true);

$webequipe_sww_spin_display_fields = $webequipe_sww_spin_spin->display_field_setting;



$webequipe_sww_spin_spin_title = $webequipe_sww_spin_spin->spin_title;
$webequipe_sww_spin_spin_description = $webequipe_sww_spin_spin->spin_description;
$webequipe_sww_spin_spin_status = $webequipe_sww_spin_spin->spin_status;
$webequipe_sww_spin_schedule_state = ( 'active' === $webequipe_sww_spin_spin_status ) ? 'active' : 'inactive';
$webequipe_sww_spin_spin_display_setting = $webequipe_sww_spin_spin->spin_display_setting;

$webequipe_sww_spin_spin_type = $webequipe_sww_spin_spin->spin_type;
$webequipe_sww_spin_spin_position = $webequipe_sww_spin_spin->spin_position;
$webequipe_sww_spin_display_field_setting = $webequipe_sww_spin_spin->display_field_setting;

$webequipe_sww_spin_auto_minute = $webequipe_sww_spin_spin->auto_minute;

$webequipe_sww_spin_spin_duration_sec = $webequipe_sww_spin_spin->spin_duration;
$webequipe_sww_spin_spin_duration = $webequipe_sww_spin_spin_duration_sec * 1000;

$webequipe_sww_spin_display_after = $webequipe_sww_spin_spin->display_after;
$webequipe_sww_spin_display_after_same_email = $webequipe_sww_spin_spin->display_after_same_email;

$webequipe_sww_spin_template_setting = $webequipe_sww_spin_spin->template_setting;

$webequipe_sww_spin_card_background = $webequipe_sww_spin_spin->card_background;
$webequipe_sww_spin_card_color = $webequipe_sww_spin_spin->card_color;
$webequipe_sww_spin_card_boder_radius = $webequipe_sww_spin_spin->card_boder_radius;
$webequipe_sww_spin_card_boder_padding = $webequipe_sww_spin_spin->card_boder_padding;

$webequipe_sww_spin_wheel_center_background = $webequipe_sww_spin_spin->wheel_center_background;
$webequipe_sww_spin_wheel_center_boder_color = $webequipe_sww_spin_spin->wheel_center_boder_color;
$webequipe_sww_spin_wheel_center_boder_width = $webequipe_sww_spin_spin->wheel_center_boder_width;
$webequipe_sww_spin_wheel_center_image_icon = $webequipe_sww_spin_spin->wheel_center_image_icon;
$webequipe_sww_spin_custom_wheel_center_image_icon = $webequipe_sww_spin_spin->custom_wheel_center_image_icon ?? '';

$webequipe_sww_spin_wheel_stroke_border_color = $webequipe_sww_spin_spin->wheel_stroke_border_color;
$webequipe_sww_spin_wheel_stroke_border_size = $webequipe_sww_spin_spin->wheel_stroke_border_size;
$webequipe_sww_spin_wheel_stroke_font_size = $webequipe_sww_spin_spin->wheel_stroke_font_size;
$webequipe_sww_spin_wheel_stroke_font_family = $webequipe_sww_spin_spin->wheel_stroke_font_family;
$webequipe_sww_spin_wheel_stroke_font_style = $webequipe_sww_spin_spin->wheel_stroke_font_style;

$webequipe_sww_spin_from_design_alinment = $webequipe_sww_spin_spin->from_design_alinment;
$webequipe_sww_spin_from_button_text = $webequipe_sww_spin_spin->from_button_text;
$webequipe_sww_spin_from_button_color = $webequipe_sww_spin_spin->from_button_color;
$webequipe_sww_spin_from_button_background_color = $webequipe_sww_spin_spin->from_button_background_color;

$webequipe_sww_spin_initial_button_text = $webequipe_sww_spin_spin->initial_button_text;
$webequipe_sww_spin_initial_button_color = $webequipe_sww_spin_spin->initial_button_color;
$webequipe_sww_spin_initial_button_background_color = $webequipe_sww_spin_spin->initial_button_background_color;

$webequipe_sww_spin_win_message = $webequipe_sww_spin_spin->win_message;
$webequipe_sww_spin_loss_message = $webequipe_sww_spin_spin->loss_message;

$webequipe_sww_spin_send_email = $webequipe_sww_spin_spin->send_email;
$webequipe_sww_spin_from_name = $webequipe_sww_spin_spin->from_name;
$webequipe_sww_spin_from_address = $webequipe_sww_spin_spin->from_address;
$webequipe_sww_spin_from_subject = $webequipe_sww_spin_spin->from_subject;
$webequipe_sww_spin_from_message = $webequipe_sww_spin_spin->from_message;

$webequipe_sww_spin_admin_geting_email = $webequipe_sww_spin_spin->admin_geting_email;

// Decode JSON if needed
if (!empty($webequipe_sww_spin_display_fields) && is_string($webequipe_sww_spin_display_fields)) {
    $webequipe_sww_spin_display_fields = json_decode($webequipe_sww_spin_display_fields, true);
}

if (!is_array($webequipe_sww_spin_display_fields)) {
    $webequipe_sww_spin_display_fields = [];
}

if (!function_exists('webequipe_sww_spin_minify_css')) {
    function webequipe_sww_spin_minify_css($css)
    {
        // Remove comments
        $css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);

        // Remove unnecessary whitespace
        $css = preg_replace('/\s+/', ' ', $css);

        // Remove whitespace around specific characters
        $css = preg_replace('/\s*([{}:;,>+~])\s*/', '$1', $css);

        $css = str_replace(';}', '}', $css);

        // Remove any remaining leading/trailing whitespace
        return trim($css);
    }
}

if ('left' === $webequipe_sww_spin_spin_position) {
    $webequipe_sww_spin_spin_position_class = 'left';
    $webequipe_sww_spin_circleWidth_genarel = 650;
    $webequipe_sww_spin_circleWidth_400 = 300;
    $webequipe_sww_spin_circleWidth_575 = 320;
    $webequipe_sww_spin_circleWidth_768 = 420;
    $webequipe_sww_spin_circleWidth_1200 = 520;
    $webequipe_sww_spin_circleWidth_1400 = 540;
    $webequipe_sww_spin_circleWidth_1600 = 580;
} elseif ('right' === $webequipe_sww_spin_spin_position) {
    $webequipe_sww_spin_spin_position_class = 'right';
    $webequipe_sww_spin_circleWidth_genarel = 650;
    $webequipe_sww_spin_circleWidth_400 = 300;
    $webequipe_sww_spin_circleWidth_575 = 320;
    $webequipe_sww_spin_circleWidth_768 = 420;
    $webequipe_sww_spin_circleWidth_1200 = 520;
    $webequipe_sww_spin_circleWidth_1400 = 540;
    $webequipe_sww_spin_circleWidth_1600 = 580;
} else {
    $webequipe_sww_spin_spin_position_class = 'popup';
    $webequipe_sww_spin_circleWidth_genarel = 450;
    $webequipe_sww_spin_circleWidth_400 = 300;
    $webequipe_sww_spin_circleWidth_575 = 320;
    $webequipe_sww_spin_circleWidth_768 = 450;
    $webequipe_sww_spin_circleWidth_1200 = 450;
    $webequipe_sww_spin_circleWidth_1400 = 450;
    $webequipe_sww_spin_circleWidth_1600 = 450;

    if ($webequipe_sww_spin_wheel_stroke_font_size > 18) {
        $webequipe_sww_spin_wheel_stroke_font_size = 18;
    }
}



// CSS variables are now added via wp_add_inline_style() in the shortcode handler
// This ensures proper WordPress enqueue standards compliance








if ($webequipe_sww_spin_wheel_center_image_icon === 'custom') {
    if (!empty($webequipe_sww_spin_custom_wheel_center_image_icon)) {
        $webequipe_sww_spin_center_icon = $webequipe_sww_spin_custom_wheel_center_image_icon;
    } else {
        // Fallback to a valid bundled icon instead of non-existent custom.png
        $webequipe_sww_spin_center_icon = WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/img/cbl_logo.png';
    }
} else {
    $webequipe_sww_spin_center_icon = WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/img/' . $webequipe_sww_spin_wheel_center_image_icon . '.png';
}

$webequipe_sww_spin_arrow_icon = WEBEQUIPE_SPIN_WIN_WHEEL_ASSETS . '/img/wheel-arrow.png';

if ('spin_auto' === $webequipe_sww_spin_spin_type || $webequipe_sww_spin_default_id !== 0) {
    $webequipe_sww_spin_spin_type_class = 'spinner-type-spin_auto';
} else {
    $webequipe_sww_spin_spin_type_class = 'spinner-type-spin_button';
}
