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

namespace Billing.Views
{
    public partial class AddAccountPage : BillingPage
    {
        private static readonly BindableProperty AccountNameProperty = Helper.Create<string, AddAccountPage>(nameof(AccountName));
        private static readonly BindableProperty AccountIconProperty = Helper.Create<string, AddAccountPage>(nameof(AccountIcon));
        private static readonly BindableProperty CategoryProperty = Helper.Create<AccountCategory, AddAccountPage>(nameof(Category));
        private static readonly BindableProperty InitialProperty = Helper.Create<string, AddAccountPage>(nameof(Initial));
        private static readonly BindableProperty MemoProperty = Helper.Create<string, AddAccountPage>(nameof(Memo));

        public string AccountName
        {
            get => (string)GetValue(AccountNameProperty);
            set => SetValue(AccountNameProperty, value);
        }
        public string AccountIcon
        {
            get => (string)GetValue(AccountIconProperty);
            set => SetValue(AccountIconProperty, value);
        }
        public AccountCategory Category
        {
            get => (AccountCategory)GetValue(CategoryProperty);
            set => SetValue(CategoryProperty, value);
        }
        public string Initial
        {
            get => (string)GetValue(InitialProperty);
            set => SetValue(InitialProperty, value);
        }
        public string Memo
        {
            get => (string)GetValue(MemoProperty);
            set => SetValue(MemoProperty, value);
        }

        private readonly Account account;

        public Command CheckAccount { get; }
        public Command SelectIcon { get; }
        public Command SelectCategory { get; }

        public event EventHandler<AccountEventArgs> AccountChecked;

        public AddAccountPage(Account account = null)
        {
            CheckAccount = new Command(OnCheckAccount);
            SelectIcon = new Command(OnSelectIcon);
            SelectCategory = new Command(OnSelectCategory);
            Title = Resource.EditAccount;

            this.account = account;
            if (account == null)
            {
                AccountIcon = Definition.DefaultIcon;
                Category = AccountCategory.Cash;
            }
            else
            {
                AccountName = account.Name;
                AccountIcon = account.Icon;
                Category = account.Category;
                Initial = account.Initial.ToString();
                Memo = account.Memo;
            }
            InitializeComponent();
        }

        protected override void OnLoaded()
        {
            editorName.SetFocus();
        }

        private async void OnCheckAccount()
        {
            if (Tap.IsBusy)
            {
                return;
            }
            using (Tap.Start())
            {
                if (string.IsNullOrWhiteSpace(AccountName))
                {
                    await this.ShowMessage(Resource.AccountRequired);
                    return;
                }
                await Navigation.PopAsync();
                _ = decimal.TryParse(Initial, out decimal initial);
                if (account != null)
                {
                    account.Name = AccountName;
                    account.Icon = AccountIcon;
                    account.Category = Category;
                    account.Initial = initial;
                    account.Memo = Memo;
                }
                AccountChecked?.Invoke(this, new AccountEventArgs
                {
                    Account = account ?? new Account
                    {
                        Name = AccountName,
                        Icon = AccountIcon,
                        Category = Category,
                        Initial = initial,
                        Balance = initial,
                        Memo = Memo
                    }
                });
            }
        }

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

        private void Account_IconChecked(object sender, string icon)
        {
            AccountIcon = icon;
        }

        private async void OnSelectCategory()
        {
            if (Tap.IsBusy)
            {
                return;
            }
            using (Tap.Start())
            {
                var source = new List<SelectItem<AccountCategory>>
                {
                    new() { Icon = "dollar", Value = AccountCategory.Cash, Name = Resource.Cash },
                    new() { Icon = "creditcard", Value = AccountCategory.CreditCard, Name = Resource.CreditCard },
                    new() { Icon = "debitcard", Value = AccountCategory.DebitCard, Name = Resource.DebitCard },
                    new() { Icon = "online", Value = AccountCategory.ElecAccount, Name = Resource.ElecAccount }
                };
                var page = new ItemSelectPage<SelectItem<AccountCategory>>(source);
                page.ItemTapped += Category_ItemTapped;
                await Navigation.PushAsync(page);
            }
        }

        private void Category_ItemTapped(object sender, SelectItem<AccountCategory> e)
        {
            Category = e.Value;
        }
    }

    public class AccountEventArgs : EventArgs
    {
        public Account Account { get; set; }
    }
}