@page "/"
@using MudBlazor
@inject ISnackbar Snackbar

<PageTitle>Dashboard | OnProf</PageTitle>

<MudContainer MaxWidth="MaxWidth.ExtraLarge" Class="mt-8">
    <MudGrid>
        <MudItem xs="12">
            <div class="d-flex align-center">
                <MudText Typo="Typo.h4" Class="fw-bold">Hallo, Marc!</MudText>
                <MudChip T="string" Color="Color.Primary" Variant="Variant.Text" Class="ml-4">März 2026</MudChip>
            </div>
            <MudText Typo="Typo.body1" Color="Color.Secondary">Hier ist deine Übersicht für die aktuelle Woche.</MudText>
        </MudItem>

        <MudItem xs="12" sm="6" md="3">
            <MudPaper Elevation="2" Class="pa-4" Style="height: 120px; border-left: 6px solid var(--mud-palette-primary);">
                <MudText Typo="Typo.subtitle2" Color="Color.Secondary">Heute gebucht</MudText>
                <div class="d-flex align-end justify-space-between">
                    <MudText Typo="Typo.h3">6,5 <small style="font-size: 1rem">h</small></MudText>
                    <MudIcon Icon="@Icons.Material.Filled.HistoryToggleOff" Color="Color.Primary" Size="Size.Large" />
                </div>
            </MudPaper>
        </MudItem>

        <MudItem xs="12" sm="6" md="3">
            <MudPaper Elevation="2" Class="pa-4" Style="height: 120px; border-left: 6px solid var(--mud-palette-success);">
                <MudText Typo="Typo.subtitle2" Color="Color.Secondary">Wochenfortschritt</MudText>
                <MudText Typo="Typo.h3">32 <small style="font-size: 1rem">/ 40h</small></MudText>
                <MudProgressLinear Color="Color.Success" Value="80" Class="mt-2" />
            </MudPaper>
        </MudItem>

        <MudItem xs="12" sm="6" md="3">
            <MudPaper Elevation="2" Class="pa-4" Style="height: 120px; border-left: 6px solid var(--mud-palette-info);">
                <MudText Typo="Typo.subtitle2" Color="Color.Secondary">Aktuelles Projekt</MudText>
                <MudText Typo="Typo.h6" Class="mt-2" Style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap;">
                    00000001 - Gleitzeit
                </MudText>
                <MudLink Typo="Typo.caption" Color="Color.Info">Projekt wechseln</MudLink>
            </MudPaper>
        </MudItem>

        <MudItem xs="12" sm="6" md="3">
            <MudPaper Elevation="2" Class="pa-4" Style="height: 120px; border-left: 6px solid var(--mud-palette-warning);">
                <MudText Typo="Typo.subtitle2" Color="Color.Secondary">Urlaub / Überstunden</MudText>
                <div class="d-flex gap-4 mt-2">
                    <div>
                        <MudText Typo="Typo.h6">12</MudText>
                        <MudText Typo="Typo.caption">Urlaub</MudText>
                    </div>
                    <MudDivider Vertical="true" FlexItem="true" />
                    <div>
                        <MudText Typo="Typo.h6">+14,5</MudText>
                        <MudText Typo="Typo.caption">Gleitzeit</MudText>
                    </div>
                </div>
            </MudPaper>
        </MudItem>

        <MudItem xs="12">
            <MudTabs Elevation="2" Rounded="true" ApplyEffectsToContainer="true" PanelClass="pa-6" Color="Color.Primary">
                
                <MudTabPanel Icon="@Icons.Material.Filled.ViewWeek" Text="Wochenansicht">
                    <MudDropContainer @ref="_dropContainer" T="DropItem" Items="_items" ItemsSelector="@((item, dropzone) => item.Status == dropzone)" ItemDropped="ItemUpdated" Class="d-flex flex-column flex-grow-1">
                        <ChildContent>
                            <div class="d-flex justify-space-between align-center mb-6">
                                <MudButtonGroup Color="Color.Default" Variant="Variant.Outlined">
                                    <MudIconButton Icon="@Icons.Material.Filled.ChevronLeft" />
                                    <MudButton>KW 12</MudButton>
                                    <MudIconButton Icon="@Icons.Material.Filled.ChevronRight" />
                                </MudButtonGroup>
                                
                                <div class="d-flex gap-4">
                                    <MudPaper Variant="Variant.Outlined" Class="pa-2 d-flex gap-2 align-center" Style="background-color: var(--mud-palette-background-grey);">
                                        <MudText Typo="Typo.caption" Class="px-2"><b>Projekt-Stapel:</b></MudText>
                                        <MudDropZone T="DropItem" Identifier="Backlog" Class="d-flex gap-2" />
                                    </MudPaper>
                                </div>
                            </div>

                            <div class="d-flex gap-2 overflow-x-auto pb-4">
                                @foreach (var day in _weekDays)
                                {
                                    <div class="flex-1" style="min-width: 180px;">
                                        <MudPaper Elevation="0" Class="pa-3 mb-2" Style="@(day.IsToday ? "background: var(--mud-palette-primary-hover);" : "background: transparent;")">
                                            <MudText Typo="Typo.subtitle2" Align="Align.Center"><b>@day.Name</b></MudText>
                                            <MudText Typo="Typo.caption" Align="Align.Center" Class="d-block">@day.Date.ToShortDateString()</MudText>
                                        </MudPaper>
                                        
                                        <MudDropZone T="DropItem" Identifier="@day.Name" Class="rounded-lg d-flex flex-column gap-2" 
                                                     Style="min-height: 400px; border: 2px dashed var(--mud-palette-divider); padding: 8px;">
                                        </MudDropZone>
                                        
                                        <div class="mt-2 d-flex justify-center">
                                            <MudText Typo="Typo.caption" Color="Color.Secondary">Summe: @(_items.Where(i => i.Status == day.Name).Sum(i => i.Hours))h</MudText>
                                        </div>
                                    </div>
                                }
                            </div>
                        </ChildContent>
                        <ItemRenderer>
                            <MudPaper Elevation="2" Class="pa-3 rounded-lg border-l-4" Style="@($"border-left: 6px solid {context.Color}; cursor: grab; width: 160px; position: relative;")">
                                <div class="d-flex flex-column">
                                    <div class="d-flex justify-space-between align-start">
                                        <MudText Typo="Typo.caption" Class="mud-text-secondary" Style="font-size: 0.7rem">@context.Project</MudText>
                                        @if (context.Status != "Backlog")
                                        {
                                            <MudIconButton Icon="@Icons.Material.Filled.Delete" Size="Size.Small" Color="Color.Error" 
                                                           Style="position: absolute; top: 0px; right: 0px; padding: 4px;" 
                                                           OnClick="() => DeleteItem(context)" />
                                        }
                                    </div>
                                    <MudText Typo="Typo.body2" Class="mt-1"><b>@context.Task</b></MudText>
                                    <div class="d-flex align-center justify-space-between mt-1">
                                        <MudNumericField @bind-Value="context.Hours" Variant="Variant.Text" Margin="Margin.Dense" Style="width: 50px;" T="double" Step="0.5" Min="0" />
                                        <MudIcon Icon="@Icons.Material.Filled.DragIndicator" Size="Size.Small" Color="Color.Error" />
                                    </div>
                                </div>
                            </MudPaper>
                        </ItemRenderer>
                    </MudDropContainer>
                </MudTabPanel>
                
                <MudTabPanel Icon="@Icons.Material.Filled.List" Text="Alle Buchungen">
                    <MudDataGrid T="BookingPlaceholder" Items="@_dummyBookings" Hover="true" Bordered="false" Striped="true" QuickFilter="@_quickFilter">
                        <ToolBarContent>
                            <MudText Typo="Typo.h6">Verlauf</MudText>
                            <MudSpacer />
                            <MudTextField @bind-Value="_searchString" Placeholder="Suchen..." Adornment="Adornment.Start" Immediate="true"
                                          AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0"></MudTextField>
                        </ToolBarContent>
                        <Columns>
                            <PropertyColumn Property="x => x.Date" Title="Datum" Format="dd.MM.yyyy" />
                            <PropertyColumn Property="x => x.Project" Title="Auftrag" />
                            <PropertyColumn Property="x => x.Task" Title="Tätigkeit" />
                            <PropertyColumn Property="x => x.Hours" Title="Stunden" />
                            <TemplateColumn CellClass="d-flex justify-end">
                                <CellTemplate>
                                    <MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Edit" Color="Color.Default" />
                                    <MudIconButton Size="@Size.Small" Icon="@Icons.Material.Outlined.Delete" Color="Color.Error" />
                                </CellTemplate>
                            </TemplateColumn>
                        </Columns>
                    </MudDataGrid>
                </MudTabPanel>
            </MudTabs>
        </MudItem>
    </MudGrid>
</MudContainer>

<MudFab Color="Color.Primary" StartIcon="@Icons.Material.Filled.Add" Style="position: fixed; bottom: 24px; right: 24px;" OnClick="OpenBookingDialog" />

@code {
    private MudDropContainer<DropItem> _dropContainer;
    private string _searchString = "";
    private List<DayInfo> _weekDays = new();
    private List<DropItem> _items = new();
    
    // Datenstrukturen
    public class DropItem
    {
        public string Project { get; set; } = string.Empty;
        public string Task { get; set; } = string.Empty;
        public double Hours { get; set; }
        public string Color { get; set; } = "#7e6fff";
        public string Status { get; set; } = "Backlog";
    }

    public record DayInfo(string Name, DateTime Date, bool IsToday);
    public record BookingPlaceholder(DateTime Date, string Project, string Task, double Hours);

    // Dummy Daten für das DataGrid
    private List<BookingPlaceholder> _dummyBookings = new()
    {
        new(DateTime.Now, "00000001 - Gleitzeit", "Projektarbeit", 0.0),
        new(DateTime.Now.AddDays(-1), "00000001 - Gleitzeit", "Meeting", 0.0),
        new(DateTime.Now.AddDays(-2), "00000010 - Allg. Besprechung", "Jour Fixe", 0.0),
    };

    protected override void OnInitialized()
    {
        // Wochentage generieren (Mo-Fr)
        var startOfWeek = DateTime.Now.AddDays(-(int)DateTime.Now.DayOfWeek + (int)DayOfWeek.Monday);
        for (int i = 0; i < 5; i++)
        {
            var date = startOfWeek.AddDays(i);
            _weekDays.Add(new DayInfo(date.ToString("dddd"), date, date.Date == DateTime.Today));
        }

        // Stapel an Vorlagen im Backlog
        _items.Add(new DropItem { Project = "00001", Task = "Gleitzeit", Hours = 0, Color = "#7e6fff", Status = "Backlog" });
        _items.Add(new DropItem { Project = "00010", Task = "Meeting", Hours = 0, Color = "#3dcb6c", Status = "Backlog" });
        _items.Add(new DropItem { Project = "00500", Task = "Entwicklung", Hours = 0, Color = "#ffb545", Status = "Backlog" });
        
    }

    private void ItemUpdated(MudItemDropInfo<DropItem> dropInfo)
    {
        if (dropInfo.DropzoneIdentifier != "Backlog" && dropInfo.Item.Status == "Backlog")
        {
            // Erstelle eine echte Kopie vom Template und füge sie sofort hinzu
            var newItem = new DropItem
            {
                Project = dropInfo.Item.Project,
                Task = dropInfo.Item.Task,
                Hours = dropInfo.Item.Hours,
                Color = dropInfo.Item.Color,
                Status = dropInfo.DropzoneIdentifier
            };
            _items.Add(newItem);
            Snackbar.Add($"{newItem.Task} zum {dropInfo.DropzoneIdentifier} hinzugefügt", Severity.Success);
        }
        else
        {
            // Einfaches Verschieben zwischen den Tagen
            dropInfo.Item.Status = dropInfo.DropzoneIdentifier;
        }

        _dropContainer.Refresh();
    }

    private void DeleteItem(DropItem item)
    {
        if (item.Status != "Backlog")
        {
            _items.Remove(item);
            Snackbar.Add($"{item.Task} gelöscht", Severity.Warning);
            _dropContainer.Refresh();
        }
    }

    private Func<BookingPlaceholder, bool> _quickFilter => x =>
    {
        if (string.IsNullOrWhiteSpace(_searchString)) return true;
        return x.Project.Contains(_searchString, StringComparison.OrdinalIgnoreCase) || 
               x.Task.Contains(_searchString, StringComparison.OrdinalIgnoreCase);
    };

    private void OpenBookingDialog()
    {
        Snackbar.Add("Dialog für manuelle Buchung wird implementiert...", Severity.Info);
    }
}