using Blahblah.FlowerApp.Controls;
using static Blahblah.FlowerApp.Extensions;

namespace Blahblah.FlowerApp;

public class ItemSearchHandler : SearchHandler
{
    public static readonly BindableProperty FlowersProperty = CreateProperty<FlowerClientItem[], ItemSearchHandler>(nameof(Flowers));

    public FlowerClientItem[] Flowers
    {
        get => (FlowerClientItem[])GetValue(FlowersProperty);
        set => SetValue(FlowersProperty, value);
    }

    protected override void OnQueryChanged(string oldValue, string newValue)
    {
        base.OnQueryChanged(oldValue, newValue);

        if (string.IsNullOrWhiteSpace(newValue))
        {
            ItemsSource = null;
        }
        else
        {
            ItemsSource = Flowers?.Where(f => f.Name.Contains(newValue, StringComparison.OrdinalIgnoreCase)).ToList();
        }
    }

    protected override void OnItemSelected(object item)
    {
        base.OnItemSelected(item);
    }
}