using Blahblah.FlowerApp.Data;
using Blahblah.FlowerApp.Views.User;
using Microsoft.Extensions.Logging;
using System.Globalization;
using static Blahblah.FlowerApp.Extensions;

namespace Blahblah.FlowerApp;

public partial class UserPage : AppContentPage
{
    static readonly BindableProperty LogCountProperty = CreateProperty<string, UserPage>(nameof(LogCount));

    public string LogCount
    {
        get => GetValue<string>(LogCountProperty);
        set => SetValue(LogCountProperty, value);
    }

    public UserPage(FlowerDatabase database, ILogger<UserPage> logger) : base(database, logger)
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        Task.Run(async () =>
        {
            var count = await Database.GetLogCount();
            LogCount = L("logs", "{count} logs").Replace("{count}", count.ToString());
        });
    }

    private async void Log_Tapped(object sender, EventArgs e)
    {
        var logPage = new LogPage(Database, Logger);
        await Navigation.PushAsync(logPage);
    }
}

class UserNameConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is string name)
        {
            return L("welcome", "Welcome, {name}").Replace("{name}", name);
        }
        return null;
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}