<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Skill Check</title>

    <!-- Add a modern font from Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    
    <style>
        body {
            margin: 0;
            padding: 0;
            background-color: transparent;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            font-family: 'Poppins', sans-serif;
            position: relative;
            overflow: hidden;
        }

        .dark-fade {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: 
                linear-gradient(to top left, rgba(0, 0, 0, 0.8), transparent 50%),
                linear-gradient(to top right, rgba(0, 0, 0, 0.8), transparent 50%),
                linear-gradient(to bottom left, rgba(0, 0, 0, 0.8), transparent 50%),
                linear-gradient(to bottom right, rgba(0, 0, 0, 0.8), transparent 50%);
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.5s ease-out;
        }

        .dark-fade.show {
            opacity: 1;
        }

        .skill-check-container {
            display: none;
            position: relative;
            width: 300px;
            height: 300px;
            border: 5px solid white;
        }

        .corner-letter {
            width: 60px;
            height: 60px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: white;
            border-radius: 10px;
            font-size: 30px;
            font-weight: bold;
            color: black;
            cursor: pointer;
            transition: background-color 0.3s;
            position: absolute;
        }

        .corner-letter.green {
            background-color: rgb(142, 251, 142);
        }

        .top-left {
            top: -30px;
            left: -30px;
        }
        .top-right {
            top: -30px;
            right: -30px;
        }
        .bottom-left {
            bottom: -30px;
            left: -30px;
        }
        .bottom-right {
            bottom: -30px;
            right: -30px;
        }

        /* Image display for selected item */
        #selectedItem {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            max-width: 100px;
            max-height: 100px;
            display: none; /* Initially hidden */
        }
    </style>
</head>
<body>

    <div class="dark-fade" id="darkFade"></div>

    <div class="skill-check-container" id="skillCheckContainer">
        <div class="corner-letter top-left" id="w-btn">W</div>
        <div class="corner-letter top-right" id="a-btn">A</div>
        <div class="corner-letter bottom-left" id="s-btn">S</div>
        <div class="corner-letter bottom-right" id="d-btn">D</div>
    </div>

    <!-- Image placeholder for selected item -->
    <img id="selectedItem" src="" alt="Selected Item" style="display: none;"/>

    <script>
        let pressedLetters = [];
        const correctOrder = ['w', 'a', 'd', 's']; // The fixed order of keypresses
        const cornerIds = ['w-btn', 'a-btn', 's-btn', 'd-btn'];
        let canPress = true;  // Flag to control the cooldown period
        const cooldownTime = 500; // Cooldown in milliseconds (500ms = 0.5 seconds)

        // Listen for keypress events
        window.addEventListener('keydown', function(event) {
            const key = event.key.toLowerCase();

            if (correctOrder.includes(key) && canPress) {
                if (key === correctOrder[pressedLetters.length]) {
                    pressedLetters.push(key);
                    document.getElementById(key + '-btn').classList.add('green');

                    if (pressedLetters.length === correctOrder.length) {
                        fetch(`https://${GetParentResourceName()}/completeSkillCheck`, {
                            method: 'POST',
                            body: JSON.stringify({ success: true })
                        });

                        document.getElementById('skillCheckContainer').style.display = 'none';
                        document.getElementById('darkFade').classList.remove('show');
                        document.getElementById('selectedItem').style.display = 'none'; // Hide the image after success
                    }
                }

                canPress = false;
                setTimeout(() => {
                    canPress = true;
                }, cooldownTime);
            }
        });

        window.addEventListener('message', function(event) {
            if (event.data.action === 'startSkillCheck') {
                pressedLetters = [];
                document.getElementById('skillCheckContainer').style.display = 'flex';
                document.getElementById('darkFade').classList.add('show');

                correctOrder.forEach(letter => {
                    document.getElementById(letter + '-btn').classList.remove('green');
                });

                // Show the selected item image
                const item = event.data.item || 'weed_leaf'; // Default to 'weed_leaf' if no item specified
                document.getElementById('selectedItem').src = `nui://ox_inventory/web/images/${item}.png`;
                document.getElementById('selectedItem').style.display = 'block'; // Show the image
            }

            // If skill check is not successful, hide the item image
            if (event.data.action === 'completeSkillCheck') {
                document.getElementById('selectedItem').style.display = 'none'; // Hide the image
            }
        });
    </script>
</body>
</html>
