<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FloodCord</title>
    <style>
        :root {
            --bg-color: black;
            --text-color: red;
            --border-color: red;
        }

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            background-color: var(--bg-color);
            color: var(--text-color);
            font-family: monospace;
            margin: 0;
        }

        .container {
            text-align: left;
            background-color: var(--bg-color);
            padding: 20px;
            border: 1px solid var(--border-color);
            width: 60%;
            max-width: 800px;
        }

        h1 {
            font-size: 1.5em;
            margin-bottom: 20px;
            color: var(--text-color);
            text-align: center;
        }

        input, button, textarea, select {
            display: block;
            width: 100%;
            max-width: 500px;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid var(--border-color);
            border-radius: 3px;
            background-color: var(--bg-color);
            color: var(--text-color);
        }

        button:hover {
            background-color: #004d00;
        }

        .slider-container {
            margin: 20px 0;
            width: 100%;
            max-width: 500px;
        }

        .slider {
            width: 100%;
            appearance: none;
            height: 15px;
            background: var(--border-color);
            outline: none;
            border-radius: 5px;
        }

        .slider::-webkit-slider-thumb {
            appearance: none;
            width: 25px;
            height: 25px;
            background: var(--bg-color);
            border: 2px solid var(--border-color);
            cursor: pointer;
            border-radius: 50%;
        }

        .message {
            margin: 5px 0;
            padding: 5px;
            border: 1px solid var(--border-color);
            border-radius: 3px;
            background-color: var(--bg-color);
        }

        .chat-container {
            max-height: 300px;
            overflow-y: auto;
            border: 1px solid var(--border-color);
            border-radius: 3px;
            background-color: var(--bg-color);
        }
    </style>
</head>
<body>
    <div class="container" id="tokenContainer">
        <h1>FloodCord - Enter Token</h1>
        <form id="tokenForm" onsubmit="enterToken(); return false;">
            <input type="password" id="discordToken" placeholder="Enter your Discord token" required>
            <button type="button" onclick="enterToken()">Enter Token</button>
        </form>
    </div>

    <div class="container hidden" id="mainContainer">
        <h1>FloodCord</h1>

        <!-- Theme Selector -->
        <div>
            <label for="themeSelector">Select Theme:</label>
            <select id="themeSelector" onchange="changeTheme()">
                <option value="dark">Dark</option>
                <option value="light">Light</option>
            </select>
        </div>

        <!-- FloodCord Form -->
        <form id="floodCordForm" onsubmit="sendPatternMessage(); return false;">
            <input type="text" id="channelId" placeholder="Enter Channel ID" required>
            <textarea id="message" placeholder="Enter your message" rows="5" required></textarea>
            <div>
                <label for="customPattern">Custom Pattern (use {message} for input message):</label>
                <textarea id="customPattern" rows="3">A\n\n\n\n\n{message}\n\nA</textarea>
            </div>
            <div class="slider-container">
                <label for="delayInput">Delay:</label>
                <input type="number" id="delayInput" min="0.1" max="10" step="0.1" value="0.1" onchange="syncSliderWithInput()">
                <input type="range" id="delaySlider" class="slider" min="0.1" max="10" step="0.1" value="0.1" oninput="syncInputWithSlider()">
                <span id="delayValue">0.1</span> seconds
            </div>
            <button type="button" onclick="sendPatternMessage()">Send Pattern Message</button>
        </form>

        <!-- Manual message box to send messages -->
        <textarea id="chatMessage" placeholder="Type your message and press Enter to send..." rows="3"></textarea>
        <div class="chat-container" id="chatContainer"></div>
    </div>

    <script>
        let discordToken = '';

        function enterToken() {
            discordToken = document.getElementById('discordToken').value.trim();
            if (discordToken) {
                document.getElementById('tokenContainer').classList.add('hidden');
                document.getElementById('mainContainer').classList.remove('hidden');
            } else {
                alert('Please enter a valid Discord token.');
            }
        }

        function changeTheme() {
            const theme = document.getElementById('themeSelector').value;
            if (theme === 'light') {
                document.documentElement.style.setProperty('--bg-color', 'white');
                document.documentElement.style.setProperty('--text-color', 'black');
                document.documentElement.style.setProperty('--border-color', 'gray');
            } else {
                document.documentElement.style.setProperty('--bg-color', 'black');
                document.documentElement.style.setProperty('--text-color', 'red');
                document.documentElement.style.setProperty('--border-color', 'red');
            }
        }

        function syncSliderWithInput() {
            const delayInput = document.getElementById('delayInput');
            document.getElementById('delaySlider').value = delayInput.value;
            updateDelayDisplay();
        }

        function syncInputWithSlider() {
            const delaySlider = document.getElementById('delaySlider');
            document.getElementById('delayInput').value = delaySlider.value;
            updateDelayDisplay();
        }

        function updateDelayDisplay() {
            document.getElementById('delayValue').innerText = document.getElementById('delayInput').value;
        }

        function generatePatternMessage(message) {
            const customPattern = document.getElementById('customPattern').value || '{message}';
            return customPattern.replace(/{message}/g, message);
        }

        function sendPatternMessage() {
            const channelId = document.getElementById('channelId').value;
            const message = document.getElementById('message').value;
            const delay = document.getElementById('delayInput').value * 1000;

            if (!discordToken || !channelId || !message) {
                alert('Please fill all the fields and enter a valid token.');
                return;
            }

            const patternMessage = generatePatternMessage(message);

            fetch(`https://discord.com/api/v9/channels/${channelId}/messages`, {
                method: 'POST',
                headers: {
                    'Authorization': discordToken,
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ content: patternMessage })
            })
                .then(response => response.json())
                .then(data => {
                    if (data.id) {
                        fetchMessages();
                    } else {
                        alert('Failed to send the message.');
                    }
                })
                .catch(error => console.error('Error sending message:', error));
        }

        function fetchMessages() {
            const channelId = document.getElementById('channelId').value;

            if (discordToken && channelId) {
                fetch(`https://discord.com/api/v9/channels/${channelId}/messages?limit=50`, {
                    headers: {
                        'Authorization': discordToken
                    }
                })
                    .then(response => response.json())
                    .then(data => {
                        const chatContainer = document.getElementById('chatContainer');
                        chatContainer.innerHTML = '';
                        data.reverse().forEach(message => {
                            const msgElement = document.createElement('div');
                            msgElement.classList.add('message');
                            msgElement.innerHTML = `<span class="sender">${message.author.username}:</span> ${message.content}`;
                            chatContainer.appendChild(msgElement);
                        });
                        chatContainer.scrollTop = chatContainer.scrollHeight;
                    })
                    .catch(error => console.error('Error fetching messages:', error));
            }
        }

        document.getElementById('chatMessage').addEventListener('keydown', function (event) {
            if (event.key === 'Enter') {
                event.preventDefault();
                const message = event.target.value.trim();
                if (message) {
                    sendPatternMessage(message);
                }
            }
        });

        window.onload = function () {
            if (localStorage.getItem('floodCordConfig')) {
                loadConfig();
            }
        };

        function saveConfig() {
            const config = {
                theme: document.getElementById('themeSelector').value,
                delay: document.getElementById('delayInput').value,
                pattern: document.getElementById('customPattern').value,
            };
            localStorage.setItem('floodCordConfig', JSON.stringify(config));
        }

        function loadConfig() {
            const config = JSON.parse(localStorage.getItem('floodCordConfig'));
            if (config) {
                document.getElementById('themeSelector').value = config.theme;
                changeTheme();
                document.getElementById('delayInput').value = config.delay;
                syncSliderWithInput();
                document.getElementById('customPattern').value = config.pattern;
            }
        }
    </script>
</body>
</html>
