using Billing.Languages;
using Billing.Models;
using Billing.Themes;
using Billing.UI;
using System;
using System.Linq;
using Xamarin.Forms;

namespace Billing.Views
{
    public partial class AddCategoryPage : BillingPage
    {
        private static readonly BindableProperty CategoryNameProperty = BindableProperty.Create(nameof(CategoryName), typeof(string), typeof(AddCategoryPage));
        private static readonly BindableProperty CategoryIconProperty = BindableProperty.Create(nameof(CategoryIcon), typeof(string), typeof(AddCategoryPage));
        private static readonly BindableProperty TintColorProperty = BindableProperty.Create(nameof(TintColor), typeof(Color), typeof(AddCategoryPage));
        private static readonly BindableProperty TintColorStringProperty = BindableProperty.Create(nameof(TintColorString), typeof(string), typeof(AddCategoryPage));

        public string CategoryName
        {
            get => (string)GetValue(CategoryNameProperty);
            set => SetValue(CategoryNameProperty, value);
        }
        public string CategoryIcon
        {
            get => (string)GetValue(CategoryIconProperty);
            set => SetValue(CategoryIconProperty, value);
        }
        public Color TintColor
        {
            get => (Color)GetValue(TintColorProperty);
            set => SetValue(TintColorProperty, value);
        }
        public string TintColorString
        {
            get => (string)GetValue(TintColorStringProperty);
            set => SetValue(TintColorStringProperty, value);
        }

        public Command CheckCategory { get; }
        public Command SelectIcon { get; }

        public event EventHandler<Category> CategoryChecked;

        private readonly int categoryId;
        private readonly Category parent;

        public AddCategoryPage(int id = -1, Category parent = null)
        {
            categoryId = id;
            this.parent = parent;
            var category = App.Categories.FirstOrDefault(c => c.Id == id);
            Title = category?.Name ?? Resource.AddCategory;

            if (category != null)
            {
                CategoryName = category.Name;
                CategoryIcon = category.Icon;
                if (category.TintColor == Color.Transparent ||
                    category.TintColor == default)
                {
                    TintColor = BaseTheme.CurrentPrimaryColor;
                }
                else
                {
                    TintColor = category.TintColor;
                }
            }
            else
            {
                TintColor = BaseTheme.CurrentPrimaryColor;
            }
            TintColorString = Helper.WrapColorString(TintColor.ToHex());

            CheckCategory = new Command(OnCheckCategory);
            SelectIcon = new Command(OnSelectIcon);

            InitializeComponent();
        }

        private bool focused;

        public override void OnLoaded()
        {
            if (!focused)
            {
                focused = true;
                editorName.SetFocus();
            }
        }

        private void ColorPicker_ColorChanged(object sender, Color e)
        {
            TintColor = e;
            TintColorString = Helper.WrapColorString(e.ToHex());
        }

        private async void OnCheckCategory()
        {
            if (Tap.IsBusy)
            {
                return;
            }
            using (Tap.Start())
            {
                var currentColor = BaseTheme.CurrentPrimaryColor;
                var tintColor = TintColor;
                var category = App.Categories.FirstOrDefault(c => c.Id == categoryId);
                if (category == null)
                {
                    CategoryChecked?.Invoke(this, new Category
                    {
                        Id = -1,
                        Name = CategoryName,
                        Icon = CategoryIcon,
                        TintColor = tintColor == currentColor ? Color.Transparent : tintColor,
                        ParentId = parent?.Id,
                        Type = parent?.Type ?? CategoryType.Spending
                    });
                }
                else
                {
                    category.Name = CategoryName;
                    category.Icon = CategoryIcon;
                    category.TintColor = tintColor == currentColor ? Color.Transparent : tintColor;
                    CategoryChecked?.Invoke(this, category);
                }
                await Navigation.PopAsync();
            }
        }

        private async void OnSelectIcon()
        {
            if (Tap.IsBusy)
            {
                return;
            }
            using (Tap.Start())
            {
                var page = new IconSelectPage(CategoryIcon);
                page.IconChecked += Category_IconChecked;
                await Navigation.PushAsync(page);
            }
        }

        private void Category_IconChecked(object sender, string icon)
        {
            CategoryIcon = icon;
        }
    }
}