@using Indotalent.Features.Utilities.Todo.Cqrs
@using MudBlazor
@inject TodoService TodoService
@inject ISnackbar Snackbar
<MudDialog>
    <DialogContent>
        <MudForm @ref="_form" Model="_model">
            <MudGrid Spacing="2">
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Item Name</MudText>
                    <MudTextField @bind-Value="_model.Name" Required="true" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" sm="6">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Start Date</MudText>
                    <MudDatePicker @bind-Date="_startDate" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" sm="6">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Start Time</MudText>
                    <MudTimePicker @bind-Time="_startTime" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" sm="6">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">End Date</MudText>
                    <MudDatePicker @bind-Date="_endDate" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12" sm="6">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">End Time</MudText>
                    <MudTimePicker @bind-Time="_endTime" Variant="Variant.Outlined" Margin="Margin.Dense" />
                </MudItem>
                <MudItem xs="12">
                    <MudCheckBox @bind-Value="_model.IsCompleted" Label="Completed" Color="Color.Primary" />
                </MudItem>
                <MudItem xs="12">
                    <MudText Typo="Typo.subtitle2" Class="mb-1">Description</MudText>
                    <MudTextField @bind-Value="_model.Description" Variant="Variant.Outlined" Margin="Margin.Dense" Lines="3" />
                </MudItem>
            </MudGrid>
        </MudForm>
    </DialogContent>
    <DialogActions>
        <MudButton OnClick="Cancel" Variant="Variant.Text">Cancel</MudButton>
        <MudButton Color="Color.Primary" OnClick="Submit" Variant="Variant.Filled" Disabled="_processing" Style="text-transform: none; font-weight: 700; border-radius: 4px;">
            @if (_processing)
            {
                <MudProgressCircular Size="Size.Small" Indeterminate="true" />
                <MudText Class="ms-2" Typo="Typo.button" Style="text-transform: none !important;">Processing...</MudText>
            }
            else
            {
                <MudText>Add Item</MudText>
            }
        </MudButton>
    </DialogActions>
</MudDialog>

@code {
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = default!;
    [Parameter] public string TodoId { get; set; } = string.Empty;
    private MudForm _form = default!;
    private CreateTodoItemRequest _model = new();
    private bool _processing = false;

    private DateTime? _startDate = DateTime.Today;
    private TimeSpan? _startTime = DateTime.Now.TimeOfDay;
    private DateTime? _endDate = DateTime.Today;
    private TimeSpan? _endTime = DateTime.Now.TimeOfDay.Add(TimeSpan.FromHours(1));

    private void Cancel() => MudDialog.Cancel();

    private async Task Submit()
    {
        if (_startDate.HasValue && _startTime.HasValue)
            _model.StartTime = _startDate.Value.Date.Add(_startTime.Value);
        
        if (_endDate.HasValue && _endTime.HasValue)
            _model.EndTime = _endDate.Value.Date.Add(_endTime.Value);

        await _form.Validate();
        if (!_form.IsValid) return;
        _processing = true;
        _model.TodoId = TodoId;
        try
        {
            var response = await TodoService.CreateTodoItemAsync(_model);
            await Task.Delay(500);
            if (response != null && response.IsSuccess)
            {
                Snackbar.Add("Item added successfully", Severity.Success);
                MudDialog.Close(DialogResult.Ok(true));
            }
        }
        finally { _processing = false; }
    }
}