<?php
require_once BONGDAKO_PLUGIN_DIR_PATH . 'layouts/settings/components/tab2/includes/setting_field.php';


function enqueue_scripts()
{
    if (is_admin()) {
        wp_enqueue_script(
            'sport-plugin-admin-js',
            BONGDAKO_PLUGIN_DIR_URL . 'admin/assets/js/admin-bongdako.js',
            array('jquery'),
            null,
            true
        );

        wp_localize_script('sport-plugin-admin-js', 'my_ajax_object', array(
            'save_nonce' => wp_create_nonce('save_template_bongdako_nonce'),
            'default_nonce' => wp_create_nonce('default_template_bongdako_nonce'),
            'ajaxurl' => admin_url('admin-ajax.php')
        ));

        ?>
        <meta name="license_url" content="<?php echo LICENSE_URL ?>">
        <?php
    }
}

add_action('admin_enqueue_scripts', 'enqueue_scripts');


function bongdako_view_enqueue_scripts()
{
    wp_register_style('bongdako-view-sixinone-style', BONGDAKO_PLUGIN_DIR_URL . 'admin/assets/view-css/6in1_style.css', array(), ASSETS_VERSION, 'all');
    wp_register_style('bongdako-view-player_board-style', BONGDAKO_PLUGIN_DIR_URL . 'admin/assets/view-css/player_board_style.css', array(), ASSETS_VERSION, 'all');
    wp_register_style('bongdako-view-rank-style', BONGDAKO_PLUGIN_DIR_URL . 'admin/assets/view-css/rank_style.css', array(), ASSETS_VERSION, 'all');
    wp_register_style('bongdako-view-livescore-style', BONGDAKO_PLUGIN_DIR_URL . 'admin/assets/view-css/rate_style.css', array(), ASSETS_VERSION, 'all');
    wp_register_style('bongdako-view-result-style', BONGDAKO_PLUGIN_DIR_URL . 'admin/assets/view-css/result_style.css', array(), ASSETS_VERSION, 'all');
    wp_register_style('bongdako-view-schedule-style', BONGDAKO_PLUGIN_DIR_URL . 'admin/assets/view-css/schedule_style.css', array(), ASSETS_VERSION, 'all');
}

add_action('wp_enqueue_scripts', 'bongdako_view_enqueue_scripts', 1);

add_action('admin_enqueue_scripts', 'bongdako_view_enqueue_scripts');


function save_template_bongdako()
{
    if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'save_template_bongdako_nonce')) {
        wp_send_json_error(array('message' => __('Bảo mật không hợp lệ.', 'sport')));
        exit;
    }

    if (!isset($_POST['style_id']) || !isset($_POST['data'])) {
        wp_send_json_error(array('message' => __('Dữ liệu không hợp lệ.', 'sport')));
        exit;
    }

    $style_id = sanitize_text_field($_POST['style_id']);
    $json = $_POST['data'];
    $cleanJsonString = stripslashes($json);
    $data = json_decode($cleanJsonString, true);
    update_option('template_' . $style_id, serialize($data));

    wp_send_json_success(array('message' => __('Dữ liệu đã được lưu.', 'sport')));
}

add_action('wp_ajax_get_template_bongdako', 'get_template_bongdako');
function get_template_bongdako()
{
    if (!isset($_GET['style_id'])) {
        wp_send_json_error(array('message' => __('Dữ liệu không hợp lệ.', 'sport')));
        exit;
    }

    $style_id = sanitize_text_field($_GET['style_id']);

    $dataColor = get_option('template_' . $style_id, false);

    $prefix = 'style_';

    if (substr($style_id, 0, strlen($prefix)) === $prefix) {
        $key = substr($style_id, strlen($prefix));
    }
    if (!$key) {
        exit;
    }

    $arrayColor = BongdakoSettings::$arrayColor[$key];

    $dataColor = ($unserializedData = unserialize(get_option('template_' . $style_id))) !== false || $unserializedData === 'b:0;' ? $unserializedData : $arrayColor;


    if (!$dataColor) {
        wp_send_json_error(array('message' => __('Không tìm thấy dữ liệu cho style_id này.', 'sport')));
        exit;
    }

    if (!is_array($dataColor)) {
        wp_send_json_error(array('message' => __('Dữ liệu không hợp lệ.', 'sport')));
        exit;
    }


    $html = '';
    foreach ($dataColor as $styleField) {
        $html .= BongdakoSettings::input_color_bongdako($styleField);
    }
    $output = $html;
    wp_send_json_success(array('data' => $output));

    exit;
}

add_action('wp_ajax_default_template_bongdako', 'default_template_bongdako');
function default_template_bongdako()
{
    if (!isset($_POST['security']) || !wp_verify_nonce($_POST['security'], 'default_template_bongdako_nonce')) {
        wp_send_json_error(array('message' => __('Bảo mật không hợp lệ.', 'sport')));
        exit;
    }

    if (!isset($_POST['style_id'])) {
        wp_send_json_error(array('message' => __('Dữ liệu không hợp lệ.', 'sport')));
        exit;
    }

    $style_id = sanitize_text_field($_POST['style_id']);
    update_option('template_' . $style_id, '');

    $prefix = 'style_';

    if (substr($style_id, 0, strlen($prefix)) === $prefix) {
        $key = substr($style_id, strlen($prefix));
    }
    if (!$key) {
        exit;
    }

    $dataColor = BongdakoSettings::$arrayColor[$key];

    $html = '';
    foreach ($dataColor as $styleField) {
        $html .= BongdakoSettings::input_color_bongdako($styleField);
    }
    $output = $html;


    wp_send_json_success(array('message' => __('Dữ liệu đã được lưu.', 'sport'), 'data' => $output));
}

add_action('wp_ajax_save_template_bongdako', 'save_template_bongdako');

function add_loading_bongdako()
{
    ; ?>
    <div class="loading_html">
        <div class="loader">
            <div class="loader-square"></div>
            <div class="loader-square"></div>
            <div class="loader-square"></div>
            <div class="loader-square"></div>
            <div class="loader-square"></div>
            <div class="loader-square"></div>
            <div class="loader-square"></div>
        </div>
        <div class="loading_inner"></div>
    </div>
    <style>
        .loading_html {
            display: none;
        }

        .loading_html.active {
            display: block;
        }

        .loading_inner {
            width: 100vw;
            height: 100vh;
            background: #000000c9;
            top: 0;
            z-index: 9999;
            position: fixed;
            left: 0;
        }

        @keyframes square-animation {
            0% {
                left: 0;
                top: 0;
            }

            10.5% {
                left: 0;
                top: 0;
            }

            12.5% {
                left: 32px;
                top: 0;
            }

            23% {
                left: 32px;
                top: 0;
            }

            25% {
                left: 64px;
                top: 0;
            }

            35.5% {
                left: 64px;
                top: 0;
            }

            37.5% {
                left: 64px;
                top: 32px;
            }

            48% {
                left: 64px;
                top: 32px;
            }

            50% {
                left: 32px;
                top: 32px;
            }

            60.5% {
                left: 32px;
                top: 32px;
            }

            62.5% {
                left: 32px;
                top: 64px;
            }

            73% {
                left: 32px;
                top: 64px;
            }

            75% {
                left: 0;
                top: 64px;
            }

            85.5% {
                left: 0;
                top: 64px;
            }

            87.5% {
                left: 0;
                top: 32px;
            }

            98% {
                left: 0;
                top: 32px;
            }

            100% {
                left: 0;
                top: 0;
            }
        }

        .loader {
            position: fixed !important;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            transform-origin: center;
            transition: opacity .2s, transform .2s;
            z-index: 99999999;
        }

        .loader {
            position: relative;
            width: 96px;
            height: 96px;
            transform: rotate(45deg);
        }

        .loader-square {
            position: absolute;
            top: 0;
            left: 0;
            width: 28px;
            height: 28px;
            margin: 2px;
            border-radius: 0px;
            background: white;
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            animation: square-animation 10s ease-in-out infinite both;
        }

        .loader-square:nth-of-type(0) {
            animation-delay: 0s;
        }

        .loader-square:nth-of-type(1) {
            animation-delay: -1.4285714286s;
        }

        .loader-square:nth-of-type(2) {
            animation-delay: -2.8571428571s;
        }

        .loader-square:nth-of-type(3) {
            animation-delay: -4.2857142857s;
        }

        .loader-square:nth-of-type(4) {
            animation-delay: -5.7142857143s;
        }

        .loader-square:nth-of-type(5) {
            animation-delay: -7.1428571429s;
        }

        .loader-square:nth-of-type(6) {
            animation-delay: -8.5714285714s;
        }

        .loader-square:nth-of-type(7) {
            animation-delay: -10s;
        }
    </style>
<?php }

add_action('admin_footer', 'add_loading_bongdako');

add_action('wp_ajax_save_hot_leagues', 'save_hot_leagues_callback');
function save_hot_leagues_callback() {
    // 1. Bảo mật: Kiểm tra nonce
    check_ajax_referer('hot_leagues_nonce', 'security');

    // 2. Lấy và làm sạch dữ liệu
    $league_ids = isset($_POST['league_ids']) ? $_POST['league_ids'] : [];
    // Đảm bảo mỗi ID là một số nguyên
    $sanitized_ids = array_map('intval', $league_ids);

    // 3. Lưu vào database
    update_option('hot_leagues_list', $sanitized_ids);

    // 4. Gửi phản hồi thành công
    wp_send_json_success(['message' => 'Đã lưu thành công!']);
}
