using Blahblah.FlowerApp.Data.Model;
using static Blahblah.FlowerApp.Extensions;

namespace Blahblah.FlowerApp.Controls;

public class FlowerClientItem : BindableObject
{
    static readonly BindableProperty NameProperty = CreateProperty<string, FlowerClientItem>(nameof(Name));
    static readonly BindableProperty CategoryIdProperty = CreateProperty<int, FlowerClientItem>(nameof(CategoryId));
    static readonly BindableProperty DaysProperty = CreateProperty<string, FlowerClientItem>(nameof(Days));
    static readonly BindableProperty CoverProperty = CreateProperty<ImageSource?, FlowerClientItem>(nameof(Cover));
    static readonly BindableProperty BoundsProperty = CreateProperty<Rect, FlowerClientItem>(nameof(Bounds));

    public int Id { get; }
    public FlowerItem? FlowerItem { get; }

    public string Name
    {
        get => (string)GetValue(NameProperty);
        set => SetValue(NameProperty, value);
    }
    public int CategoryId
    {
        get => (int)GetValue(CategoryIdProperty);
        set => SetValue(CategoryIdProperty, value);
    }
    public string Days
    {
        get => (string)GetValue(DaysProperty);
        set => SetValue(DaysProperty, value);
    }
    public ImageSource? Cover
    {
        get => (ImageSource?)GetValue(CoverProperty);
        set => SetValue(CoverProperty, value);
    }
    public Rect Bounds
    {
        get => (Rect)GetValue(BoundsProperty);
        set => SetValue(BoundsProperty, value);
    }

    public int? Width { get; set; }
    public int? Height { get; set; }

    public FlowerClientItem(int id)
    {
        Id = id;
    }

    public FlowerClientItem(FlowerItem item) : this(item.Id)
    {
        FlowerItem = item;
        Name = item.Name;
        CategoryId = item.CategoryId;

        if (item.Photos?.Length > 0 && item.Photos[0] is PhotoItem cover)
        {
            Width = cover.Width;
            Height = cover.Height;
        }
    }
}
