using System;
//using System.Text.Json.Serialization;
using Newtonsoft.Json;
using Xamarin.Forms;

namespace Gallery.Util.Model
{
    //[JsonConverter(typeof(GalleryItemConverter))]
    [JsonObject(MemberSerialization.OptIn)]
    public class GalleryItem : BindableObject
    {
        const double PREVIEW_WIDTH = 200.0;

        public static readonly BindableProperty TagDescriptionProperty = BindableProperty.Create(nameof(TagDescription), typeof(string), typeof(GalleryItem));
        public static readonly BindableProperty PreviewImageProperty = BindableProperty.Create(nameof(PreviewImage), typeof(ImageSource), typeof(GalleryItem));
        public static readonly BindableProperty UserNameProperty = BindableProperty.Create(nameof(UserName), typeof(string), typeof(GalleryItem));
        public static readonly BindableProperty CreatedTimeProperty = BindableProperty.Create(nameof(CreatedTime), typeof(DateTime), typeof(GalleryItem));
        public static readonly BindableProperty UpdatedTimeProperty = BindableProperty.Create(nameof(UpdatedTime), typeof(DateTime), typeof(GalleryItem));
        public static readonly BindableProperty ImageHeightProperty = BindableProperty.Create(nameof(ImageHeight), typeof(GridLength), typeof(GalleryItem),
            defaultValue: GridLength.Auto);
        public static readonly BindableProperty IsFavoriteProperty = BindableProperty.Create(nameof(IsFavorite), typeof(bool), typeof(GalleryItem));
        public static readonly BindableProperty BookmarkIdProperty = BindableProperty.Create(nameof(BookmarkId), typeof(string), typeof(GalleryItem));

        public string TagDescription
        {
            get => (string)GetValue(TagDescriptionProperty);
            set => SetValue(TagDescriptionProperty, value);
        }
        public ImageSource PreviewImage
        {
            get => (ImageSource)GetValue(PreviewImageProperty);
            set => SetValue(PreviewImageProperty, value);
        }
        public GridLength ImageHeight
        {
            get => (GridLength)GetValue(ImageHeightProperty);
            set => SetValue(ImageHeightProperty, value);
        }
        public bool IsFavorite
        {
            get => (bool)GetValue(IsFavoriteProperty);
            set => SetValue(IsFavoriteProperty, value);
        }

        [JsonProperty]
        public string BookmarkId
        {
            get => (string)GetValue(BookmarkIdProperty);
            set => SetValue(BookmarkIdProperty, value);
        }

        [JsonProperty]
        public long Id { get; internal set; }
        private string[] tags;
        [JsonProperty]
        public string[] Tags
        {
            get => tags;
            set
            {
                tags = value;
                if (value != null)
                {
                    TagDescription = string.Join(' ', tags);
                }
                else
                {
                    TagDescription = null;
                }
            }
        }
        [JsonProperty]
        public DateTime CreatedTime { get; set; }
        [JsonProperty]
        public DateTime UpdatedTime { get; set; }
        [JsonProperty]
        public string UserId { get; set; }
        [JsonProperty]
        public string UserName { get; set; }
        [JsonProperty]
        public string Source { get; set; }
        [JsonProperty]
        public string PreviewUrl { get; set; }
        [JsonProperty]
        public string RawUrl { get; set; }
        [JsonProperty]
        public bool IsRawPage { get; set; }

        private int width;
        private int height;
        [JsonProperty]
        public int Width
        {
            get => width;
            set
            {
                width = value;
                if (width > 0 && height > 0)
                {
                    ImageHeight = new GridLength(PREVIEW_WIDTH * height / width);
                }
            }
        }
        [JsonProperty]
        public int Height
        {
            get => height;
            set
            {
                height = value;
                if (width > 0 && height > 0)
                {
                    ImageHeight = new GridLength(PREVIEW_WIDTH * height / width);
                }
            }
        }

        internal GalleryItem() { }

        public GalleryItem(long id)
        {
            Id = id;
        }

        public override string ToString()
        {
            var source = string.IsNullOrEmpty(Source) ? RawUrl : Source;
            return $"{Id}, {source}";
        }
    }
}