<?php
// =============================================
// CONFIGURAÇÕES E FUNÇÕES PHP
// =============================================

// 1. Funções de codificação/decodificação
function codificarURL($url) {
    return base64_encode($url);
}

function decodificarURL($urlCodificado) {
    return base64_decode($urlCodificado);
}

// 2. Redirecionamento para imagens
if (isset($_GET['img'])) {
    header("Location: " . decodificarURL($_GET['img']));
    exit;
}

// 3. Configuração de timezone
date_default_timezone_set('America/Sao_Paulo');

// 4. Mapeamento de datas em português
$diaSemanaMap = [
    'Sunday' => 'Domingo', 'Monday' => 'Segunda', 'Tuesday' => 'Terça',
    'Wednesday' => 'Quarta', 'Thursday' => 'Quinta', 'Friday' => 'Sexta',
    'Saturday' => 'Sábado'
];

$meses = [
    'January' => 'Janeiro', 'February' => 'Fevereiro', 'March' => 'Março',
    'April' => 'Abril', 'May' => 'Maio', 'June' => 'Junho',
    'July' => 'Julho', 'August' => 'Agosto', 'September' => 'Setembro',
    'October' => 'Outubro', 'November' => 'Novembro', 'December' => 'Dezembro'
];

$dataAtual = $diaSemanaMap[date('l')] . ', ' . date('d') . ' de ' . $meses[date('F')] . ' de ' . date('Y');

// 5. Carregar dados dos jogos
$api_url = 'https://meusjogos.shop/dados_api.json';
$jogos = [];

if ($api_json_content = @file_get_contents($api_url)) {
    $jogos = json_decode($api_json_content, true) ?: [];
}

// 6. Configurações de cores - LUXO MODERNO (Dourado e Preto Profundo)
session_start();

$config = [
    'color1' => $_SESSION['color1'] ?? '#FFD700', // Dourado
    'color2' => $_SESSION['color2'] ?? '#B8860B', // Dourado Escuro
    'color3' => $_SESSION['color3'] ?? '#0A0A0A', // Preto Profundo
    'glowColor' => $_SESSION['glowColor'] ?? '#FFD700',
    'starColor' => $_SESSION['starColor'] ?? '#FFD700',
    'scrollSpeed' => 1.2 // Velocidade moderada
];

// Salvar configurações
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    foreach (['color1', 'color2', 'color3', 'glowColor', 'starColor'] as $key) {
        if (isset($_POST[$key])) {
            $_SESSION[$key] = $config[$key] = $_POST[$key];
        }
    }
}

// 7. Converter cores HEX para RGB
function hexToRgb($hex) {
    $hex = str_replace("#", "", $hex);
    if (strlen($hex) === 3) {
        $r = hexdec(str_repeat(substr($hex, 0, 1), 2));
        $g = hexdec(str_repeat(substr($hex, 1, 1), 2));
        $b = hexdec(str_repeat(substr($hex, 2, 1), 2));
    } else {
        $r = hexdec(substr($hex, 0, 2));
        $g = hexdec(substr($hex, 2, 2));
        $b = hexdec(substr($hex, 4, 2));
    }
    return [$r, $g, $b];
}

list($glowR, $glowG, $glowB) = hexToRgb($config['glowColor']);

?>
<!doctype html>
<html lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Jogos do Dia - Luxo Moderno</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --primary-1: <?= $config['color1'] ?>;
            --primary-2: <?= $config['color2'] ?>;
            --primary-3: <?= $config['color3'] ?>;
            --glow-color: rgba(<?= "$glowR,$glowG,$glowB" ?>, 0.4);
            --bg-dark: #050505;
            --bg-card: #111111;
            --border-color: rgba(255, 215, 0, 0.2);
        }
        
        body {
            font-family: 'Segoe UI', 'Arial', sans-serif;
            background: radial-gradient(circle at center, #151515 0%, var(--bg-dark) 100%);
            color: #fff;
            margin: 0;
            padding: 20px;
            min-height: 100vh;
            overflow-x: hidden;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
        }

        h1 {
            font-size: 2rem;
            margin-bottom: 40px;
            text-align: center;
            font-weight: 900;
            letter-spacing: 3px;
            text-transform: uppercase;
            background: linear-gradient(to right, #fff, var(--primary-1), #fff);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .multiple-container {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 30px;
            padding: 20px;
        }

        /* Card Redondo */
        .jogo-card {
            width: 340px;
            transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        }

        .jogo-card:hover {
            transform: scale(1.03);
        }
        
        .jogo-card-inner {
            background: var(--bg-card);
            border-radius: 60px; /* Super redondo */
            padding: 30px;
            border: 2px solid var(--border-color);
            box-shadow: 0 15px 35px rgba(0,0,0,0.5);
            position: relative;
            overflow: hidden;
            text-align: center;
        }

        .jogo-card:hover .jogo-card-inner {
            border-color: var(--primary-1);
            box-shadow: 0 0 25px var(--glow-color);
        }

        .campeonato {
            font-size: 12px;
            color: var(--primary-1);
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 20px;
            font-weight: bold;
        }

        .times {
            display: flex;
            justify-content: space-around;
            align-items: center;
            margin-bottom: 20px;
        }

        .time-box {
            flex: 1;
        }

        .time img {
            width: 80px;
            height: 80px;
            object-fit: contain;
            margin-bottom: 10px;
            filter: drop-shadow(0 5px 10px rgba(0,0,0,0.3));
        }

        .time p {
            font-size: 14px;
            font-weight: bold;
            height: 34px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .placar {
            font-size: 32px;
            font-weight: 900;
            color: var(--primary-1);
            margin-top: 5px;
        }

        .vs {
            font-size: 18px;
            font-weight: 900;
            color: #444;
            font-style: italic;
        }

        .hora {
            background: rgba(255,215,0,0.1);
            color: var(--primary-1);
            padding: 5px 15px;
            border-radius: 20px;
            display: inline-block;
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 15px;
        }

        .status {
            font-size: 12px;
            font-weight: bold;
            text-transform: uppercase;
            padding: 4px 12px;
            border-radius: 10px;
            display: inline-block;
        }

        .status-andamento { background: #00ff0022; color: #00ff00; }
        .status-fim { background: #ff000022; color: #ff0000; }

        /* Estilo para a barra de rolagem */
        ::-webkit-scrollbar { width: 6px; }
        ::-webkit-scrollbar-track { background: #050505; }
        ::-webkit-scrollbar-thumb { background: var(--primary-1); border-radius: 10px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>Jogos de Hoje <span><?= $dataAtual ?></span></h1>
        
        <div class="multiple-container">
            <?php if (empty($jogos)): ?>
                <p style="text-align:center; width:100%;">Nenhum jogo encontrado no momento.</p>
            <?php else: ?>
                <?php foreach ($jogos as $jogo): ?>
                    <div class="jogo-card">
                        <div class="jogo-card-inner">
                            <?php if (!empty($jogo['campeonato'])): ?>
                            <div class="campeonato"><?= $jogo['campeonato'] ?></div>
                            <?php endif; ?>
                            <div class="times">
                                <div class="time-box">
                                    <div class="time">
                                        <?php if (!empty($jogo['img_time1_url'])): ?>
                                        <img src="<?= $jogo['img_time1_url'] ?>" alt="<?= $jogo['time1'] ?? 'Time 1' ?>">
                                        <?php endif; ?>
                                        <p><?= $jogo['time1'] ?? 'Time 1' ?></p>
                                    </div>
                                    <div class="placar"><?= $jogo['placar_time1'] ?? '-' ?></div>
                                </div>
                                <div class="vs">VS</div>
                                <div class="time-box">
                                    <div class="time">
                                        <?php if (!empty($jogo['img_time2_url'])): ?>
                                        <img src="<?= $jogo['img_time2_url'] ?>" alt="<?= $jogo['time2'] ?? 'Time 2' ?>">
                                        <?php endif; ?>
                                        <p><?= $jogo['time2'] ?? 'Time 2' ?></p>
                                    </div>
                                    <div class="placar"><?= $jogo['placar_time2'] ?? '-' ?></div>
                                </div>
                            </div>
                            <div class="hora"><?= $jogo['horario'] ?? 'Horário não definido' ?></div>
                            <br>
                            <div class="status <?= ($jogo['status'] ?? '') === 'Finalizado' ? 'status-fim' : 'status-andamento' ?>">
                                <?= ($jogo['status'] ?? 'Aguardando') === 'Ao Vivo' ? '● Ao Vivo' : ($jogo['status'] ?? 'Aguardando') ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <script>
    // Sistema de Auto-Scroll Bidirecional (Suave)
    class AutoScroll {
        constructor() {
            this.container = document.documentElement;
            this.direction = 1; // 1 para baixo, -1 para cima
            this.animationFrame = null;
            this.scrollSpeed = <?= $config['scrollSpeed'] ?>; 
            this.isPaused = false;
            this.setupEvents();
            this.start();
        }
        
        setupEvents() {
            // Pausa ao interagir
            const pauseEvents = ['mousedown', 'touchstart'];
            const resumeEvents = ['mouseup', 'touchend'];
            
            pauseEvents.forEach(e => document.addEventListener(e, () => this.isPaused = true));
            resumeEvents.forEach(e => document.addEventListener(e, () => {
                this.isPaused = false;
                this.start();
            }));
            
            // Pausa temporária no scroll manual
            let wheelTimeout;
            document.addEventListener('wheel', () => {
                this.isPaused = true;
                clearTimeout(wheelTimeout);
                wheelTimeout = setTimeout(() => {
                    this.isPaused = false;
                    this.start();
                }, 2500);
            }, { passive: true });
        }
        
        animate() {
            if (this.isPaused) return;
            
            const currentScroll = window.pageYOffset || document.documentElement.scrollTop;
            const maxScroll = document.documentElement.scrollHeight - window.innerHeight;
            
            // Inverte direção nos limites
            if (currentScroll >= maxScroll - 1 && this.direction === 1) {
                this.direction = -1;
            } else if (currentScroll <= 1 && this.direction === -1) {
                this.direction = 1;
            }
            
            window.scrollBy(0, this.scrollSpeed * this.direction);
            this.animationFrame = requestAnimationFrame(() => this.animate());
        }
        
        start() {
            if (this.animationFrame) cancelAnimationFrame(this.animationFrame);
            this.animationFrame = requestAnimationFrame(() => this.animate());
        }
    }

    document.addEventListener('DOMContentLoaded', () => {
        new AutoScroll();
    });
    </script>
</body>
</html>
