using CMSMicroservice.Protobuf.Protos.UserOrder;
using DateTimeConverterCL;
using FrontOffice.Main.Utilities;
using Microsoft.AspNetCore.Components;
using Messages = CMSMicroservice.Protobuf.Protos;
using MudBlazor;

namespace FrontOffice.Main.Pages.Store;

public partial class Orders : ComponentBase
{
    [Inject] private OrderService OrderService { get; set; } = default!;

    private List<GetUserOrderResponse> _orders = new();
    private bool _loading;

    protected override async Task OnInitializedAsync()
    {
        _loading = true;
        try
        {
            _orders = await OrderService.GetOrdersAsync();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در بارگذاری سفارش‌ها: {ex.Message}", Severity.Error);
            _orders = new List<GetUserOrderResponse>();
        }
        finally
        {
            _loading = false;
        }
    }

    private void ViewOrder(long orderId)
    {
        Navigation.NavigateTo($"{RouteConstants.Store.OrderDetail}{orderId}");
    }

    private static string FormatPrice(long price) => $"{price:N0}";

    private static string FormatDate(GetUserOrderResponse order)
    {
        try
        {
            var timestamp = order.PaymentDate;
            if (timestamp is null)
                return "در انتظار پرداخت";

            return timestamp.ToDateTime().ToLocalTime().MiladiToJalaliWithTime();
        }
        catch
        {
            return "—";
        }
    }

    private static int GetItemsCount(GetUserOrderResponse order) =>
        order.FactorDetails.Sum(f => f.Count ?? 0);

    private static long GetTotalAmount(GetUserOrderResponse order)
    {
        if (order.VatInfo is not null && order.VatInfo.TotalAmount > 0)
            return order.VatInfo.TotalAmount;

        if (order.Amount > 0)
            return order.Amount;

        return order.FactorDetails.Sum(f => (f.UnitPrice ?? 0) * (f.Count ?? 0));
    }

    private static string GetPaymentStatusText(Messages.PaymentStatus status) => status switch
    {
        Messages.PaymentStatus.Pending => "در انتظار پرداخت",
        Messages.PaymentStatus.Success => "پرداخت شده",
        Messages.PaymentStatus.Reject => "پرداخت ناموفق",
        _ => "نامشخص"
    };

    private static Color GetPaymentStatusColor(Messages.PaymentStatus status) => status switch
    {
        Messages.PaymentStatus.Pending => Color.Warning,
        Messages.PaymentStatus.Success => Color.Success,
        Messages.PaymentStatus.Reject => Color.Error,
        _ => Color.Default
    };

    // Domain/public_messages: None=0, Pending=1, InTransit=2, Delivered=3, Returned=4, Cancelled=5, ReadyForOfficePickup=6
    private static string GetDeliveryStatusText(int status) => status switch
    {
        0 => "بدون ارسال",
        1 => "در انتظار ارسال",
        2 => "ارسال شده",
        3 => "تحویل داده شده",
        4 => "مرجوع شده",
        5 => "لغو شده",
        6 => "آماده تحویل در دفتر",
        _ => "نامشخص"
    };

    private static Color GetDeliveryStatusColor(int status) => status switch
    {
        0 => Color.Default,
        1 => Color.Warning,
        2 => Color.Primary,
        3 => Color.Success,
        4 => Color.Error,
        5 => Color.Dark,
        6 => Color.Primary,
        _ => Color.Default
    };
}
