using System;
using System.Collections.Generic;
using System.Linq;
using Pixiview.UI;
using Pixiview.Utils;
using Xamarin.Forms;

namespace Pixiview.Illust
{
    public partial class RelatedIllustsPage : IllustRecommendsCollectionPage
    {
        private const int STEP = 18;

        private readonly IllustItem illustItem;
        private int startIndex;
        private int nextIndex;
        private string[] illustIds;

        public RelatedIllustsPage(IllustItem item)
        {
            illustItem = item;

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

            startIndex = -1;
            nextIndex = 0;
        }

        protected override bool IsAutoReload => false;
        protected override ActivityIndicator LoadingIndicator => activityLoading;

        protected override IEnumerable<IllustItem> DoGetIllustList(IllustRecommendsData data)
        {
            if (data.body == null)
            {
                return null;
            }
            return data.body.illusts.Select(i => i.ConvertToItem());
        }

        protected override IllustRecommendsData DoLoadIllustData(bool force)
        {
            IllustRecommendsData data;
            if (startIndex < 0)
            {
                // init
                data = Stores.LoadIllustRecommendsInitData(illustItem.Id);
                if (data == null || data.body == null)
                {
                    return null;
                }
                illustIds = data.body.nextIds;
            }
            else
            {
                if (illustIds == null || startIndex >= illustIds.Length)
                {
                    return null;
                }
                var ids = illustIds.Skip(startIndex).Take(STEP).ToArray();
                data = Stores.LoadIllustRecommendsListData(illustItem.Id, ids);
                nextIndex = startIndex + STEP;
                if (ids.Length == 0 || nextIndex >= illustIds.Length)
                {
                    // done
#if DEBUG
                    App.DebugPrint($"download completed: {startIndex}");
#endif
                    startIndex = nextIndex;
                }
            }
            return data;
        }

        private void FlowLayout_MaxHeightChanged(object sender, HeightEventArgs e)
        {
            SetOffset(e.ContentHeight - scrollView.Bounds.Height - SCROLL_OFFSET);
        }

        protected override bool CheckRefresh()
        {
            if (nextIndex > startIndex)
            {
                startIndex = nextIndex;
                return true;
            }
            return false;
        }

        private void ScrollView_Scrolled(object sender, ScrolledEventArgs e)
        {
            var y = e.ScrollY;
            OnScrolled(y);
        }

        private async void Refresh_Clicked(object sender, EventArgs e)
        {
            if (IsLoading)
            {
                return;
            }
            await ScrollToTopAsync(scrollView);
            startIndex = -1;
            nextIndex = 0;
            illustIds = null;
            StartLoad(true);
        }
    }
}
