using Gallery.Illust;
using Xamarin.Forms;

namespace Gallery.UI
{
    public class CardView : ContentView
    {
        public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(
            nameof(CornerRadius), typeof(float), typeof(CardView));
        public static readonly BindableProperty ShadowColorProperty = BindableProperty.Create(
            nameof(ShadowColor), typeof(Color), typeof(CardView));
        public static readonly BindableProperty ShadowRadiusProperty = BindableProperty.Create(
            nameof(ShadowRadius), typeof(float), typeof(CardView), 3f);
        public static readonly BindableProperty ShadowOffsetProperty = BindableProperty.Create(
            nameof(ShadowOffset), typeof(Size), typeof(CardView));
        public static readonly BindableProperty RankProperty = BindableProperty.Create(
            nameof(Rank), typeof(int), typeof(CardView));

        public float CornerRadius
        {
            get => (float)GetValue(CornerRadiusProperty);
            set => SetValue(CornerRadiusProperty, value);
        }
        public Color ShadowColor
        {
            get => (Color)GetValue(ShadowColorProperty);
            set => SetValue(ShadowColorProperty, value);
        }
        public float ShadowRadius
        {
            get => (float)GetValue(ShadowRadiusProperty);
            set => SetValue(ShadowRadiusProperty, value);
        }
        public Size ShadowOffset
        {
            get => (Size)GetValue(ShadowOffsetProperty);
            set => SetValue(ShadowOffsetProperty, value);
        }
        public int Rank
        {
            get => (int)GetValue(RankProperty);
            set => SetValue(RankProperty, value);
        }

        protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
        {
            if (BindingContext is IllustItem illust &&
                illust.Width > 0 &&
                illust.ImageHeight.IsAuto)
            {
                illust.ImageHeight = widthConstraint * illust.Height / illust.Width;
            }
            return base.OnMeasure(widthConstraint, heightConstraint);
        }
    }
}