using BackOffice.Services.DiscountOrder;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace BackOffice.Pages.DiscountShop.Components;

public partial class OrderDetailsDialog
{
    [CascadingParameter] IMudDialogInstance MudDialog { get; set; } = null!;
    [Inject] public IDiscountOrderService OrderService { get; set; } = default!;

    [Parameter] public DiscountOrderDetailsDto Order { get; set; } = null!;

    private OrderStatus _newStatus;
    private string? _trackingCode;
    private string? _adminNotes;
    private bool _isSaving;
    private List<TimelineStep> _timelineSteps = new();

    protected override void OnInitialized()
    {
        _newStatus = Order.Status;
        _trackingCode = Order.TrackingCode;
        _adminNotes = Order.AdminNotes;
        BuildTimeline();
    }

    private void BuildTimeline()
    {
        var status = Order.Status;
        var steps = new List<TimelineStep>
        {
            new()
            {
                Title = "ثبت سفارش",
                State = status == OrderStatus.Pending ? StepState.Active : StepState.Completed,
                Icon = "1"
            },
            new()
            {
                Title = "پرداخت",
                State = status switch
                {
                    OrderStatus.Pending => StepState.Inactive,
                    OrderStatus.Paid => StepState.Active,
                    OrderStatus.Cancelled => StepState.Inactive,
                    _ => Order.PaymentCompleted ? StepState.Completed : StepState.Inactive
                },
                Icon = "2"
            },
            new()
            {
                Title = "آماده‌سازی",
                State = status switch
                {
                    OrderStatus.Processing => StepState.Active,
                    OrderStatus.Shipped or OrderStatus.Delivered => StepState.Completed,
                    _ => StepState.Inactive
                },
                Icon = "3"
            },
            new()
            {
                Title = "ارسال",
                State = status switch
                {
                    OrderStatus.Shipped => StepState.Active,
                    OrderStatus.Delivered => StepState.Completed,
                    _ => StepState.Inactive
                },
                Icon = "4"
            },
            new()
            {
                Title = "تحویل / مرجوعی",
                State = status switch
                {
                    OrderStatus.Delivered => StepState.Completed,
                    OrderStatus.Returned => StepState.Completed,
                    OrderStatus.Cancelled => StepState.Completed,
                    _ => StepState.Inactive
                },
                Icon = "5"
            }
        };

        _timelineSteps = steps;
    }

    private Color GetStepColor(StepState state)
    {
        return state switch
        {
            StepState.Completed => Color.Success,
            StepState.Active => Color.Info,
            _ => Color.Default
        };
    }

    private async Task SaveAsync()
    {
        _isSaving = true;
        StateHasChanged();

        try
        {
            await OrderService.UpdateStatusAsync(Order.Id, new UpdateOrderStatusDto
            {
                Status = _newStatus,
                TrackingCode = _trackingCode,
                AdminNotes = _adminNotes
            });

            Snackbar.Add("تغییرات سفارش با موفقیت ذخیره شد.", Severity.Success);
            MudDialog.Close(DialogResult.Ok(true));
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در ذخیره تغییرات: {ex.Message}", Severity.Error);
        }
        finally
        {
            _isSaving = false;
            StateHasChanged();
        }
    }

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

    private Color GetStatusColor(OrderStatus status)
    {
        return status switch
        {
            OrderStatus.Pending => Color.Warning,
            OrderStatus.Paid => Color.Info,
            OrderStatus.Processing => Color.Primary,
            OrderStatus.Shipped => Color.Secondary,
            OrderStatus.Delivered => Color.Success,
            OrderStatus.Cancelled => Color.Error,
            OrderStatus.Returned => Color.Dark,
            _ => Color.Default
        };
    }

    private string GetStatusText(OrderStatus status)
    {
        return status switch
        {
            OrderStatus.Pending => "در انتظار پرداخت",
            OrderStatus.Paid => "پرداخت شده",
            OrderStatus.Processing => "در حال آماده‌سازی",
            OrderStatus.Shipped => "ارسال شده",
            OrderStatus.Delivered => "تحویل داده شده",
            OrderStatus.Cancelled => "لغو شده",
            OrderStatus.Returned => "مرجوع شده",
            _ => "نامشخص"
        };
    }

    private enum StepState
    {
        Inactive = 0,
        Active = 1,
        Completed = 2
    }

    private class TimelineStep
    {
        public string Title { get; set; } = string.Empty;
        public StepState State { get; set; }
        public string Icon { get; set; } = string.Empty;
    }
}
