using Billing.Models;
using Billing.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;

namespace Billing.Views
{
    public partial class IconSelectPage : BillingPage
    {
        public static readonly BindableProperty IconsSourceProperty = BindableProperty.Create(nameof(IconsSource), typeof(IList<BillingIcon>), typeof(IconSelectPage));

        public IList<BillingIcon> IconsSource
        {
            get => (IList<BillingIcon>)GetValue(IconsSourceProperty);
            set => SetValue(IconsSourceProperty, value);
        }

        public Command IconCheck { get; }

        public event EventHandler<string> IconChecked;

        private string iconChecked;

        public IconSelectPage(string icon)
        {
            iconChecked = icon;
            IconCheck = new Command(OnIconCheck);

            InitializeComponent();

            InitIcons();
        }

        private void InitIcons()
        {
            var source = new List<BillingIcon>
            {
                new() { Icon = BaseModel.ICON_DEFAULT },
                new() { Icon = "wallet" },
                new() { Icon = "dollar" },
                new() { Icon = "creditcard" },
                new() { Icon = "debitcard" },
                new() { Icon = "cmb" },
                new() { Icon = "rcb" },
                new() { Icon = "yuebao" },
                new() { Icon = "zhaozhaoying" },
                new() { Icon = "clothes" },
                new() { Icon = "food" },
                new() { Icon = "drink" },
                new() { Icon = "daily" },
                new() { Icon = "trans" },
                new() { Icon = "face" },
                new() { Icon = "learn" },
                new() { Icon = "medical" },
                new() { Icon = "gem" },
                new() { Icon = "makeup" },
                new() { Icon = "brunch" },
                new() { Icon = "dinner" },
                new() { Icon = "fruit" },
                new() { Icon = "bill" },
                new() { Icon = "fee" },
                new() { Icon = "rent" },
                new() { Icon = "maintenance" },
                new() { Icon = "rail" },
                new() { Icon = "taxi" },
                new() { Icon = "fitness" },
                new() { Icon = "party" },
            };
            source.AddRange(IconConverter.IconPreset.Select(icon => new BillingIcon { Icon = $"#brand#{icon.Key}" }));
            foreach (var icon in source)
            {
                if (icon.Icon == iconChecked)
                {
                    icon.IsChecked = true;
                }
            }
            IconsSource = source;
        }

        private async void OnIconCheck(object o)
        {
            if (Tap.IsBusy)
            {
                return;
            }
            using (Tap.Start())
            {
                if (o is string icon)
                {
                    foreach (var ic in IconsSource)
                    {
                        ic.IsChecked = ic.Icon == icon;
                    }
                    iconChecked = icon;
                    IconChecked?.Invoke(this, icon);
                    await Navigation.PopAsync();
                }
            }
        }
    }

    public class BillingIcon : BindableObject
    {
        public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(BillingIcon));

        public bool IsChecked
        {
            get => (bool)GetValue(IsCheckedProperty);
            set => SetValue(IsCheckedProperty, value);
        }
        public string Icon { get; set; }
    }
}