using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using Pixiview.Utils;
using Xamarin.Forms;

namespace Pixiview.Illust
{
    public partial class UserIllustPage : IllustUserDataCollectionPage
    {
        public static readonly BindableProperty UserIconProperty = BindableProperty.Create(
            nameof(UserIcon), typeof(ImageSource), typeof(UserIllustPage));

        public ImageSource UserIcon
        {
            get => (ImageSource)GetValue(UserIconProperty);
            set => SetValue(UserIconProperty, value);
        }

        public IllustItem IllustItem { get; }

        public UserIllustPage(IllustItem illust)
        {
            IllustItem = illust;
            UserIcon = illust.ProfileImage;

            Resources.Add("cardView", GetCardViewTemplate(true));
            InitializeComponent();
        }

        protected override IEnumerable<IllustItem> DoGetIllustList(IllustUserData data, ICommand command)
        {
            return data.body.works.Select(i =>
            {
                var item = i.Value?.ConvertToItem();
                if (item != null)
                {
                    item.IllustTapped = command;
                }
                return item;
            });
        }

        protected override IllustUserData DoLoadIllustData(bool force)
        {
            return Stores.LoadIllustUserData(IllustItem.UserId, force);
        }

        private void Refresh_Clicked(object sender, EventArgs e)
        {
            if (IsLoading)
            {
                return;
            }
            StartLoad(true);
        }
    }
}
