<script lang="ts">
import { onMount } from 'svelte';
import { fade } from 'svelte/transition';

let text = '';
let targetText = 'taui.shyyluna.dev';
let currentIndex = 0;
let typingComplete = false;
let cursorVisible = true;

// Typing effect
function typeEffect() {
    if (currentIndex < targetText.length) {
        text = targetText.substring(0, currentIndex + 1);
        currentIndex++;
        
        // Randomize typing speed between 50ms and 150ms for a more realistic effect
        const randomSpeed = Math.floor(Math.random() * 100) + 50;
        setTimeout(typeEffect, randomSpeed);
        } else {
        typingComplete = true;
        // Continue cursor blinking after typing is complete
        setInterval(() => {
            cursorVisible = !cursorVisible;
        }, 530);
    }
}

// Start cursor blinking before typing begins
onMount(() => {
    setInterval(() => {
        if (!typingComplete) {
            cursorVisible = !cursorVisible;
        }
    }, 530);
    
    // Start typing after a small delay
    setTimeout(typeEffect, 1000);
});
</script>

<div class="typing-container">
    <span class="typed-text">{text}</span>
    <span class="cursor" class:blink={cursorVisible}>|</span>
</div>

<main>
    <div class="hero-section">
        {#if typingComplete}
            <div class="content" transition:fade={{ duration: 800 }}>
                <h1>Tournament Assistant UI</h1>
                <p>Streamline your tournament management with our powerful and intuitive platform</p>
                <a href="/tournaments" class="cta-button">Use TAUI</a>
            </div>
        {/if}
    </div>

    <div class="features-section">
        <div class="feature-card">
            <div class="feature-icon">
                <i class="material-icons">speed</i>
            </div>
            <h3>Fast & Efficient</h3>
            <p>Organise tournaments with maximum efficiency and minimal effort. We aim to make it as easy as possible ;)</p>
        </div>
        
        <div class="feature-card">
            <div class="feature-icon">
                <i class="material-icons">devices</i>
            </div>
            <h3>Reliable</h3>
            <p>ShyyTAUI is built to be safe and reliable. This means you don't have to worry about accidentally ending a match, or refreshing when you shouldn't.</p>
        </div>
        
        <div class="feature-card">
            <div class="feature-icon">
                <i class="material-icons">code</i>
            </div>
            <h3>Build More</h3>
            <p>Use moons-ta-client to build what you need! This site has the official TournamentAssistant Client documentation.</p>
        </div>
    </div>
</main>

<style>
    /* Typing effect styles */
    .typing-container {
        font-size: 1.5rem;
        font-weight: 700;
        letter-spacing: 1px;
        display: flex;
        align-items: center;
        margin: 2rem 0;
        justify-content: center;
    }

    .typed-text {
        color: var(--accent-color);
        text-shadow: 0 0 10px var(--accent-glow);
    }

    .cursor {
        color: var(--accent-color);
        margin-left: 2px;
        font-weight: 700;
        text-shadow: 0 0 10px var(--accent-glow);
    }

    .cursor.blink {
        opacity: 1;
    }

    .cursor:not(.blink) {
        opacity: 0;
    }

    /* Main content */
    main {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 2rem 1.5rem;
        max-width: 1200px;
        margin: 0 auto;
        width: 100%;
    }

    /* Hero section */
    .hero-section {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        text-align: center;
        padding: 4rem 0;
        width: 100%;
    }

    .content {
        max-width: 800px;
    }

    .hero-section h1 {
        font-size: 3.5rem;
        margin-bottom: 1.5rem;
        background: linear-gradient(90deg, var(--accent-color), var(--accent-hover));
        -webkit-background-clip: text;
        background-clip: text;
        color: transparent;
        text-shadow: 0 0 15px var(--accent-glow);
    }

    .hero-section p {
        font-size: 1.25rem;
        color: var(--text-secondary);
        margin-bottom: 3rem;
        line-height: 1.6;
    }

    /* CTA button - glowing on hover with no fill or border */
    .cta-button {
        display: inline-block;
        font-size: 1.25rem;
        font-weight: 600;
        color: var(--text-primary);
        text-decoration: none;
        padding: 1rem 2.5rem;
        border-radius: 4px;
        background-color: transparent;
        position: relative;
        transition: all 0.3s ease;
        letter-spacing: 1.5px;
        overflow: hidden;
    }

    .cta-button::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 3px;
        background: linear-gradient(90deg, transparent, var(--accent-color), transparent);
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        opacity: 0;
    }

    .cta-button:hover {
        color: var(--accent-hover);
        text-shadow: 0 0 8px var(--accent-glow);
    }

    .cta-button:hover::after {
        transform: translateX(0);
        opacity: 1;
        box-shadow: 0 0 15px var(--accent-glow);
    }

    /* Features section */
    .features-section {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
        gap: 2rem;
        width: 100%;
        margin-top: 3rem;
    }

    .feature-card {
        background-color: var(--bg-secondary);
        border-radius: 8px;
        padding: 2rem;
        text-align: center;
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        border: 1px solid var(--border-color);
    }

    .feature-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    }

    .feature-icon {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 1.5rem;
    }

    .feature-icon i {
        font-size: 3rem;
        color: var(--accent-color);
    }

    .feature-card h3 {
        font-size: 1.5rem;
        margin-bottom: 1rem;
    }

    .feature-card p {
        color: var(--text-secondary);
        line-height: 1.6;
    }

    /* Responsive design */
    @media (max-width: 768px) {
        .hero-section h1 {
            font-size: 2.5rem;
        }

        .hero-section p {
            font-size: 1rem;
        }

        .typing-container {
            font-size: 1.2rem;
        }
    }

    @media (max-width: 480px) {
        .hero-section h1 {
            font-size: 2rem;
        }

        .features-section {
            grid-template-columns: 1fr;
        }
    }
</style>