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

namespace FrontOffice.Main.Pages.Store;

public partial class OrderTracking : ComponentBase
{
    [Inject] private OrderService OrderService { get; set; } = default!;
    [Inject] private IJSRuntime JS { get; set; } = default!;
    
    [Parameter] public long id { get; set; }
    
    private GetUserOrderResponse? _order;
    private bool _loading = true;
    private List<TrackingStep> _trackingSteps = new();

    protected override async Task OnInitializedAsync()
    {
        await LoadOrderAsync();
    }

    private async Task LoadOrderAsync()
    {
        _loading = true;
        StateHasChanged();

        try
        {
            var response = await OrderService.GetOrderAsync(id);
            if (response is not null)
            {
                _order = response;
                BuildTrackingSteps();
            }
        }
        finally
        {
            _loading = false;
            StateHasChanged();
        }
    }

    private void BuildTrackingSteps()
    {
        if (_order is null) return;
        
        // Build tracking steps based on PaymentStatus
        var isPaid = _order.PaymentStatus == Messages.PaymentStatus.Success;
        
        _trackingSteps = new List<TrackingStep>
        {
            new()
            {
                Title = "ثبت سفارش",
                Description = "سفارش شما با موفقیت ثبت شد",
                IsCompleted = true,
                Date = FormatDate(_order.PaymentDate)
            },
            new()
            {
                Title = "در انتظار پرداخت",
                Description = "منتظر تایید پرداخت هستیم",
                IsCompleted = isPaid,
                IsCurrent = !isPaid,
                Date = isPaid ? FormatDate(_order.PaymentDate) : ""
            },
            new()
            {
                Title = "تایید پرداخت",
                Description = "پرداخت شما تایید شد",
                IsCompleted = isPaid,
                IsCurrent = false,
                Date = isPaid ? FormatDate(_order.PaymentDate) : ""
            },
            new()
            {
                Title = "در حال پردازش",
                Description = "سفارش شما در حال آماده‌سازی است",
                IsCompleted = false,
                IsCurrent = isPaid,
                Date = ""
            },
            new()
            {
                Title = "ارسال شده",
                Description = "سفارش شما به پست تحویل داده شد",
                IsCompleted = false,
                IsCurrent = false,
                Date = ""
            },
            new()
            {
                Title = "تحویل داده شده",
                Description = "سفارش به دست شما رسید",
                IsCompleted = false,
                IsCurrent = false,
                Date = ""
            }
        };
    }

    private static string FormatDate(Google.Protobuf.WellKnownTypes.Timestamp? timestamp)
    {
        if (timestamp is null) return "";
        var date = timestamp.ToDateTime();
        var persianCalendar = new System.Globalization.PersianCalendar();
        return $"{persianCalendar.GetYear(date)}/{persianCalendar.GetMonth(date):00}/{persianCalendar.GetDayOfMonth(date):00}";
    }

    private static string GetPaymentMethodText(Messages.PaymentMethod method) => method switch
    {
        Messages.PaymentMethod.Ipg => "پرداخت آنلاین",
        Messages.PaymentMethod.Wallet => "کیف پول",
        _ => "نامشخص"
    };

    private class TrackingStep
    {
        public string Title { get; set; } = "";
        public string Description { get; set; } = "";
        public bool IsCompleted { get; set; }
        public bool IsCurrent { get; set; }
        public string Date { get; set; } = "";
    }

    private async Task GoBack() => await JS.InvokeVoidAsync("history.back");
}
