using FrontOffice.Main.Utilities;
using CMSMicroservice.Protobuf.Protos.UserAddress;
using FrontOffice.Main.Pages.Profile.Components;
using Microsoft.AspNetCore.Components;
using MudBlazor;

namespace FrontOffice.Main.Pages.Profile;

public partial class Addresses : ComponentBase
{
    [Inject] private UserAddressContract.UserAddressContractClient UserAddressContract { get; set; } = default!;
    [Inject] private AuthService AuthService { get; set; } = default!;

    private List<CustomerAddressModel> _addresses = new();
    private bool _isLoadingAddresses;

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

    private async Task LoadAddresses()
    {
        _isLoadingAddresses = true;
        try
        {
            var response = await UserAddressContract.GetCustomerAddressesAsync(new());
            _addresses = response?.Models?.ToList() ?? new();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در بارگذاری آدرس‌ها: {ex.Message}", Severity.Error);
        }
        finally
        {
            _isLoadingAddresses = false;
            await InvokeAsync(StateHasChanged);
        }
    }

    private async Task OpenAddAddressDialog()
    {
        var dialog = await DialogService.ShowAsync<AddAddressDialog>("افزودن آدرس جدید");
        var result = await dialog.Result;
        if (result is not null && !result.Canceled)
        {
            await LoadAddresses();
            await AuthService.RefreshTokenAsync();
        }
    }

    private async Task OpenEditAddressDialog(CustomerAddressModel address)
    {
        var dialog = await DialogService.ShowAsync<EditAddressDialog>("ویرایش آدرس", new DialogParameters<EditAddressDialog>
        {
            { x => x.Model, address }
        });
        var result = await dialog.Result;
        if (result is not null && !result.Canceled)
            await LoadAddresses();
    }

    private async Task SetAsDefaultAddress(long id)
    {
        try
        {
            await UserAddressContract.SetCustomerDefaultAddressAsync(new() { Id = id });
            Snackbar.Add("آدرس پیش‌فرض تغییر کرد.", Severity.Success);
            await LoadAddresses();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"خطا در تغییر آدرس پیش‌فرض: {ex.Message}", Severity.Error);
        }
    }

    private async Task DeleteAddress(long id)
    {
        var confirm = await DialogService.ShowMessageBox("تأیید حذف", "آیا از حذف این آدرس مطمئن هستید؟", yesText: "حذف", cancelText: "لغو");
        if (confirm == true)
        {
            try
            {
                await UserAddressContract.DeleteCustomerAddressAsync(new() { Id = id });
                Snackbar.Add("آدرس حذف شد.", Severity.Success);
                await LoadAddresses();
                await AuthService.RefreshTokenAsync();
            }
            catch (Exception ex)
            {
                Snackbar.Add($"خطا در حذف آدرس: {ex.Message}", Severity.Error);
            }
        }
    }
}

