{% extends "layout.html" %}
{% block content %}
<h2>Twój serwer Minecraft</h2>

<div class="tabs">
    <button class="tab-button" onclick="showTab('controls')">
        <span class="tab-text">Sterowanie</span> <span class="tab-icon">🎛️</span>
    </button>
    <button class="tab-button" onclick="showTab('console')">
        <span class="tab-text">Konsola</span> <span class="tab-icon">📟</span>
    </button>
    <button class="tab-button" onclick="showTab('files')">
        <span class="tab-text">Pliki</span> <span class="tab-icon">📁</span>
    </button>
    <button class="tab-button" onclick="showTab('config')">
        <span class="tab-text">Konfiguracja</span> <span class="tab-icon">🛠️</span>
    </button>
    <button class="tab-button" onclick="showTab('mods')">
        <span class="tab-text">Mody/Pluginy</span> <span class="tab-icon">🧩</span>
    </button>
    <button class="tab-button" onclick="showTab('statistics')">
        <span class="tab-text">Statystyki</span> <span class="tab-icon">📈</span>
    </button>
</div>

<div class="tab-content">
    <div class="tab-panel active" id="controls">
        <div style="margin-bottom: 20px;">
            <p><strong>IP:</strong> {{ ip }}</p>
            <p><strong>Port (Serwer MC):</strong> {{ ports[0] }}</p>
            <p><strong>Dodatkowe porty:</strong> {{ ports[1:] | join(", ") }}</p>
        </div>
        <p><strong>Status serwera:</strong> <span id="server-status">Sprawdzanie...</span></p><br>
        <button class="btn btn-success" onclick="sendAction('start')">Start</button>
        <button class="btn btn-warning" onclick="sendAction('restart')">Restart</button>
        <button class="btn btn-danger" onclick="sendAction('stop')">Stop</button><br>
      <button class="btn btn-worsedanger" onclick="deleteServer()">Usuń Serwer</button>
    </div>

    <div class="tab-panel" id="console">
        <div class="console-output" id="console-log">Ładowanie logów...</div>
        <div class="console-input">
            <input type="text" id="console-command" placeholder="Wpisz komendę..." onkeydown="if(event.key==='Enter') sendCommand()">
            <button onclick="sendCommand()">Wyślij</button>
        </div>
    </div>

    <div class="tab-panel" id="files">
        <h4 id="current-path">/</h4>
        <div id="drop-zone">
            <p>Przeciągnij pliki tutaj lub kliknij, aby przesłać</p>
            <input type="file" id="file-input" multiple style="display: none;">
            <div id="upload-progress" class="hidden">
                <div id="progress-bar"></div>
            </div>
        </div>

        <br>
        <h4>Lista plików:</h4>
        <ul id="file-list"></ul>

        <div id="file-editor-container" style="display: none;">
            <h3>Edytuj plik: <span id="editing-filename"></span></h3>
            <textarea id="file-editor" style="width: 100%; height: 400px;"></textarea>
            <button class="btn btn-primary" onclick="saveFileContent()">Zapisz zmiany</button>
            <button class="btn btn-secondary" onclick="closeFileEditor()">Zamknij</button>
        </div>
    </div>

    <div class="tab-panel" id="config">
        <h4>Konfiguracja Serwera:</h4>
        <form id="config-form">
            <div class="form-group">
                <label for="server-type">Typ serwera</label>
                <select id="server-type" class="form-control">
                    <option value="paper">Paper</option>
                    <option value="fabric">Fabric</option>
                </select>
            </div>
            <div class="form-group">
                <label for="server-version">Wersja</label>
                <input type="text" id="server-version" class="form-control" placeholder="Np. 1.20.4">
            </div>
            <div class="form-group">
                <label for="max-players">Maksymalna liczba graczy</label>
                <input type="number" id="max-players" class="form-control" min="1" value="{{ config['max-players'] }}">
            </div>
            <div class="form-group">
                <label for="pvp">PvP</label>
                <input type="checkbox" id="pvp" class="form-check-input" {% if config['pvp'] %} checked {% endif %}>
            </div>
            <div class="form-group">
                <label for="difficulty">Trudność</label>
                <select id="difficulty" class="form-control">
                    <option value="peaceful" {% if config['difficulty'] == 'peaceful' %}selected{% endif %}>Spokojny</option>
                    <option value="easy" {% if config['difficulty'] == 'easy' %}selected{% endif %}>Łatwy</option>
                    <option value="normal" {% if config['difficulty'] == 'normal' %}selected{% endif %}>Normalny</option>
                    <option value="hard" {% if config['difficulty'] == 'hard' %}selected{% endif %}>Trudny</option>
                </select>
            </div>
            <div class="form-group">
                <label for="online-mode">Tryb online</label>
                <h6>W tym trybie mogą dołączyć tylko gracze z kupionym kontem Minecraft. Jest owiele bezpieczniejszy.
                    <br>Jeżeli chcesz używać serwera z wyłączonym trybem online, zainstaluj AuthMe lub podobny plugin i włącz whitelistę</h6>
                <input type="checkbox" id="online-mode" class="form-check-input" {% if config['online-mode'] %} checked {% endif %}>
            </div>
            <div class="form-group">
                <label for="spawn-monsters">Spawnowanie potworów</label>
                <input type="checkbox" id="spawn-monsters" class="form-check-input" {% if config['spawn-monsters'] %} checked {% endif %}>
            </div>
            <div class="form-group">
                <label for="spawn-animals">Spawnowanie zwierząt</label>
                <input type="checkbox" id="spawn-animals" class="form-check-input" {% if config['spawn-animals'] %} checked {% endif %}>
            </div>
            <div class="form-group">
                <label for="allow-nether">Nether Włączony</label>
                <input type="checkbox" id="allow-nether" class="form-check-input" {% if config['allow-nether'] %} checked {% endif %}>
            </div>
            <div class="form-group">
                <label for="max-build-height">Maksymalna wysokość budowania</label>
                <input type="number" id="max-build-height" class="form-control" min="1" value="{{ config['max-build-height'] }}">
            </div>
            <div class="form-group">
                <label for="view-distance">Zasięg widzenia</label>
                <input type="number" id="view-distance" class="form-control" min="1" value="{{ config['view-distance'] }}">
            </div>
            <h5>Inne:</h5>
            <div class="form-group">
                <label for="andus-drasl">Serwer Drasl Andusa</label>
                <h5>Tylko dla serwerów 1.16 i wyższych</h5>
                <h6>W tym trybie mogą dołączyć tylko gracze z kontem Drasl (https://drasl.andus.ovh).
                    <br>Tryb online musi być włączony, ponieważ ten serwer dziala tak samo jak serwery Mojang (Ale bez konieczności kupowania Minecrafta).</h6>
                <h6 style="color: red">UWAGA: W tym trybie konta z kupionym Minecraftem nie mogą dołączyć na serwer, też muszą utworzyć konto Drasl.</h6>
                <input type="checkbox" id="andus-drasl" class="form-check-input" {%  if config['andus-drasl'] %} checked {% endif %}>
            </div>
            <button type="button" class="btn btn-primary" onclick="saveConfig()">Zapisz zmiany</button>
        </form>
    </div>

    <div class="tab-panel" id="mods">
        <h4>Zainstaluj mody/pluginy z <span style="color: #2ecc71;">Modrinth</span></h4>
        <input type="text" id="mod-search" placeholder="Wyszukaj mod/plugin..." oninput="searchMods()">
        <div id="mod-results"></div>
    </div>

    <div class="tab-panel" id="statistics">
        <div class="charts-row">
            <div class="chart-container"><canvas id="cpuChart"></canvas></div>
            <div class="chart-container"><canvas id="ramChart"></canvas></div>
        </div>
    </div>
</div>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/codemirror.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/theme/material-darker.min.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="/static/js/controls.js"></script>
<script src="/static/js/console.js"></script>
<script src="/static/js/files.js"></script>
<script src="/static/js/config.js"></script>
<script src="/static/js/mods.js"></script>
<script>
    const username = "{{ username }}";

    const $ = id => document.getElementById(id);

    let statsInterval = null;
    let logsInterval = null;
    let currentStatus = null;

    function showTab(id) {
        document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
        $(id).classList.add('active');
        // Close the mobile menu if it's open (optional, depending on your menu implementation)
        const tabsContainer = document.querySelector('.tabs');
        if (tabsContainer.classList.contains('mobile-active')) {
            tabsContainer.classList.remove('mobile-active');
        }
    }

    let currentPath = '';

    function checkServerStatus() {
        fetch(`/api/status?username=${username}`)
            .then(r => r.json())
            .then(data => {
                const statusEl = $("server-status");

                if (data.running) {
                    statusEl.textContent = "Online";
                    statusEl.style.color = "#2ecc71";
                    if (currentStatus !== true) {
                        startStats();
                        currentStatus = true;
                    }
                } else {
                    statusEl.textContent = "Offline";
                    statusEl.style.color = "#e74c3c";
                    if (currentStatus !== false) {
                        stopStats();
                        currentStatus = false;
                    }
                }
            });
    }

    function startStats() {
        if (!statsInterval) {
            statsInterval = setInterval(updateStats, 4000);
            updateStats();
        }
        if (!logsInterval) {
            logsInterval = setInterval(loadLogs, 5000);
            loadLogs();
        }
    }

    function stopStats() {
        if (statsInterval) {
            clearInterval(statsInterval);
            statsInterval = null;
        }
        if (logsInterval) {
            clearInterval(logsInterval);
            logsInterval = null;
        }
    }

    function updateStats() {
        fetch(`/api/stats?username=${username}`)
            .then(r => r.json())
            .then(data => {
                const now = new Date().toLocaleTimeString();
                const charts = [cpuChart, ramChart];
                const values = [data.cpu, data.ram];

                charts.forEach((chart, i) => {
                    if (chart.data.labels.length > 20) {
                        chart.data.labels.shift();
                        chart.data.datasets[0].data.shift();
                    }
                    chart.data.labels.push(now);
                    chart.data.datasets[0].data.push(values[i]);
                    chart.update();
                });
            });
    }

    const chartOptions = {
        responsive: true,
        maintainAspectRatio: false,
        scales: {
            x: { ticks: { color: '#aaa' }, grid: { color: '#222' } },
            y: { ticks: { color: '#aaa' }, grid: { color: '#222' } }
        },
        plugins: {
            legend: { display: false },
            tooltip: {
                backgroundColor: '#1a1a2a',
                titleColor: '#f0f0f0',
                bodyColor: '#ccc'
            }
        }
    };

    const cpuChart = new Chart($("cpuChart"), {
        type: 'line',
        data: {
            labels: ['CPU (%)'],
            datasets: [{
                label: 'CPU (%)',
                data: [],
                borderColor: '#6c7bff',
                backgroundColor: 'rgba(108, 123, 255, 0.2)',
                tension: 0.3,
                pointRadius: 0
            }]
        },
        options: {
            ...chartOptions,
            scales: { ...chartOptions.scales, y: { min: 0, max: 100, ticks: { color: '#aaa' } } }
        }
    });

    const ramChart = new Chart($("ramChart"), {
        type: 'line',
        data: {
            labels: ['RAM (MB)'],
            datasets: [{
                label: 'RAM (MB)',
                data: [],
                borderColor: '#2ecc71',
                backgroundColor: 'rgba(46, 204, 113, 0.2)',
                tension: 0.3,
                pointRadius: 0
            }]
        },
        options: {
            ...chartOptions,
            scales: { ...chartOptions.scales, y: { min: 0, max: 4096, ticks: { color: '#aaa' } } }
        }
    });

    checkServerStatus();
    setInterval(checkServerStatus, 5000);
</script>

<!-- CSS do Edytora Plików -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/javascript/javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/xml/xml.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/css/css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/htmlmixed/htmlmixed.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/clike/clike.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/python/python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/yaml/yaml.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/properties/properties.min.js"></script>
<script src="/static/js/file_editor.js"></script>

{% endblock %}