<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Eversync</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    {% load static %}
    <link rel="icon" href="{% static 'favicon.ico' %}">
    <link rel="stylesheet" href="{% static 'index-style.css' %}" ">

    {% block scripts %}
        {% include "sentry_replay.html" %}
    {% endblock %}
    <style>
    #pomodoro-box {
        display: block;
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: white;
        color: black;
        padding: 40px;
        border-radius: 20px;
        text-align: center;
        font-size: 3rem;
        z-index: 9999;
    }
    #pomodoro-box button {
        font-size: 1.5rem;
        margin: 10px;
        padding: 15px 30px;
        background: #444;
        border: none;
        border-radius: 8px;
        color: white;
        cursor: pointer;
    }
    #pomodoro-settings {
        margin-top: 20px;
        font-size: 1.2rem;
        display: none;
        text-align: center;
    }
    </style>
</head>
<body>
    
    <div id="pomodoro-box" style="display: none;">
        <h3>Focus Timer</h3>
        <p id="timer-display">25:00</p>
        <button onclick="startPomodoro()">Start</button>
        <button onclick="stopPomodoro()">Stop</button>
        <button onclick="toggleSettings()">Settings</button>

        <div id="pomodoro-settings">
            <h4>Settings</h4>
            <label for="session-minutes">Session Length (minutes):</label>
            <input type="number" id="session-minutes" min="1" value="25" style="font-size: 1.2rem; width: 60px;">
            <br><br>
            <button onclick="applySettings()">Apply</button>
        </div>
    </div>

    <div class="header">
        <div class="header-content">
            <a href="/"><img src="{% static 'eversync2.png' %}" alt="Eversync Logo" style="height: 80px; margin-right: 10px; display: flex; align-items: center; gap: 5px;"></a>
            <a href="/" class="logo" >eversync</a>
            <div class="nav-links" style="position: relative;">
                <div class="dropdown">
                    <button class="dropdown-toggle" style="background: none; border: none; color: white; font-size: 16px; cursor: pointer;">
                        Welcome, {{ user.username }} <i class="fas fa-caret-down"></i>
                    </button>
                    <div class="dropdown-menu" style="display: none; position: absolute; right: 0; background-color: #333; border: 1px solid #444; border-radius: 4px; padding: 10px; width: 184px;">
                        
                        <form action="{% url 'manage' %}" method="post" style="margin: 0;">
                            {% csrf_token %}
                            <button type="submit" class="logout-button" style="background-color: transparent; color: white; border: none; cursor: pointer;">Manage Account</button>
                        </form>
                        
                        <form action="{% url 'logoutz' %}" method="post" style="margin: 0;">
                            {% csrf_token %}
                            <button type="submit" class="logout-button" style="background-color: transparent; color: white; border: none; cursor: pointer;">Log Out</button>
                        </form>
                        <button id="themeToggle" class="logout-button" style="background-color: transparent; color: white; border: none; cursor: pointer;">Toggle Dark Mode</button>

                        
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        let pomodoroInterval = null;
        let totalSeconds = 25 * 60;
        let sessionLength = 25;
        let running = false;

        function updateDisplay() {
            const minutes = String(Math.floor(totalSeconds / 60)).padStart(2, '0');
            const seconds = String(totalSeconds % 60).padStart(2, '0');
            document.getElementById('timer-display').textContent = `${minutes}:${seconds}`;
        }

        function applySettings() {
            const input = document.getElementById('session-minutes');
            sessionLength = parseInt(input.value) || 25;
            totalSeconds = sessionLength * 60;
            updateDisplay();
        }

        function startPomodoro() {
            if (running) return;
            running = true;
            pomodoroInterval = setInterval(() => {
                if (totalSeconds > 0) {
                    totalSeconds--;
                    updateDisplay();
                } else {
                    stopPomodoro();
                }
            }, 1000);
        }

        function stopPomodoro() {
            running = false;
            clearInterval(pomodoroInterval);
            totalSeconds = sessionLength * 60;
            updateDisplay();
        }

        function togglePomodoro() {
            document.getElementById('pomodoro-box').style.display = 'block';
        }

        function toggleSettings() {
            const settings = document.getElementById('pomodoro-settings');
            settings.style.display = settings.style.display === 'block' ? 'none' : 'block';
        }

        document.addEventListener('DOMContentLoaded', () => {
            togglePomodoro();
            updateDisplay();

            const dropdownToggle = document.querySelector('.dropdown-toggle');
            const dropdownMenu = document.querySelector('.dropdown-menu');
            dropdownToggle.addEventListener('click', () => {
                dropdownMenu.style.display = dropdownMenu.style.display === 'block' ? 'none' : 'block';
            });
        });

        const toggle = document.getElementById("themeToggle");
        const root = document.documentElement;

        if (localStorage.getItem("theme") === "dark") {
        root.classList.add("dark");
        }

        toggle.addEventListener("click", () => {
        root.classList.toggle("dark");
        if (root.classList.contains("dark")) {
            localStorage.setItem("theme", "dark");
        } else {
            localStorage.setItem("theme", "light");
        }
        });
    </script>
</body>
</html>