using LiteCharms.Features;
using LiteCharms.Features.MidrandBooks.Authors;
using LiteCharms.Features.MidrandBooks.Authors.Models;
using LiteCharms.Features.MidrandBooks.Products;
using LiteCharms.Features.MidrandBooks.Products.Models;

namespace MidrandBookshop.Components.Pages;

public partial class ProductView : ComponentBase
{
    [Parameter] public long BookId { get; set; }

    [Inject] private ProductService ProductService { get; set; } = default!;
    [Inject] private AuthorService AuthorService { get; set; } = default!;
    [Inject] private NavigationManager Navigation { get; set; } = default!;

    protected bool IsLoading { get; private set; } = true;
    protected Product? CurrentProduct { get; private set; }
    protected decimal LivePrice { get; private set; } = 0.00m;
    protected string AuthorName { get; private set; } = "Unknown Author";
    protected string PrimaryCategory { get; private set; } = "General";

    protected string ActiveImageUrl { get; set; } = string.Empty;
    protected List<string> Thumbnails { get; private set; } = [];
    protected int Quantity { get; private set; } = 1;

    protected Author? CurrentAuthor { get; private set; }

    protected override async Task OnParametersSetAsync()
    {
        try
        {
            IsLoading = true;
            CurrentProduct = null;
            CurrentAuthor = null;
            Thumbnails.Clear();

            var productResult = await ProductService.GetProductAsync(BookId);

            if (productResult.IsSuccess && productResult.Value != null)
            {
                CurrentProduct = productResult.Value;
                AuthorName = CurrentProduct.Metadata?.Manufacturer ?? "Unknown Author";

                var priceResult = await ProductService.GetProductPriceAsync(BookId);
                LivePrice = priceResult.IsSuccess ? priceResult.Value.Amount : 0m;

                var categoryResult = await ProductService.GetProductCategoriesAsync(BookId);
                if (categoryResult.IsSuccess && categoryResult.Value.Length > 0)
                    PrimaryCategory = categoryResult.Value[0].Name ?? "General";

                var authorResult = await AuthorService.GetAuthorByProductIdAsync(BookId);
                if (authorResult.IsSuccess && authorResult.Value != null)
                {
                    CurrentAuthor = authorResult.Value;

                    if (CurrentAuthor.PublisherType == PublisherTypes.Company && !string.IsNullOrWhiteSpace(CurrentAuthor.Company))
                        AuthorName = CurrentAuthor.Company;
                    else
                        AuthorName = $"{CurrentAuthor.Name} {CurrentAuthor.LastName}".Trim();
                }

                if (!string.IsNullOrWhiteSpace(CurrentProduct.ImageUrl))
                    Thumbnails.Add(CurrentProduct.ImageUrl);

                if (CurrentProduct.ThumbnailUrls != null && CurrentProduct.ThumbnailUrls?.Length != 0)
                    foreach (var url in CurrentProduct.ThumbnailUrls!)
                        if (!string.IsNullOrWhiteSpace(url) && !Thumbnails.Contains(url)) Thumbnails.Add(url);

                ActiveImageUrl = Thumbnails.FirstOrDefault() ?? string.Empty;
            }
        }
        catch (Exception)
        {
            CurrentProduct = null;
        }
        finally
        {
            IsLoading = false;
        }
    }

    protected void IncreaseQty() => Quantity++;
    protected void DecreaseQty() { if (Quantity > 1) Quantity--; }

    protected void HandleAddToCart()
    {
        if (CurrentProduct == null) return;
    }
        
    protected void ViewAllAuthorBooks()
    {
        if (CurrentAuthor is not null)
            Navigation.NavigateTo($"/?authorId={CurrentAuthor.Id}");
    }
}