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

namespace Pixiview.Illust
{
    public partial class RankingPage : IllustDataCollectionPage
    {
        public static readonly BindableProperty KeywordsProperty = BindableProperty.Create(
            nameof(Keywords), typeof(string), typeof(RankingPage));

        public string Keywords
        {
            get => (string)GetValue(KeywordsProperty);
            set => SetValue(KeywordsProperty, value);
        }

        public RankingPage()
        {
            totalBarOffset = new Thickness(0, AppShell.TotalBarOffset.Top + 50, 0, 0);
            navigationBarOffset = new Thickness(0, AppShell.NavigationBarOffset.Top + 50, 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)
        {
            return data.body.page.ranking.items.Select(i =>
            {
                var item = data.body.thumbnails.illust.FirstOrDefault(l => l.illustId == i.id)?.ConvertToItem();
                if (item != null)
                {
                    item.IllustTapped = command;
                }
                return item;
            });
        }

        protected override IllustData DoLoadIllustData(bool force)
        {
            var data = Stores.LoadIllustData(force);
            if (data != null)
            {
                var date = data.body.page.ranking.date;
                if (date.Length == 8)
                {
                    date = date.Substring(0, 4) + "-" + date.Substring(4, 2) + "-" + date.Substring(6, 2);
                }
                Device.BeginInvokeOnMainThread(() => Title = date);
            }
            return data;
        }

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

        private void SearchBar_SearchButtonPressed(object sender, EventArgs e)
        {
            var key = Keywords;
            if (key != null)
            {
                key = key.Trim().ToLower();
            }

            if (string.IsNullOrEmpty(key))
            {
                Illusts = IllustCollection;
            }
            else
            {
                var list = IllustCollection.Where(i =>
                    (i.UserName != null && i.UserName.ToLower().Contains(key)) ||
                    (i.Title != null && i.Title.ToLower().Contains(key)));
                Illusts = new IllustCollection(list);
            }
        }

        private void SearchBar_Unfocused(object sender, FocusEventArgs e)
        {
            if (!e.IsFocused)
            {
                SearchBar_SearchButtonPressed(sender, e);
            }
        }
    }
}
