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

namespace Pixiview.Illust
{
    public partial class RecommendsPage : IllustDataCollectionPage
    {
        public static readonly BindableProperty SegmentIndexProperty = BindableProperty.Create(
            nameof(SegmentIndex), typeof(int), typeof(RecommendsPage), propertyChanged: OnSegmentIndexPropertyChanged);

        private static void OnSegmentIndexPropertyChanged(BindableObject obj, object oldValue, object newValue)
        {
            var page = (RecommendsPage)obj;
            Task.Run(() => page.DoLoadIllusts());
        }

        public int SegmentIndex
        {
            get => (int)GetValue(SegmentIndexProperty);
            set => SetValue(SegmentIndexProperty, value);
        }

        public RecommendsPage()
        {
            totalBarOffset = new Thickness(0, AppShell.TotalBarOffset.Top + 40, 0, 0);
            navigationBarOffset = new Thickness(0, AppShell.NavigationBarOffset.Top + 40, 0, 0);

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

        public override void OnOrientationChanged(Orientation orientation)
        {
            int columns;
            switch (orientation)
            {
                case Orientation.Portrait:
                    columns = 2;
                    PageTopMargin = totalBarOffset;
                    break;
                case Orientation.PortraitUpsideDown:
                    columns = isPhone ? 4 : 2;
                    PageTopMargin = isPhone ? navigationBarOffset : totalBarOffset;
                    break;
                case Orientation.Unknown:
                case Orientation.LandscapeLeft:
                case Orientation.LandscapeRight:
                default:
                    columns = 4;
                    PageTopMargin = navigationBarOffset;
                    break;
            }
            if (Columns != columns)
            {
                App.DebugPrint($"ranking page, change columns to {columns}");
                Columns = columns;
            }
        }

        protected override IEnumerable<IllustItem> DoGetIllustList(IllustData data, ICommand command)
        {
            if (SegmentIndex == 1)
            {
                // by user
                return data.body.page.recommendUser.SelectMany(i => i.illustIds)
                    .Select(id =>
                    {
                        var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == id)?.ConvertToItem();
                        if (item != null)
                        {
                            item.IllustTapped = command;
                        }
                        return item;
                    });
            }
            else
            {
                // recommends
                return data.body.page.recommend.Select(id =>
                {
                    var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == id)?.ConvertToItem();
                    if (item != null)
                    {
                        item.IllustTapped = command;
                    }
                    return item;
                });
            }
        }

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

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