using System;
using CMSMicroservice.Protobuf.Protos.UserOrder;
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!;
    [Inject] private VATService VAT { 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;
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            // بارگذاری نرخ VAT
            await VAT.LoadAsync();
        }
        await base.OnAfterRenderAsync(firstRender);
    }

    private static string FormatPrice(long price) => string.Format("{0:N0} تومان", price);


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

