using BookReader.Services;
using BookReader.ViewModels;

namespace BookReader.Views;

public partial class BookshelfPage : ContentPage
{
    private readonly BookshelfViewModel _viewModel;
    private readonly INavigationService _navigationService;

    public BookshelfPage(BookshelfViewModel viewModel, INavigationService navigationService, ICachedImageLoadingService imageLoadingService)
    {
        InitializeComponent();
        _viewModel = viewModel;
        _navigationService = navigationService;
        BindingContext = viewModel;
    }

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        await _viewModel.LoadBooksCommand.ExecuteAsync(null);
    }

    private async void OnMenuClicked(object? sender, EventArgs e)
    {
        var action = await _navigationService.DisplayActionSheetAsync("Menu", "Cancel",
            "⚙️ Settings", "☁️ Calibre Library", "ℹ️ About");

        switch (action)
        {
            case "⚙️ Settings":
                await _viewModel.OpenSettingsCommand.ExecuteAsync(null);
                break;
            case "☁️ Calibre Library":
                await _viewModel.OpenCalibreLibraryCommand.ExecuteAsync(null);
                break;
            case "ℹ️ About":
                await _navigationService.DisplayAlertAsync("About", "BookReader v1.0\nEPUB & FB2 Reader", "OK");
                break;
        }
    }
}