using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Elwig.Helpers;
using Elwig.Models.Entities;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Elwig.Documents;
using System.Diagnostics;

namespace Elwig.Windows {
    public partial class MemberAdminWindow : AdministrationWindow {

        private List<string> TextFilter = [];
        private readonly (ComboBox Type, TextBox Number, TextBox Comment)[] PhoneNrInputs;
        private readonly (Label Label, TextBox Address)[] EmailAddressInputs;

        private readonly RoutedCommand CtrlF = new("CtrlF", typeof(MemberAdminWindow), [new KeyGesture(Key.F, ModifierKeys.Control)]);
        private readonly RoutedCommand CtrlP = new("CtrlP", typeof(MemberAdminWindow), [new KeyGesture(Key.P, ModifierKeys.Control)]);
        private readonly RoutedCommand CtrlShiftP = new("CtrlShiftP", typeof(MemberAdminWindow), [new KeyGesture(Key.P, ModifierKeys.Control | ModifierKeys.Shift)]);

        private static ObservableCollection<KeyValuePair<string, string>> PhoneNrTypes { get; set; } = new(Utils.PhoneNrTypes);

        public MemberAdminWindow() {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(CtrlF, FocusSearchInput));
            CommandBindings.Add(new CommandBinding(CtrlP, Menu_Show_MemberDataSheet_Click));
            CommandBindings.Add(new CommandBinding(CtrlShiftP, Menu_Print_MemberDataSheet_Click));
            ExemptInputs = [
                SearchInput, ActiveMemberInput, MemberList,
            ];
            RequiredInputs = [
                MgNrInput, GivenNameInput, FamilyNameInput,
                AddressInput, PlzInput, OrtInput, BillingOrtInput,
                BusinessSharesInput, BranchInput, DefaultKgInput
            ];
            EmailAddressInputs = [
                (EmailAddress1Label, EmailAddress1Input),
                (EmailAddress2Label, EmailAddress2Input),
                (EmailAddress3Label, EmailAddress3Input),
                (EmailAddress4Label, EmailAddress4Input),
                (EmailAddress5Label, EmailAddress5Input),
                (EmailAddress6Label, EmailAddress6Input),
                (EmailAddress7Label, EmailAddress7Input),
                (EmailAddress8Label, EmailAddress8Input),
                (EmailAddress9Label, EmailAddress9Input),
            ];
            PhoneNrInputs = [
                (PhoneNr1TypeInput, PhoneNr1Input, PhoneNr1CommentInput),
                (PhoneNr2TypeInput, PhoneNr2Input, PhoneNr2CommentInput),
                (PhoneNr3TypeInput, PhoneNr3Input, PhoneNr3CommentInput),
                (PhoneNr4TypeInput, PhoneNr4Input, PhoneNr4CommentInput),
                (PhoneNr5TypeInput, PhoneNr5Input, PhoneNr5CommentInput),
                (PhoneNr6TypeInput, PhoneNr6Input, PhoneNr6CommentInput),
                (PhoneNr7TypeInput, PhoneNr7Input, PhoneNr7CommentInput),
                (PhoneNr8TypeInput, PhoneNr8Input, PhoneNr8CommentInput),
                (PhoneNr9TypeInput, PhoneNr9Input, PhoneNr9CommentInput),
            ];
            foreach (var input in PhoneNrInputs) input.Type.ItemsSource = PhoneNrTypes;

            InitializeDelayTimer(SearchInput, SearchInput_TextChanged);
            SearchInput.TextChanged -= SearchInput_TextChanged;
        }

        private void Window_Loaded(object sender, RoutedEventArgs evt) {
            ActiveMemberInput.IsChecked = true;
            UpdateContactInfoVisibility();
            LockInputs();
        }

        public void FocusMember(int mgnr) {
            if (IsEditing || IsCreating) return;
            var item = MemberList.ItemsSource.Cast<Member>().FirstOrDefault(m => m.MgNr == mgnr);
            if (item == null) {
                using var ctx = new AppDbContext();
                var m = ctx.Members.Find(mgnr);
                if (m == null) return;
                if (SearchInput.Text.Length > 0) {
                    SearchInput.Text = "";
                }
                if (!m.IsActive) {
                    ActiveMemberInput.IsChecked = false;
                }
                App.MainDispatcher.BeginInvoke(async () => {
                    await Task.Delay(500);
                    FocusMember(mgnr);
                });
            } else {
                MemberList.SelectedItem = item;
                MemberList.ScrollIntoView(MemberList.SelectedItem);
            }
        }

        private async Task RefreshMemberList() {
            await RefreshMemberListQuery();
        }

        private async Task RefreshMemberListQuery(bool updateSort = false) {
            using var ctx = new AppDbContext();
            IQueryable<Member> memberQuery = ctx.Members;
            if (ActiveMemberInput.IsChecked == true) memberQuery = memberQuery.Where(m => m.IsActive);

            var filterMgNr = new List<int>();
            var filterZwst = new List<string>();
            var filterKgNr = new List<int>();
            var filterLfbisNr = new List<string>();
            var filterUstIdNr = new List<string>();
            var filterAreaCom = new List<string>();

            var filter = TextFilter.ToList();
            if (filter.Count > 0) {
                var branches = await ctx.Branches.ToListAsync();
                var mgnr = await ctx.Members.ToDictionaryAsync(m => m.MgNr.ToString(), m => m);
                var kgs = await ctx.WbKgs.ToDictionaryAsync(k => k.AtKg.Name.ToLower(), k => k.AtKg);
                var areaComs = await ctx.AreaCommitmentTypes.ToDictionaryAsync(t => $"{t.SortId}{t.AttrId}", t => t);

                for (int i = 0; i < filter.Count; i++) {
                    var e = filter[i];

                    if (e.Length >= 5 && e.Length <= 10 && "funktionär".StartsWith(e, StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsFunktionär);
                        filter.RemoveAt(i--);
                    } else if (e.Length >= 6 && e.Length <= 11 && e[0] == '!' && "funktionär".StartsWith(e[1..], StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => !m.IsFunktionär);
                        filter.RemoveAt(i--);
                    } else if (e.Equals("bio", StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsOrganic);
                        filter.RemoveAt(i--);
                    } else if (e.Equals("!bio", StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => !m.IsOrganic);
                        filter.RemoveAt(i--);
                    } else if (e.Length >= 4 && e.Length <= 13 && "volllieferant".StartsWith(e, StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsVollLieferant);
                        filter.RemoveAt(i--);
                    } else if (e.Length >= 5 && e.Length <= 14 && e[0] == '!' && "volllieferant".StartsWith(e[1..], StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => !m.IsVollLieferant);
                        filter.RemoveAt(i--);
                    } else if (e.Length >= 5 && e.Length <= 11 && "buchführend".StartsWith(e, StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsBuchführend);
                        filter.RemoveAt(i--);
                    } else if (e.Length >= 6 && e.Length <= 12 && e[0] == '!' && "buchführend".StartsWith(e[1..], StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => !m.IsBuchführend);
                        filter.RemoveAt(i--);
                    } else if (e.Length >= 8 && e.Length <= 12 && "pauschaliert".StartsWith(e, StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => !m.IsBuchführend);
                        filter.RemoveAt(i--);
                    } else if (e.Length >= 9 && e.Length <= 13 && e[0] == '!' && "pauschaliert".StartsWith(e[1..], StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsBuchführend);
                        filter.RemoveAt(i--);
                    } else if (e.All(char.IsAsciiDigit) && mgnr.ContainsKey(e)) {
                        filterMgNr.Add(int.Parse(e));
                        filter.RemoveAt(i--);
                    } else if (kgs.TryGetValue(e, out var kg)) {
                        filterKgNr.Add(kg.KgNr);
                        filter.RemoveAt(i--);
                    } else if (e.StartsWith("zwst:")) {
                        try {
                            filterZwst.Add(branches.Where(b => b.Name.StartsWith(e[5..], StringComparison.CurrentCultureIgnoreCase)).Single().ZwstId);
                            filter.RemoveAt(i--);
                        } catch (InvalidOperationException) { }
                    } else if (e.StartsWith('+') && e[1..].All(char.IsAsciiDigit)) {
                        memberQuery = memberQuery.Where(m => m.TelephoneNumbers.Any(t => t.Number.Replace(" ", "").StartsWith(e)));
                        filter.RemoveAt(i--);
                    } else if (areaComs.ContainsKey(e.ToUpper())) {
                        filterAreaCom.Add(e.ToUpper());
                        filter.RemoveAt(i--);
                    } else if (Validator.CheckLfbisNr(e)) {
                        filterLfbisNr.Add(e);
                        filter.RemoveAt(i--);
                    } else if (Validator.CheckUstIdNr(e.ToUpper())) {
                        filterUstIdNr.Add(e.ToUpper());
                        filter.RemoveAt(i--);
                    } else if (e.Length > 2 && e.StartsWith('"') && e.EndsWith('"')) {
                        filter[i] = e[1..^1];
                    } else if (e.Length < 2) {
                        filter.RemoveAt(i--);
                    }
                }

                if (filterMgNr.Count > 0) memberQuery = memberQuery.Where(m => filterMgNr.Contains(m.MgNr));
                if (filterKgNr.Count > 0) memberQuery = memberQuery.Where(m => m.DefaultKgNr != null && filterKgNr.Contains((int)m.DefaultKgNr));
                if (filterZwst.Count > 0) memberQuery = memberQuery.Where(m => m.ZwstId != null && filterZwst.Contains(m.ZwstId));
                if (filterAreaCom.Count > 0) memberQuery = memberQuery.Where(m => m.AreaCommitments.Where(c => c.YearFrom <= Utils.CurrentYear && (c.YearTo ?? int.MaxValue) >= Utils.CurrentYear).Any(c => filterAreaCom.Contains(c.VtrgId)));
                if (filterLfbisNr.Count > 0) memberQuery = memberQuery.Where(m => m.LfbisNr != null && filterLfbisNr.Contains(m.LfbisNr));
                if (filterUstIdNr.Count > 0) memberQuery = memberQuery.Where(m => m.UstIdNr != null && filterUstIdNr.Contains(m.UstIdNr));
            }

            memberQuery = memberQuery
                .Include(m => m.Branch)
                .Include(m => m.DefaultWbKg!.AtKg)
                .Include(m => m.EmailAddresses)
                .Include(m => m.TelephoneNumbers)
                .Include(m => m.PostalDest.AtPlz!.Ort)
                .Include(m => m.PostalDest.AtPlz!.Country)
                .Include(m => m.BillingAddress!.PostalDest.AtPlz!.Ort)
                .Include(m => m.BillingAddress!.PostalDest.AtPlz!.Country);
            List<Member> members = await memberQuery.ToListAsync();

            if (filter.Count > 0 && members.Count > 0) {
                var dict = members.AsParallel()
                    .ToDictionary(m => m, m => m.SearchScore(filter))
                    .OrderByDescending(a => a.Value)
                    .ThenBy(a => a.Key.FamilyName)
                    .ThenBy(a => a.Key.GivenName);
                var threshold = dict.Select(a => a.Value).Max() * 3 / 4;
                members = dict
                    .Where(a => a.Value > threshold)
                    .Select(a => a.Key)
                    .ToList();
            } else {
                members = members
                    .OrderBy(m => m.FamilyName)
                    .ThenBy(m => m.GivenName)
                    .ToList();
            }

            ControlUtils.RenewItemsSource(MemberList, members, i => (i as Member)?.MgNr,
                MemberList_SelectionChanged, TextFilter.Count > 0 ? ControlUtils.RenewSourceDefault.IfOnly : ControlUtils.RenewSourceDefault.None, !updateSort);
        }

        private void RefreshInputs(bool validate = false) {
            ClearInputStates();
            if (MemberList.SelectedItem is Member m) {
                EditMemberButton.IsEnabled = true;
                DeleteMemberButton.IsEnabled = true;
                AreaCommitmentButton.IsEnabled = true;
                DeliveryButton.IsEnabled = true;
                OrganicButton.IsEnabled = true;
                FillInputs(m);
            } else {
                EditMemberButton.IsEnabled = false;
                DeleteMemberButton.IsEnabled = false;
                AreaCommitmentButton.IsEnabled = false;
                DeliveryButton.IsEnabled = false;
                OrganicButton.IsEnabled = false;
                ClearOriginalValues();
                ClearDefaultValues();
                ClearInputs(validate);
                ClearInputStates();
            }
            GC.Collect();
        }

        private async void InitInputs() {
            ClearOriginalValues();
            ClearDefaultValues();

            using (var ctx = new AppDbContext()) {
                MgNrInput.Text = (await ctx.NextMgNr()).ToString();
                EntryDateInput.Text = DateTime.Now.ToString("dd.MM.yyyy");
                if (await ctx.Branches.CountAsync() == 1)
                    BranchInput.SelectedIndex = 0;
                ActiveInput.IsChecked = true;
                ContactPostalInput.IsChecked = true;
                MemberReferenceButton.IsEnabled = false;
            }

            SetDefaultValue(MgNrInput);
            SetDefaultValue(EntryDateInput);
            SetDefaultValue(ActiveInput);
            ValidateRequiredInputs();
        }

        protected override async Task OnRenewContext() {
            await base.OnRenewContext();
            using var ctx = new AppDbContext();
            ControlUtils.RenewItemsSource(BranchInput, await ctx.Branches.OrderBy(b => b.Name).ToListAsync(), i => (i as Branch)?.ZwstId);
            ControlUtils.RenewItemsSource(DefaultKgInput, await ctx.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync(), i => (i as AT_Kg)?.KgNr);
            await RefreshMemberList();
            StatusMembers.Text = $"Mitglieder: {await ctx.Members.CountAsync(m => m.IsActive):N0} ({await ctx.Members.CountAsync():N0})";
            StatusBusinessShares.Text = $"Geschäftsanteile: {await ctx.Members.Where(m => m.IsActive).SumAsync(m => m.BusinessShares):N0} ({await ctx.Members.SumAsync(m => m.BusinessShares):N0})";
        }

        private void SetPhoneNrInput(int nr, string? type, string? number, string? comment) {
            var inputs = PhoneNrInputs[nr];
            inputs.Type.SelectedItem = (type == null) ? null : inputs.Type.ItemsSource.Cast<KeyValuePair<string, string>>().FirstOrDefault(p => p.Key == type);
            inputs.Number.Text = number;
            inputs.Comment.Text = comment;
        }

        private void SetEmailAddressInput(int nr, string? address) {
            var inputs = EmailAddressInputs[nr];
            inputs.Address.Text = address;
        }

        private (string Type, string Number, string? Comment)? GetPhoneNrInput(int nr) {
            var inputs = PhoneNrInputs[nr];
            var number = inputs.Number.Text;
            if (string.IsNullOrEmpty(number))
                return null;
            var type = (inputs.Type.SelectedItem as KeyValuePair<string, string>?)?.Key ?? (number.StartsWith("+43 ") && number[4] == '6' ? "mobile" : "landline");
            var comment = inputs.Comment.Text;
            return (type, number, comment == "" ? null : comment);
        }

        private string? GetEmailAddressInput(int nr) {
            var inputs = EmailAddressInputs[nr];
            return inputs.Address.Text == "" ? null : inputs.Address.Text;
        }

        private void SetPhoneNrInputVisible(int nr, bool visible, int? position = null) {
            var inputs = PhoneNrInputs[nr];
            if (position is int p) {
                var mt = 10 + p * 30;
                inputs.Type.Margin = new(6, mt, 5, 0);
                inputs.Number.Margin = new(0, mt, 5, 0);
                inputs.Comment.Margin = new(0, mt, 10, 0);
            }
            var vis = visible ? Visibility.Visible : Visibility.Hidden;
            inputs.Type.Visibility = vis;
            inputs.Number.Visibility = vis;
            inputs.Comment.Visibility = vis;
        }

        private void SetEmailAddressInputVisible(int nr, bool visible, int? position = null) {
            var inputs = EmailAddressInputs[nr];
            if (position is int p) {
                var mt = 10 + p * 30;
                inputs.Label.Margin = new(10, mt, 0, 0);
                inputs.Address.Margin = new(0, mt, 10, 0);
            }
            var vis = visible ? Visibility.Visible : Visibility.Hidden;
            inputs.Label.Visibility = vis;
            inputs.Address.Visibility = vis;
        }

        private void MemberList_SelectionChanged(object sender, RoutedEventArgs evt) {
            RefreshInputs();
        }

        private async void ActiveMemberInput_Changed(object sender, RoutedEventArgs evt) {
            await RefreshMemberListQuery();
        }

        private void NewMemberButton_Click(object sender, RoutedEventArgs evt) {
            IsCreating = true;
            MemberList.IsEnabled = false;
            MemberList.SelectedItem = null;
            HideNewEditDeleteButtons();
            ShowSaveResetCancelButtons();
            UnlockInputs();
            UpdateContactInfoVisibility(true);
            InitInputs();
            LockSearchInputs();
        }

        private void EditMemberButton_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem == null)
                return;

            IsEditing = true;
            MemberList.IsEnabled = false;

            HideNewEditDeleteButtons();
            ShowSaveResetCancelButtons();
            UnlockInputs();
            UpdateContactInfoVisibility(true);
            LockSearchInputs();
        }

        private async void DeleteMemberButton_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;

            var r = MessageBox.Show(
                $"Soll das Mitglied \"{m.AdministrativeName}\" (MgNr. {m.MgNr}) wirklich unwiderruflich gelöscht werden?\n" +
                $"Sämtliche Lieferungen und Flächenbindungen dieses Mitglieds werden auch gelöscht!",
                "Mitglied löschen", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
            if (r == MessageBoxResult.Yes) {
                using var ctx = new AppDbContext();
                ctx.Remove(m);
                await ctx.SaveChangesAsync();
                await RefreshMemberList();
            }
        }

        private async void SaveButton_Click(object sender, RoutedEventArgs evt) {
            int? mgnr = null;
            try {
                mgnr = await UpdateMember((MemberList.SelectedItem as Member)?.MgNr);
            } catch (Exception exc) {
                var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message;
                if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
                MessageBox.Show(str, "Mitglied aktualisieren", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            IsEditing = false;
            IsCreating = false;
            MemberList.IsEnabled = true;
            HideSaveResetCancelButtons();
            ShowNewEditDeleteButtons();
            LockInputs();
            UpdateContactInfoVisibility();
            UnlockSearchInputs();
            FinishInputFilling();
            await RefreshMemberList();
            RefreshInputs();
            SearchInput.Text = "";
            if (mgnr is int m)
                FocusMember(m);
        }

        private void ResetButton_Click(object sender, RoutedEventArgs evt) {
            if (IsEditing) {
                RefreshInputs();
            } else if (IsCreating) {
                ClearInputs();
                InitInputs();
            }
            UpdateButtons();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs evt) {
            IsEditing = false;
            IsCreating = false;
            MemberList.IsEnabled = true;
            HideSaveResetCancelButtons();
            ShowNewEditDeleteButtons();
            RefreshInputs();
            LockInputs();
            UpdateContactInfoVisibility();
            UnlockSearchInputs();
        }

        private void AreaCommitmentButton_Click(object sender, RoutedEventArgs evt) {
            App.FocusMemberAreaComs(((Member)MemberList.SelectedItem).MgNr);
        }

        private void DeliveryButton_Click(object sender, RoutedEventArgs evt) {
            App.FocusMemberDeliveries(((Member)MemberList.SelectedItem).MgNr);
        }

        private async void SearchInput_TextChanged(object sender, RoutedEventArgs evt) {
            TextFilter = SearchInput.Text.ToLower().Split(" ").ToList().FindAll(e => e.Length > 0);
            await RefreshMemberListQuery(true);
        }

        private void Menu_Member_SendEmail_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            Utils.MailTo(m.EmailAddresses.Select(a => a.Address));
        }

        private async void Menu_Print_Letterhead_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            Mouse.OverrideCursor = Cursors.AppStarting;
            try {
                using var doc = new Letterhead(m);
                await doc.Generate();
                if (App.Config.Debug) {
                    doc.Show();
                } else {
                    await doc.Print();
                }
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Mouse.OverrideCursor = null;
        }

        private async void Menu_Print_MemberDataSheet_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            Mouse.OverrideCursor = Cursors.AppStarting;
            try {
                using var ctx = new AppDbContext();
                using var doc = new MemberDataSheet(m, ctx);
                await doc.Generate();
                if (App.Config.Debug) {
                    doc.Show();
                } else {
                    await doc.Print();
                }
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Mouse.OverrideCursor = null;
        }

        private async void Menu_Show_MemberDataSheet_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            Mouse.OverrideCursor = Cursors.AppStarting;
            try {
                using var ctx = new AppDbContext();
                using var doc = new MemberDataSheet(m, ctx);
                await doc.Generate();
                doc.Show();
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Mouse.OverrideCursor = null;
        }

        private void FocusSearchInput(object sender, RoutedEventArgs evt) {
            if (!IsEditing && !IsCreating) {
                SearchInput.Focus();
                SearchInput.SelectAll();
            }
        }

        private void ShowSaveResetCancelButtons() {
            SaveButton.IsEnabled = false;
            ResetButton.IsEnabled = false;
            CancelButton.IsEnabled = true;
            SaveButton.Visibility = Visibility.Visible;
            ResetButton.Visibility = Visibility.Visible;
            CancelButton.Visibility = Visibility.Visible;
        }

        private void HideSaveResetCancelButtons() {
            SaveButton.IsEnabled = false;
            ResetButton.IsEnabled = false;
            CancelButton.IsEnabled = false;
            SaveButton.Visibility = Visibility.Hidden;
            ResetButton.Visibility = Visibility.Hidden;
            CancelButton.Visibility = Visibility.Hidden;
        }

        private void ShowNewEditDeleteButtons() {
            NewMemberButton.IsEnabled = true;
            EditMemberButton.IsEnabled = MemberList.SelectedItem != null;
            DeleteMemberButton.IsEnabled = MemberList.SelectedItem != null;
            NewMemberButton.Visibility = Visibility.Visible;
            EditMemberButton.Visibility = Visibility.Visible;
            DeleteMemberButton.Visibility = Visibility.Visible;
        }

        private void HideNewEditDeleteButtons() {
            NewMemberButton.IsEnabled = false;
            EditMemberButton.IsEnabled = false;
            DeleteMemberButton.IsEnabled = false;
            NewMemberButton.Visibility = Visibility.Hidden;
            EditMemberButton.Visibility = Visibility.Hidden;
            DeleteMemberButton.Visibility = Visibility.Hidden;
        }

        private void LockSearchInputs() {
            SearchInput.IsEnabled = false;
            ActiveMemberInput.IsEnabled = false;
        }

        private void UnlockSearchInputs() {
            SearchInput.IsEnabled = true;
            ActiveMemberInput.IsEnabled = true;
        }

        private void UpdateContactInfoVisibility(bool extra = false) {
            var m = MemberList.SelectedItem as Member;
            bool lastVisible;
            int num = 0;
            lastVisible = true;
            for (int i = 0; i < EmailAddressInputs.Length; i++) {
                var input = EmailAddressInputs[i];
                var vis = !string.IsNullOrEmpty(input.Address.Text) || (m?.EmailAddresses.Any(a => a.Nr - 1 == i) ?? false);
                var cVis = vis || (extra && lastVisible);
                SetEmailAddressInputVisible(i, cVis, cVis ? num++ : null);
                lastVisible = vis;
            }
            lastVisible = true;
            for (int i = 0; i < PhoneNrInputs.Length; i++) {
                var input = PhoneNrInputs[i];
                var vis = !string.IsNullOrEmpty(input.Number.Text) || (m?.TelephoneNumbers.Any(n => n.Nr - 1 == i) ?? false);
                var cVis = vis || (extra && lastVisible);
                SetPhoneNrInputVisible(i, cVis, cVis ? num++ : null);
                lastVisible = vis;
            }
        }

        private async Task<int> UpdateMember(int? oldMgNr) {
            using var ctx = new AppDbContext();
            var newMgNr = int.Parse(MgNrInput.Text);
            var m = new Member {
                MgNr = oldMgNr ?? newMgNr,
                PredecessorMgNr = (PredecessorMgNrInput.Text == "") ? null : int.Parse(PredecessorMgNrInput.Text),
                Prefix = (PrefixInput.Text == "") ? null : PrefixInput.Text,
                GivenName = GivenNameInput.Text,
                FamilyName = FamilyNameInput.Text,
                Suffix = (SuffixInput.Text == "") ? null : SuffixInput.Text,
                Birthday = (BirthdayInput.Text == "") ? null : string.Join("-", BirthdayInput.Text.Split(".").Reverse()),
                IsDeceased = DeceasedInput.IsChecked ?? false,
                CountryNum = 40,  // Austria AT AUT
                PostalDestId = ((AT_PlzDest)OrtInput.SelectedItem).Id,
                Address = AddressInput.Text,

                Iban = (IbanInput.Text == "") ? null : IbanInput.Text.Replace(" ", ""),
                Bic = (BicInput.Text == "") ? null : BicInput.Text,

                UstIdNr = (UstIdNrInput.Text == "") ? null : UstIdNrInput.Text,
                LfbisNr = (LfbisNrInput.Text == "") ? null : LfbisNrInput.Text,
                IsBuchführend = BuchführendInput.IsChecked ?? false,
                IsOrganic = OrganicInput.IsChecked ?? false,

                EntryDateString = (EntryDateInput.Text == "") ? null : string.Join("-", EntryDateInput.Text.Split(".").Reverse()),
                ExitDateString = (ExitDateInput.Text == "") ? null : string.Join("-", ExitDateInput.Text.Split(".").Reverse()),
                BusinessShares = (BusinessSharesInput.Text == "") ? 0 : int.Parse(BusinessSharesInput.Text),
                AccountingNr = (AccountingNrInput.Text == "") ? null : AccountingNrInput.Text,
                IsActive = ActiveInput.IsChecked ?? false,
                IsVollLieferant = VollLieferantInput.IsChecked ?? false,
                IsFunktionär = FunkionärInput.IsChecked ?? false,
                ZwstId = ((Branch)BranchInput.SelectedItem).ZwstId,
                DefaultKgNr = ((AT_Kg)DefaultKgInput.SelectedItem).KgNr,
                Comment = (CommentInput.Text == "") ? null : CommentInput.Text,
                ContactViaPost = ContactPostalInput.IsChecked ?? false,
                ContactViaEmail = ContactEmailInput.IsChecked ?? false,
            };

            if (oldMgNr != null) {
                ctx.Update(m);
            } else {
                ctx.Add(m);
            }

            ctx.RemoveRange(ctx.BillingAddresses.Where(a => a.MgNr == oldMgNr));
            if (BillingOrtInput.SelectedItem != null) {
                var p = (AT_PlzDest)BillingOrtInput.SelectedItem;
                ctx.Add(new BillingAddr {
                    MgNr = m.MgNr,
                    Name = BillingNameInput.Text,
                    Address = BillingAddressInput.Text,
                    CountryNum = p.CountryNum,
                    PostalDestId = p.Id,
                });
            }

            ctx.RemoveRange(ctx.MemberTelephoneNrs.Where(t => t.MgNr == oldMgNr));
            ctx.AddRange(Enumerable.Range(0, PhoneNrInputs.Length)
                .Select(GetPhoneNrInput)
                .Where(input => input != null)
                .Select(input => input!.Value)
                .Select((input, i) => new MemberTelNr {
                    MgNr = m.MgNr,
                    Nr = i + 1,
                    Type = input.Type,
                    Number = input.Number,
                    Comment = input.Comment,
                }));

            ctx.RemoveRange(ctx.MemberEmailAddrs.Where(e => e.MgNr == oldMgNr));
            ctx.AddRange(Enumerable.Range(0, EmailAddressInputs.Length)
                .Select(GetEmailAddressInput)
                .Where(input => input != null && input != "")
                .Select((input, i) => new MemberEmailAddr {
                    MgNr = m.MgNr,
                    Nr = i + 1,
                    Address = input!,
                    Comment = null,
                }));

            await ctx.SaveChangesAsync();

            if (newMgNr != m.MgNr) {
                await ctx.Database.ExecuteSqlAsync($"UPDATE member SET mgnr = {newMgNr} WHERE mgnr = {oldMgNr}");
            }

            await App.HintContextChange();

            return newMgNr;
        }

        private void FillInputs(Member m) {
            ClearOriginalValues();
            ClearDefaultValues();

            MgNrInput.Text = m.MgNr.ToString();
            PredecessorMgNrInput.Text = m.PredecessorMgNr.ToString();
            MemberReferenceButton.IsEnabled = m.PredecessorMgNr != null;
            PrefixInput.Text = m.Prefix;
            GivenNameInput.Text = m.GivenName;
            FamilyNameInput.Text = m.FamilyName;
            SuffixInput.Text = m.Suffix;
            BirthdayInput.Text = (m.Birthday != null) ? string.Join(".", m.Birthday.Split("-").Reverse()) : null;
            if (m.Birthday?.Length == 10) {
                Age.Text = Utils.GetAge(DateOnly.ParseExact(m.Birthday, "yyyy-MM-dd")).ToString();
            } else if (m.Birthday != null) {
                Age.Text = "ca. " + (DateTime.Now.Year - int.Parse(m.Birthday[^4..])).ToString();
            } else {
                Age.Text = "-";
            }
            DeceasedInput.IsChecked = m.IsDeceased;
            AddressInput.Text = m.Address;
            AT_PlzDest? p = m.PostalDest.AtPlz;
            if (p != null) {
                PlzInput.Text = p.Plz.ToString();
                ControlUtils.SelectComboBoxItem(OrtInput, o => (o as AT_PlzDest)?.Okz, p.Okz);
            } else {
                PlzInput.Text = null;
                OrtInput.SelectedItem = null;
            }

            var emailAddrs = m.EmailAddresses.OrderBy(a => a.Nr).ToList();
            for (int i = 0; i< EmailAddressInputs.Length; i++) {
                if (i < emailAddrs.Count) {
                    var emailAddr = emailAddrs[i];
                    SetEmailAddressInput(i, emailAddr.Address);
                } else {
                    SetEmailAddressInput(i, null);
                }
            }

            var phoneNrs = m.TelephoneNumbers.OrderBy(p => p.Nr).ToList();
            for (int i = 0; i < PhoneNrInputs.Length; i++) {
                if (i < phoneNrs.Count) {
                    var phoneNr = phoneNrs[i];
                    SetPhoneNrInput(i, phoneNr.Type, phoneNr.Number, phoneNr.Comment);
                } else {
                    SetPhoneNrInput(i, null, null, null);
                }
            }
            UpdateContactInfoVisibility(IsEditing || IsCreating);

            IbanInput.Text = m.Iban;
            BicInput.Text = m.Bic;

            UstIdNrInput.Text = m.UstIdNr;
            LfbisNrInput.Text = m.LfbisNr;
            BuchführendInput.IsChecked = m.IsBuchführend;
            OrganicInput.IsChecked = m.IsOrganic;

            var billingAddr = m.BillingAddress;
            if (billingAddr != null) {
                BillingNameInput.Text = billingAddr.Name;
                BillingAddressInput.Text = billingAddr.Address;
                AT_PlzDest? b = billingAddr.PostalDest.AtPlz;
                if (b != null) {
                    BillingPlzInput.Text = b.Plz.ToString();
                    ControlUtils.SelectComboBoxItem(BillingOrtInput, o => (o as AT_PlzDest)?.Okz, b.Okz);
                }
            } else {
                BillingNameInput.Text = "";
                BillingAddressInput.Text = "";
                BillingPlzInput.Text = "";
                BillingOrtInput.SelectedItem = null;
            }

            EntryDateInput.Text = (m.EntryDateString != null) ? string.Join(".", m.EntryDateString.Split("-").Reverse()) : null;
            ExitDateInput.Text = (m.ExitDateString != null) ? string.Join(".", m.ExitDateString.Split("-").Reverse()) : null;
            BusinessSharesInput.Text = m.BusinessShares.ToString();
            AccountingNrInput.Text = m.AccountingNr;
            ControlUtils.SelectComboBoxItem(BranchInput, b => (b as Branch)?.ZwstId, m.ZwstId);
            ControlUtils.SelectComboBoxItem(DefaultKgInput, k => (k as AT_Kg)?.KgNr, m.DefaultKgNr);
            CommentInput.Text = m.Comment;
            ActiveInput.IsChecked = m.IsActive;
            VollLieferantInput.IsChecked = m.IsVollLieferant;
            FunkionärInput.IsChecked = m.IsFunktionär;
            ContactPostalInput.IsChecked = m.ContactViaPost;
            ContactEmailInput.IsChecked = m.ContactViaEmail;

            using (var ctx = new AppDbContext()) {
                var d1 = ctx.Deliveries.Where(d => d.Year == Utils.CurrentLastSeason && d.MgNr == m.MgNr);
                var d2 = ctx.Deliveries.Where(d => d.Year == Utils.CurrentLastSeason - 1 && d.MgNr == m.MgNr);
                StatusDeliveriesLastSeason.Text = $"Lieferungen ({Utils.CurrentLastSeason - 1}): {d2.Count():N0} ({d2.Sum(d => d.Parts.Count):N0}), {d2.SelectMany(d => d.Parts).Sum(p => p.Weight):N0} kg";
                StatusDeliveriesThisSeason.Text = $"Lieferungen ({Utils.CurrentLastSeason}): {d1.Count():N0} ({d1.Sum(d => d.Parts.Count):N0}), {d1.SelectMany(d => d.Parts).Sum(p => p.Weight):N0} kg";
                StatusAreaCommitment.Text = $"Gebundene Fläche: {m.ActiveAreaCommitments(ctx).Select(c => c.Area).Sum():N0} m²";
            }

            Menu_Member_SendEmail.IsEnabled = m.EmailAddresses.Count > 0;
            Menu_Print_Letterhead.IsEnabled = true;
            Menu_Show_MemberDataSheet.IsEnabled = true;
            Menu_Print_MemberDataSheet.IsEnabled = true;

            FinishInputFilling();
        }

        new protected void ClearInputs(bool validate = false) {
            Menu_Member_SendEmail.IsEnabled = false;
            Menu_Print_Letterhead.IsEnabled = false;
            Menu_Show_MemberDataSheet.IsEnabled = false;
            Menu_Print_MemberDataSheet.IsEnabled = false;
            MemberReferenceButton.IsEnabled = false;
            StatusDeliveriesLastSeason.Text = $"Lieferungen ({Utils.CurrentLastSeason - 1}): -";
            StatusDeliveriesThisSeason.Text = $"Lieferungen ({Utils.CurrentLastSeason}): -";
            StatusAreaCommitment.Text = "Gebundene Fläche: -";
            Age.Text = "-";
            base.ClearInputs(validate);
        }

        override protected void UpdateButtons() {
            if (!IsEditing && !IsCreating) return;
            bool ch = HasChanged, v = IsValid;
            ResetButton.IsEnabled = ch;
            SaveButton.IsEnabled = v && ch;
        }

        protected void InputTextChanged(TextBox input, Func<TextBox, bool, Member?, ValidationResult> checker) {
            InputTextChanged(input, checker(input, SenderIsRequired(input), (Member)MemberList.SelectedItem));
        }

        protected void InputLostFocus(TextBox input, Func<TextBox, bool, Member?, ValidationResult> checker, string? msg = null) {
            InputLostFocus(input, checker(input, SenderIsRequired(input), (Member)MemberList.SelectedItem), msg);
        }

        private void MgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
            InputTextChanged((TextBox)sender, Validator.CheckNewMgNr);
        }

        private void MgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
            InputLostFocus((TextBox)sender, Validator.CheckNewMgNr);
        }

        private void PredecessorMgNrInput_TextChanged(object sender, RoutedEventArgs evt) {
            InputTextChanged((TextBox)sender, Validator.CheckPredecessorMgNr);
        }

        private void PredecessorMgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
            InputLostFocus((TextBox)sender, Validator.CheckPredecessorMgNr);
        }

        private new void EmailAddressInput_TextChanged(object sender, TextChangedEventArgs evt) {
            base.EmailAddressInput_TextChanged(sender, evt);
            UpdateContactInfoVisibility(IsEditing || IsCreating);
        }

        private new void PhoneNrInput_TextChanged(object sender, TextChangedEventArgs evt) {
            base.PhoneNrInput_TextChanged(sender, evt);
            UpdateContactInfoVisibility(IsEditing || IsCreating);
        }

        private void KgDetailsButton_Click(object sender, RoutedEventArgs evt) {
            if (DefaultKgInput.SelectedItem is AT_Kg kg) {
                App.FocusOriginHierarchyKg(kg.KgNr);
            } else {
                App.FocusOriginHierarchy();
            }
        }

        private void OrganicButton_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            var url = "https://www.easy-cert.com/htm/suchergebnis.htm?" +
                //$"CustomerNumber={m.LfbisNr}&" +
                $"Name={(m.BillingAddress?.Name ?? m.Name).Replace(' ', '+')}&" +
                $"PostalCode={(m.BillingAddress?.PostalDest ?? m.PostalDest).AtPlz?.Plz}";
            Process.Start(new ProcessStartInfo(url) {
                UseShellExecute = true,
            });
        }

        private void MemberReferenceButton_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m || m.PredecessorMgNr == null) return;
            FocusMember((int)m.PredecessorMgNr);
        }
    }
}
