<?php

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

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

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

$webequipe_sww_spin_plugin_url = plugin_dir_url(dirname(dirname(dirname(__FILE__)))); 

$webequipe_sww_spin = (isset($spin) && $spin) ? $spin : null;

if (!$webequipe_sww_spin) {
    $webequipe_sww_spin = (object) [

        // Basic Information
        'spin_title' => 'Spin & Win – Try Your Luck!',
        'spin_description' => 'Spin the wheel and grab exciting rewards! Enter your email to play.',

        // status
        'spin_status' => 'active',

        // Display Settings
        'spin_display_setting' => ["laptop", "tablet", "mobile"],

        // Spin Display Settings
        'spin_type' => 'spin_auto',
        'spin_position' => 'left',
        'display_field_setting' => ["name_field", "email_field", "mobile_field"],
        
        // Spin Behavioral Settings
        'auto_minute' => 10,
        'spin_duration' => 3,
        'display_after' => 1,
        'display_after_same_email' => 5,

        // Template
        'template_setting' => 'spin-on-left',

        // Card Setting
        'card_background' => '#E6F6F4',
        'card_color' => '#000000',
        'card_boder_radius' => '0',
        'card_boder_padding' => '15',

        // Wheel Center icon setting
        'wheel_center_background' => '#ffffff',
        'wheel_center_boder_color' => '#003B33',
        'wheel_center_boder_width' => '3',
        'wheel_center_image_icon' => 'star',
        


        // Wheel Stroke Setting
        'wheel_stroke_border_color' => '#003B33',
        'wheel_stroke_border_size' => '5',
        'wheel_stroke_font_size' => '20',
        'wheel_stroke_font_family' => 'roboto',
        'wheel_stroke_font_style' => 'bold',

        // Form Design Setting
        'from_design_alinment' => 'left',
        'from_button_text' => 'Try Your Luck',
        'from_button_color' => '#FFFFFF',
        'from_button_background_color' => '#009883',

        // Initial Button Setting
        'initial_button_text' => 'Spin to win',
        'initial_button_color' => '#FFFFFF',
        'initial_button_background_color' => '#009883',

        // Message Setting
        'win_message' => '🎉 Congratulations! You won {label} worth {value}. Check your email for more details!',
        'loss_message' => 'Better luck next time! Don’t give up — spin again.',
        
        // Email Settimg
        'send_email' => 'yes',
        'from_name'    => get_bloginfo('name'),
        'from_address' => get_bloginfo('admin_email'),
        'from_subject' => 'Your Spin & Win Result!',
        'from_message' => 'Hi {customer_name}, <br> You played Spin & Win today and got {spin_label} worth {spin_value}. <br> We’ll share more details with you shortly. <br>  Thank you for participating!',
        
        // Notification Setting
        'admin_geting_email' => 'yes',


        
        // Add any other fields you use in the form
    ];
}

// Maintain backward compatibility with existing templates relying on $spin.
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Back-compat alias for included templates.
$spin = $webequipe_sww_spin;

$webequipe_sww_spin_spin_type = $webequipe_sww_spin->spin_type;
$webequipe_sww_spin_spin_position = $webequipe_sww_spin->spin_position;
$webequipe_sww_spin_wheel_center_image_icon = $webequipe_sww_spin->wheel_center_image_icon;
$webequipe_sww_spin_wheel_stroke_font_style = $webequipe_sww_spin->wheel_stroke_font_style;
$webequipe_sww_spin_wheel_stroke_font_family = $webequipe_sww_spin->wheel_stroke_font_family;
$webequipe_sww_spin_from_design_alignment = $webequipe_sww_spin->from_design_alinment;
$webequipe_sww_spin_send_email = $webequipe_sww_spin->send_email;
$webequipe_sww_spin_admin_getting_email = $webequipe_sww_spin->admin_geting_email;
$webequipe_sww_spin_display_settings = $webequipe_sww_spin->spin_display_setting;

// Go up two levels
$webequipe_sww_spin_templates = [
    'spin-on-left'   => $webequipe_sww_spin_plugin_url . 'assets/img/horizotal.png',
    'spin-on-right'  => $webequipe_sww_spin_plugin_url . 'assets/img/horizotal_reverse.png',
    'spin-on-top'    => $webequipe_sww_spin_plugin_url . 'assets/img/virtical.png',
    'spin-on-bottom' => $webequipe_sww_spin_plugin_url . 'assets/img/virtical_reverse.png',
];


$webequipe_sww_spin_spins_items = isset($webequipe_sww_spin->spins) ? json_decode($webequipe_sww_spin->spins, true) : [
    [
        "is_win"      => "Yes",
        "spin_probability"=> "20",
        "spin_label"=> "5% OFF",
        "spin_value"=> "5OFF",
        "spin_bgcolor" => "#004C41",
        "spin_textcolor"  => "#FFFFFF",
    ],
    [
        "is_win"      => "yes",
        "spin_probability"=> "15",
        "spin_label"=> "10% OFF",
        "spin_value"=> "10OFF",
        "spin_bgcolor"    => "#008774",
        "spin_textcolor"  => "#000000",
    ],
    [
        "is_win"      => "Yes",
        "spin_probability"=> "10",
        "spin_label"=> "Free Shipping",
        "spin_value"=> "FREESHIP",
        "spin_bgcolor"    => "#B0E4DD",
        "spin_textcolor"  => "#000000",
    ],
    [
        "is_win"      => "yes",
        "spin_probability"=> "10",
        "spin_label"=> "15% OFF",
        "spin_value"=> "15OFF",
        "spin_bgcolor"    => "#D9F2EF",
        "spin_textcolor"  => "#000000",
    ],
    [
        "is_win"      => "no",
        "spin_probability"=> "20",
        "spin_label"=> "Try Again",
        "spin_value"=> "ZERO",
        "spin_bgcolor" => "#004C41",
        "spin_textcolor"  => "#FFFFFF",
    ],
    [
        "is_win"      => "yes",
        "spin_probability"=> "8",
        "spin_label"=> "20% OFF",
        "spin_value"=> "20OFF",
        "spin_bgcolor"    => "#008774",
        "spin_textcolor"  => "#000000",
    ],
    [
        "is_win"      => "yes",
        "spin_probability"=> "7",
        "spin_label"=> "Mystery Gift",
        "spin_value"=> "GIFT",
        "spin_bgcolor"    => "#B0E4DD",
        "spin_textcolor"  => "#000000",
    ],
    [
        "is_win"      => "no",
        "spin_probability"=> "10",
        "spin_label"=> "No Luck 😢",
        "spin_value"=> "ZERO",
        "spin_bgcolor"    => "#D9F2EF",
        "spin_textcolor"  => "#000000",
    ]
];

$webequipe_sww_spin_total_items = is_array($webequipe_sww_spin_spins_items) ? count($webequipe_sww_spin_spins_items) : 0; 


// Decode JSON if it's a string
if (!empty($webequipe_sww_spin_display_settings) && is_string($webequipe_sww_spin_display_settings)) {
    $webequipe_sww_spin_display_settings = json_decode($webequipe_sww_spin_display_settings, true);
}

// Fallback to empty array if still not valid
if (!is_array($webequipe_sww_spin_display_settings)) {
    $webequipe_sww_spin_display_settings = [];
}


$webequipe_sww_spin_display_fields = $webequipe_sww_spin->display_field_setting;

// 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 = [];
}

$wsww_sync_defaults = [
    'enabled'      => 1,
    'provider'     => 'mailchimp',
    'capture_mode' => 'both',
];

$wsww_sync_settings_map = get_option( 'wsww_spin_sync_settings', [] );
if ( ! is_array( $wsww_sync_settings_map ) ) {
    $wsww_sync_settings_map = [];
}

$wsww_current_spin_id = isset( $webequipe_sww_spin->id ) ? absint( $webequipe_sww_spin->id ) : 0;
$wsww_current_sync    = $wsww_sync_defaults;

if ( $wsww_current_spin_id > 0 && isset( $wsww_sync_settings_map[ $wsww_current_spin_id ] ) && is_array( $wsww_sync_settings_map[ $wsww_current_spin_id ] ) ) {
    $wsww_current_sync = wp_parse_args( $wsww_sync_settings_map[ $wsww_current_spin_id ], $wsww_sync_defaults );
}

$wsww_sync_enabled      = ! empty( $wsww_current_sync['enabled'] );
$wsww_sync_provider     = isset( $wsww_current_sync['provider'] ) ? sanitize_key( (string) $wsww_current_sync['provider'] ) : 'mailchimp';
$wsww_sync_capture_mode = isset( $wsww_current_sync['capture_mode'] ) ? sanitize_key( (string) $wsww_current_sync['capture_mode'] ) : 'both';

$wsww_mailchimp_connected = false;
if ( class_exists( '\Webequipe\SpinWinWheel\Mailchimp' ) ) {
    $wsww_mailchimp_settings = \Webequipe\SpinWinWheel\Mailchimp::get_settings();
    if ( is_array( $wsww_mailchimp_settings ) ) {
        $wsww_mailchimp_connected = (
            ! empty( $wsww_mailchimp_settings['enabled'] )
            && ! empty( $wsww_mailchimp_settings['api_key'] )
            && ! empty( $wsww_mailchimp_settings['list_id'] )
        );
    }
}

$wsww_sync_available = $wsww_mailchimp_connected;
if ( ! $wsww_sync_available ) {
    $wsww_sync_enabled = false;
}

$wsww_mailchimp_settings_link = admin_url( 'admin.php?page=wsww-spin-settings&wsww_tab=wsww-mailchimp-tab' );
