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

namespace Pixiview.Illust
{
    public partial class MainPage : IllustDataCollectionPage
    {
        public MainPage()
        {
            Resources.Add("cardView", GetCardViewTemplate());
            InitializeComponent();
        }

        public override void OnLoad()
        {
            StartLoad();
        }

        public override void OnUnload()
        {
            Illusts = IllustCollection.Empty;
            loaded = false;
        }

        protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data, ICommand command)
        {
            return data.body.page.follow.Select(i =>
            {
                var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == i.ToString())?.ConvertToItem();
                if (item != null)
                {
                    item.IllustTapped = command;
                }
                return item;
            });
        }

        protected override IllustData DoLoadIllustData(bool force)
        {
            return Stores.LoadIllustData(force);
        }

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