<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Felixx Project</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #121212;
            color: #FFFFFF;
            transition: background-color 0.5s, color 0.5s;
        }

        .container {
            max-width: 800px;
            margin: 40px auto;
            padding: 20px;
            background-color: #1E1E1E;
            border-radius: 12px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
            transition: background-color 0.5s;
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        li {
            display: block;
            padding: 10px 15px;
            border-radius: 8px;
            margin-bottom: 10px;
            transition: background-color 0.3s, transform 0.3s;
            cursor: pointer;
            position: relative;
        }

        li:hover {
            background-color: rgba(255, 255, 255, 0.1);
            transform: scale(1.02);
        }

        .icon {
            margin-right: 10px;
        }

        .file-content {
            display: none;
            background-color: #000;
            color: #FFF;
            padding: 15px;
            border-radius: 6px;
            margin-top: 10px;
            font-family: 'Courier New', Courier, monospace;
            position: relative;
            overflow-x: auto;
            white-space: pre;
            flex-direction: column;
        }

        .file-content pre {
            white-space: pre-wrap;
            word-wrap: break-word;
            overflow-x: auto;
        }

        .file-content.show {
            display: block;
            animation: fadeIn 0.5s ease-in-out;
        }

        .file-content.typing {
            white-space: pre-wrap;
            overflow: hidden;
        }

        .file-content .buttons {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 10px;
        }

        .file-content.typing::after {
            content: '';
            animation: typing 1s steps(10) infinite;
        }

        .file-content .copy-btn,
        .file-content .view-raw-btn,
        .file-content .preview-btn,
        .file-content .download-btn {
            padding: 5px 15px;
            background: #007AFF;
            color: #FFF;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .file-content .view-raw-btn {
            right: 80px;
        }

        .file-content .preview-btn {
            right: 150px;
        }

        .file-content .copy-btn:hover,
        .file-content .view-raw-btn:hover,
        .file-content .preview-btn:hover,
        .download-btn:hover {
            background-color: #005BBB;
        }

        .folder-container {
            margin-left: 20px;
            padding-left: 10px;
            border-left: 2px solid rgba(255, 255, 255, 0.2);
            margin-bottom: 20px;
        }

        .folder>ul {
            margin-top: 10px;
        }

        .folder>.icon {
            transition: transform 0.3s;
        }

        .folder.open>.icon {
            transform: rotate(90deg);
        }

        .highlight {
            background: #282C34;
            color: #61AFEF;
        }

        body.dark-mode {
            background-color: #121212;
            color: #FFFFFF;
        }

        body.dark-mode .container {
            background-color: #1E1E1E;
        }

        body.light-mode {
            background-color: #F5F5F5;
            color: #000000;
        }

        body.light-mode .container {
            background-color: #FFFFFF;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        body.light-mode li:hover {
            background-color: rgba(0, 0, 0, 0.05);
        }


        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: scaleY(0.9);
            }

            to {
                opacity: 1;
                transform: scaleY(1);
            }
        }

        @keyframes typing {
            0% {
                border-right: 2px solid transparent;
            }

            50% {
                border-right: 2px solid white;
            }

            100% {
                border-right: 2px solid transparent;
            }
        }

        .dark-mode-toggle {
            position: absolute;
            top: 20px;
            right: 20px;
            background: none;
            border: none;
            font-size: 24px;
            cursor: pointer;
            transition: transform 0.3s;
        }
    </style>
</head>

<body>
    <button class="dark-mode-toggle" id="darkModeToggle">🌞</button>
    <div class="container">
        <h2>📁 Felixx Project</h2>
        <ul id="fileList"></ul>
    </div>

    <script>
        const darkModeToggle = document.getElementById('darkModeToggle');
        const body = document.body;

        const theme = localStorage.getItem('theme') || 'dark';
        body.className = theme + '-mode';
        darkModeToggle.textContent = theme === 'dark' ? '🌞' : '🌙';

        darkModeToggle.addEventListener('click', () => {
            const newTheme = body.className === 'dark-mode' ? 'light' : 'dark';
            body.className = newTheme + '-mode';
            localStorage.setItem('theme', newTheme);
            darkModeToggle.textContent = newTheme === 'dark' ? '🌞' : '🌙';
        });

        const urlParams = new URLSearchParams(window.location.search);
        const fileParam = urlParams.get('file');

        async function fetchFiles(path = 'Felixx', parentElement = null) {
            const response = await fetch(`https://felixxx.site/fileDirectory/files?path=${path}`);
            const files = await response.json();
            const fileList = parentElement || document.getElementById('fileList');

            if (!parentElement) fileList.innerHTML = '';

            files.forEach(file => {
                const li = document.createElement('li');
                li.className = file.isDirectory ? 'folder' : 'file';
                li.innerHTML = `<span class="icon">${file.isDirectory ? '📂' : '📄'}</span>${file.name}`;

                if (file.isDirectory) {
                    li.addEventListener('click', async (e) => {
                        e.stopPropagation();
                        li.classList.toggle('open');

                        const childList = li.querySelector('ul');
                        if (childList) return;

                        const folderContainer = document.createElement('div');
                        folderContainer.className = 'folder-container';

                        const ul = document.createElement('ul');
                        folderContainer.appendChild(ul);
                        li.appendChild(folderContainer);

                        await fetchFiles(file.path, ul);
                    });
                } else {
                    li.addEventListener('click', async (e) => {
                        e.stopPropagation();
                        const existingContent = li.querySelector('.file-content');
                        if (existingContent) {
                            existingContent.classList.toggle('show');
                            return;
                        }

                        const content = document.createElement('div');
                        content.className = 'file-content typing';

                        const response = await fetch(`https://felixxx.site/fileDirectory/view-file?file=${file.path}`);
                        const data = await response.json();

                        const pre = document.createElement('pre');
                        let idx = 0;

                        const typeAnimation = setInterval(() => {
                            if (idx < data.content.length) {
                                pre.textContent += data.content[idx];
                                idx++;
                            } else {
                                clearInterval(typeAnimation);
                                content.classList.remove('typing');
                            }
                        }, 10);

                        const copyBtn = document.createElement('button');
                        copyBtn.textContent = 'Salin';
                        copyBtn.className = 'copy-btn';
                        copyBtn.onclick = () => {
                            navigator.clipboard.writeText(data.content);
                            alert('Konten berhasil disalin!');
                        };

                        const viewRawBtn = document.createElement('button');
                        viewRawBtn.textContent = 'View Raw';
                        viewRawBtn.className = 'view-raw-btn';
                        viewRawBtn.onclick = () => {
                            window.location.href = `index.html?file=${file.path}&raw=true`;
                        };

                        const downloadBtn = document.createElement('button');
                        downloadBtn.textContent = 'Download File';
                        downloadBtn.className = 'download-btn';
                        downloadBtn.onclick = () => {
                            window.location.href = `https://felixxx.site/fileDirectory/download?file=${encodeURIComponent(file.path)}`;
                        };

                        if (file.name.endsWith('.html') || file.name.endsWith('.php')) {
                            const previewBtn = document.createElement('button');
                            previewBtn.textContent = 'Preview';
                            previewBtn.className = 'preview-btn';
                            previewBtn.onclick = () => {
                                window.location.href = `index.html?file=${file.path}&raw=false`;
                            };
                            content.appendChild(previewBtn);
                        }

                        content.appendChild(pre);
                        content.appendChild(copyBtn);
                        content.appendChild(viewRawBtn);
                        content.appendChild(downloadBtn);

                        li.appendChild(content);
                        content.classList.add('show');
                    });
                }
                fileList.appendChild(li);
            });
        }

        if (fileParam) {
            (async () => {
                const response = await fetch(`https://felixxx.site/fileDirectory/view-file?file=${fileParam}&raw=${urlParams.get('raw')}`);
                const data = await response.json();

                const container = document.createElement('div');
                container.className = 'container';

                if (urlParams.get('raw') === 'false') {
                    const iframe = document.createElement('iframe');
                    iframe.srcdoc = data.content;
                    iframe.width = '100%';
                    iframe.height = '600px';
                    container.appendChild(iframe);
                } else {
                    const pre = document.createElement('pre');
                    pre.textContent = data.content;
                    pre.style.backgroundColor = '#000';
                    pre.style.color = '#FFF';
                    pre.style.padding = '20px';
                    pre.style.borderRadius = '6px';
                    pre.style.fontFamily = 'Courier New, Courier, monospace';
                    pre.style.whiteSpace = 'pre-wrap';
                    pre.style.wordWrap = 'break-word';

                    container.appendChild(pre);
                }
                document.body.appendChild(container);
            })();
        } else {
            fetchFiles();
        }
    </script>
</body>

</html>