@page "/keyboard"

<PageTitle>Keyboard</PageTitle>

<div class="kb-layout">
<div class="kb-left">

@if (toast != null)
{
    <div class="toast-notification @(toast.Visible ? "toast-show" : "toast-hide")">
        <div class="toast-content">
            <span class="toast-icon">&#x26A0;</span>
            <div class="toast-text">
                <span class="toast-title">@toast.SkillName</span>
                <span class="toast-message">Cannot cast yet — @toast.Remaining seconds remaining</span>
            </div>
        </div>
    </div>
}

<div class="character-selector">
    <label>Character:</label>
    <select @bind="selectedCharacter" @bind:after="RebuildKeyboardData">
        @foreach (var c in Characters)
        {
            <option value="@c">@c</option>
        }
    </select>
</div>

<div class="keyboard-container" @onkeydown="HandleKeyDown" tabindex="0" @ref="containerRef">
    <div class="keyboard-wrapper">
        <div class="kb-row">
            @foreach (var k in Keys.Take(6))
            {
                <div class="kb-key @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)" title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-offset-1">
            @foreach (var k in Keys.Skip(6).Take(5))
            {
                <div class="kb-key @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)" title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-offset-2">
            @foreach (var k in Keys.Skip(11).Take(5))
            {
                <div class="kb-key @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)" title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-offset-1">
            @foreach (var k in Keys.Skip(16).Take(5))
            {
                <div class="kb-key @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)" title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-space-row">
            <div class="kb-key kb-space @(spaceKey.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(spaceKey)"
                 title="@spaceKey.Tooltip">
                <span class="key-label">@spaceKey.Label</span>
                @if (spaceKey.SkillName != null)
                {
                    <span class="skill-name">@spaceKey.SkillName</span>
                }
                @if (spaceKey.OnCooldown)
                {
                    <div class="cd-overlay">
                        <svg viewBox="0 0 100 100" class="cd-svg">
                            <circle cx="50" cy="50" r="45" class="cd-bg"/>
                            <circle cx="50" cy="50" r="45" class="cd-progress"
                                    stroke-dasharray="282.74"
                                    stroke-dashoffset="@(282.74 * (1 - spaceKey.CooldownFraction))"/>
                        </svg>
                        <span class="cd-text">@spaceKey.RemainingSeconds</span>
                    </div>
                }
            </div>
        </div>

        <div class="kb-divider">Shift + Key</div>

        <div class="kb-row">
            @foreach (var k in ShiftKeys.Take(6))
            {
                <div class="kb-key kb-shift @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)"
                     title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-offset-1">
            @foreach (var k in ShiftKeys.Skip(6).Take(5))
            {
                <div class="kb-key kb-shift @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)"
                     title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-offset-2">
            @foreach (var k in ShiftKeys.Skip(11).Take(5))
            {
                <div class="kb-key kb-shift @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)"
                     title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-offset-1">
            @foreach (var k in ShiftKeys.Skip(16).Take(5))
            {
                <div class="kb-key kb-shift @(k.OnCooldown ? "on-cd" : "")" @onclick="() => ActivateKey(k)"
                     title="@k.Tooltip">
                    <span class="key-label">@k.Label</span>
                    @if (k.SkillName != null)
                    {
                        <span class="skill-name">@k.SkillName</span>
                    }
                    @if (k.OnCooldown)
                    {
                        <div class="cd-overlay">
                            <svg viewBox="0 0 100 100" class="cd-svg">
                                <circle cx="50" cy="50" r="45" class="cd-bg"/>
                                <circle cx="50" cy="50" r="45" class="cd-progress"
                                        stroke-dasharray="282.74"
                                        stroke-dashoffset="@(282.74 * (1 - k.CooldownFraction))"/>
                            </svg>
                            <span class="cd-text">@k.RemainingSeconds</span>
                        </div>
                    }
                </div>
            }
        </div>
        <div class="kb-row kb-space-row">
            <div class="kb-key kb-space kb-shift @(shiftSpaceKey.OnCooldown ? "on-cd" : "")"
                 @onclick="() => ActivateKey(shiftSpaceKey)" title="@shiftSpaceKey.Tooltip">
                <span class="key-label">@shiftSpaceKey.Label</span>
                @if (shiftSpaceKey.SkillName != null)
                {
                    <span class="skill-name">@shiftSpaceKey.SkillName</span>
                }
                @if (shiftSpaceKey.OnCooldown)
                {
                    <div class="cd-overlay">
                        <svg viewBox="0 0 100 100" class="cd-svg">
                            <circle cx="50" cy="50" r="45" class="cd-bg"/>
                            <circle cx="50" cy="50" r="45" class="cd-progress"
                                    stroke-dasharray="282.74"
                                    stroke-dashoffset="@(282.74 * (1 - shiftSpaceKey.CooldownFraction))"/>
                        </svg>
                        <span class="cd-text">@shiftSpaceKey.RemainingSeconds</span>
                    </div>
                }
            </div>
        </div>
    </div>
</div>
</div>

<div class="kb-right">
    @if (selectedSkill != null)
    {
        <div class="skill-detail">
            <div class="skill-detail-header">
                <h3 class="skill-detail-name">@Path.GetFileNameWithoutExtension(selectedSkill.FileName)</h3>
                <span class="skill-detail-key">@selectedSkill.Key</span>
            </div>

            @if (!string.IsNullOrEmpty(selectedSkill.Description))
            {
                <div class="skill-detail-desc">
                    @foreach (var line in selectedSkill.Description.Split('\n'))
                    {
                        <p>@line</p>
                    }
                </div>
            }

            <div class="skill-detail-stats">
                @if (!string.IsNullOrEmpty(selectedSkill.Cast))
                {
                    <div class="stat-row">
                        <span class="stat-label">Cast</span>
                        <span class="stat-value">@selectedSkill.Cast</span>
                    </div>
                }
                @if (ParseValue(selectedSkill.Damage) > 0)
                {
                    <div class="stat-row">
                        <span class="stat-label">Damage</span>
                        <span class="stat-value">@FormatStat(selectedSkill.Damage)</span>
                    </div>
                }
                @if (!string.IsNullOrEmpty(selectedSkill.DamageType))
                {
                    <div class="stat-row">
                        <span class="stat-label">Type</span>
                        <span class="stat-value">@selectedSkill.DamageType</span>
                    </div>
                }
                @if (ParseValue(selectedSkill.Heal) > 0)
                {
                    <div class="stat-row">
                        <span class="stat-label">Healing</span>
                        <span class="stat-value">@FormatStat(selectedSkill.Heal)</span>
                    </div>
                }
                @if (ParseValue(selectedSkill.Shield) > 0)
                {
                    <div class="stat-row">
                        <span class="stat-label">Shield</span>
                        <span class="stat-value">@FormatStat(selectedSkill.Shield)</span>
                    </div>
                }
                @if (!string.IsNullOrEmpty(selectedSkill.Cooldown) && ParseValue(selectedSkill.Cooldown) > 0)
                {
                    <div class="stat-row">
                        <span class="stat-label">Cooldown</span>
                        <span class="stat-value">@selectedSkill.Cooldown s</span>
                    </div>
                }
                @if (ParseValue(selectedSkill.Mana) > 0)
                {
                    <div class="stat-row">
                        <span class="stat-label">Mana</span>
                        <span class="stat-value">@FormatStat(selectedSkill.Mana)</span>
                    </div>
                }
                @if (!string.IsNullOrEmpty(selectedSkill.Range) && ParseValue(selectedSkill.Range) > 0)
                {
                    <div class="stat-row">
                        <span class="stat-label">Range</span>
                        <span class="stat-value">@selectedSkill.Range</span>
                    </div>
                }
            </div>

            @if (selectedSkill.Tags is { Count: > 0 })
            {
                <div class="skill-detail-tags">
                    @foreach (var tag in selectedSkill.Tags)
                    {
                        <span class="tag-badge">@tag</span>
                    }
                </div>
            }
        </div>
    }
    else
    {
        <div class="skill-detail skill-detail-empty">
            <div class="empty-hint">
                <span class="empty-icon">&#x2328;</span>
                <p>Press a key to view skill details</p>
            </div>
        </div>
    }
</div>
</div>

@code {
    private ElementReference containerRef;

    private List<KeyData> Keys { get; set; } = new();
    private List<KeyData> ShiftKeys { get; set; } = new();
    private KeyData spaceKey = null!;
    private KeyData shiftSpaceKey = null!;
    private string selectedCharacter = "Xavian";
    private List<string> Characters = [];
    private ToastData? toast;
    private CancellationTokenSource? toastCts;
    private SkillDoc? selectedSkill;

    public class KeyData
    {
        public string Id { get; set; } = "";
        public string Label { get; set; } = "";
        public string? SkillName { get; set; }
        public string? Character { get; set; }
        public SkillDoc? SkillDoc { get; set; }
        public double CooldownDuration { get; set; }
        public double Remaining { get; set; }
        public bool OnCooldown => Remaining > 0;
        public double RemainingSeconds => Math.Ceiling(Remaining);
        public double CooldownFraction => CooldownDuration > 0 ? Math.Min(1, Remaining / CooldownDuration) : 0;

        public void Tick(double seconds)
        {
            if (Remaining > 0)
                Remaining = Math.Max(0, Remaining - seconds);
        }

        public string Tooltip
        {
            get
            {
                var parts = new List<string>();
                if (Character != null) parts.Add(Character);
                if (SkillName != null) parts.Add(SkillName);
                if (CooldownDuration > 0) parts.Add($"CD: {CooldownDuration}s");
                return parts.Count > 0 ? string.Join(" - ", parts) : Label;
            }
        }
    }

    protected override void OnInitialized()
    {
        Characters = DocsData.All
            .OfType<SkillDoc>()
            .Where(s => s.Character != null)
            .Select(s => s.Character!)
            .Distinct()
            .Order()
            .ToList();

        if (Characters.Count == 0) Characters.Add("Xavian");

        RebuildKeyboardData();
    }

    private void RebuildKeyboardData()
    {
        var skillByKey = DocsData.All
            .OfType<SkillDoc>()
            .Where(s => !string.IsNullOrEmpty(s.Key) && s.Character == selectedCharacter)
            .GroupBy(s => s.Key!)
            .ToDictionary(g => g.Key, g =>
                g.OrderBy(s => string.IsNullOrEmpty(s.CostSwiftReprieval) ? 0 : 1).First());

        var actionByKey = DocsData.All
            .OfType<KeyDoc>()
            .Where(k => !string.IsNullOrEmpty(k.Action))
            .Select(k => new
            {
                Key = Path.GetFileNameWithoutExtension(k.FileName),
                k.Action
            })
            .GroupBy(x => x.Key)
            .ToDictionary(g => g.Key, g => g.First().Action!, StringComparer.OrdinalIgnoreCase);

        var keyLabels = new[] { "1", "2", "3", "4", "5", "6", "Q", "W", "E", "R", "T", "A", "S", "D", "F", "G", "Z", "X", "C", "V", "B" };

        Keys = keyLabels.Select(label =>
        {
            var skill = skillByKey.GetValueOrDefault(label);
            var action = actionByKey.GetValueOrDefault(label);
            var skillName = skill != null ? Path.GetFileNameWithoutExtension(skill.FileName) : action;
            return new KeyData
            {
                Id = label.ToLower(),
                Label = label,
                SkillName = skillName,
                Character = skill?.Character,
                SkillDoc = skill,
                CooldownDuration = ParseCooldown(skill?.Cooldown)
            };
        }).ToList();

        var spaceSkill = skillByKey.GetValueOrDefault(" ");
        var spaceAction = actionByKey.GetValueOrDefault("Space");
        spaceKey = new KeyData
        {
            Id = "space",
            Label = "Space",
            SkillName = spaceSkill != null ? Path.GetFileNameWithoutExtension(spaceSkill.FileName) : spaceAction,
            Character = spaceSkill?.Character,
            SkillDoc = spaceSkill,
            CooldownDuration = ParseCooldown(spaceSkill?.Cooldown)
        };

        ShiftKeys = keyLabels.Select(label =>
        {
            var shiftKeyStr = "Shift + " + label;
            var skill = skillByKey.GetValueOrDefault(shiftKeyStr);
            var action = actionByKey.GetValueOrDefault(shiftKeyStr);
            var skillName = skill != null ? Path.GetFileNameWithoutExtension(skill.FileName) : action;
            return new KeyData
            {
                Id = "shift-" + label.ToLower(),
                Label = "Shift+" + label,
                SkillName = skillName,
                Character = skill?.Character,
                SkillDoc = skill,
                CooldownDuration = ParseCooldown(skill?.Cooldown)
            };
        }).ToList();

        var shiftSpaceSkill = skillByKey.GetValueOrDefault("Shift + Space");
        var shiftSpaceAction = actionByKey.GetValueOrDefault("Shift + Space");
        shiftSpaceKey = new KeyData
        {
            Id = "shift-space",
            Label = "Shift+Space",
            SkillName = shiftSpaceSkill != null ? Path.GetFileNameWithoutExtension(shiftSpaceSkill.FileName) : shiftSpaceAction,
            Character = shiftSpaceSkill?.Character,
            SkillDoc = shiftSpaceSkill,
            CooldownDuration = ParseCooldown(shiftSpaceSkill?.Cooldown)
        };

        selectedSkill = null;
    }

    private static double ParseCooldown(string? cooldown)
    {
        if (string.IsNullOrEmpty(cooldown)) return 0;
        if (double.TryParse(cooldown, out var result)) return result;
        return 0;
    }

    private static double ParseValue(string? value)
    {
        if (string.IsNullOrEmpty(value)) return 0;
        var clean = value.Replace(",", "").Trim();
        if (double.TryParse(clean, out var result)) return result;
        return 0;
    }

    private static string FormatStat(string? value)
    {
        if (string.IsNullOrEmpty(value)) return "";
        return value;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await containerRef.FocusAsync();
        }
    }

    private void HandleKeyDown(KeyboardEventArgs e)
    {
        var isShift = e.ShiftKey;
        var key = e.Key.ToLower();

        var allKeys = new List<KeyData>();
        allKeys.AddRange(Keys);
        allKeys.Add(spaceKey);
        allKeys.AddRange(ShiftKeys);
        allKeys.Add(shiftSpaceKey);

        KeyData? target;
        if (isShift)
        {
            if (key == "shift") return;
            var lookup = "shift-" + (key == " " ? "space" : key);
            target = allKeys.FirstOrDefault(k => k.Id == lookup);
        }
        else
        {
            var lookup = key == " " ? "space" : key;
            target = allKeys.FirstOrDefault(k => k.Id == lookup);
        }

        if (target != null)
        {
            ActivateKey(target);
        }
    }

    private record ToastData(string SkillName, double Remaining, bool Visible);

    private async void ShowToast(string skillName, double remaining)
    {
        toastCts?.Cancel();
        toastCts = new CancellationTokenSource();
        var token = toastCts.Token;

        toast = new ToastData(skillName, remaining, true);
        StateHasChanged();

        try
        {
            await Task.Delay(2000, token);
            if (!token.IsCancellationRequested)
            {
                toast = toast with { Visible = false };
                StateHasChanged();
                await Task.Delay(300, token);
                if (!token.IsCancellationRequested)
                {
                    toast = null;
                    StateHasChanged();
                }
            }
        }
        catch (TaskCanceledException) { }
    }

    private void ActivateKey(KeyData key)
    {
        if (key.OnCooldown)
        {
            ShowToast(key.SkillName ?? key.Label, Math.Ceiling(key.Remaining));
            return;
        }
        if (key.Character == null) return;

        selectedSkill = key.SkillDoc;

        var duration = key.CooldownDuration > 0 ? key.CooldownDuration : 0.5;
        key.Remaining = duration;

        var allKeys = new List<KeyData>();
        allKeys.AddRange(Keys);
        allKeys.Add(spaceKey);
        allKeys.AddRange(ShiftKeys);
        allKeys.Add(shiftSpaceKey);

        foreach (var k in allKeys)
        {
            k.Tick(1.5);
        }
    }

}
