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;
using Elwig.Models.Dtos;
using Elwig.Helpers.Export;
using Microsoft.Win32;

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

        protected bool TransferPredecessorAreaComs = false;
        protected bool CancelAreaComs = false;
        protected 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 CtrlL = new("CtrlL", typeof(MemberAdminWindow), [new KeyGesture(Key.L, ModifierKeys.Control)]);
        private readonly RoutedCommand CtrlP = new("CtrlP", typeof(MemberAdminWindow), [new KeyGesture(Key.P, ModifierKeys.Control)]);
        private readonly RoutedCommand CtrlO = new("CtrlO", typeof(MemberAdminWindow), [new KeyGesture(Key.O, ModifierKeys.Control)]);
        private readonly RoutedCommand CtrlShiftP = new("CtrlShiftP", typeof(MemberAdminWindow), [new KeyGesture(Key.P, ModifierKeys.Control | ModifierKeys.Shift)]);
        private readonly RoutedCommand CtrlShiftO = new("CtrlShiftO", typeof(MemberAdminWindow), [new KeyGesture(Key.O, ModifierKeys.Control | ModifierKeys.Shift)]);

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

        public MemberAdminWindow() {
            InitializeComponent();
            CommandBindings.Add(new CommandBinding(CtrlF, FocusSearchInput));
            CommandBindings.Add(new CommandBinding(CtrlL, Menu_List_SaveActive_Click));
            CommandBindings.Add(new CommandBinding(CtrlP, Menu_MemberDataSheet_Show_Click));
            CommandBindings.Add(new CommandBinding(CtrlO, Menu_List_ShowActive_Click));
            CommandBindings.Add(new CommandBinding(CtrlShiftP, Menu_MemberDataSheet_Print_Click));
            CommandBindings.Add(new CommandBinding(CtrlShiftO, Menu_List_PrintActive_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;

            Menu_List_OrderMgNr.IsChecked = false;
            Menu_List_OrderName.IsChecked = false;
            Menu_List_OrderOrt.IsChecked = false;
            switch (App.Client.OrderingMemberList) {
                case 0: Menu_List_OrderMgNr.IsChecked = true; break;
                case 1: Menu_List_OrderName.IsChecked = true; break;
                case 2: Menu_List_OrderOrt.IsChecked = true; break;
            }
        }

        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 {
                ControlUtils.SelectItem(MemberList, item);
            }
        }

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

        private async Task<(List<string>, IQueryable<Member>, List<string>)> GetFilters(AppDbContext ctx) {
            List<string> filterNames = [];
            IQueryable<Member> memberQuery = ctx.Members;
            if (ActiveMemberInput.IsChecked == true) {
                memberQuery = memberQuery.Where(m => m.IsActive);
                filterNames.Add("aktive Mitglieder");
            }

            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--);
                        filterNames.Add("Funktionäre");
                    } 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--);
                        filterNames.Add("Nicht-Funktionäre");
                    } else if (e.Equals("bio", StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsOrganic);
                        filter.RemoveAt(i--);
                        filterNames.Add("Bio-Betriebe");
                    } else if (e.Equals("!bio", StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => !m.IsOrganic);
                        filter.RemoveAt(i--);
                        filterNames.Add("Nicht-Bio-Betriebe");
                    } else if (e.Length >= 4 && e.Length <= 13 && "volllieferant".StartsWith(e, StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsVollLieferant);
                        filter.RemoveAt(i--);
                        filterNames.Add("Volllieferanten");
                    } 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--);
                        filterNames.Add("Nicht-Vollieferanten");
                    } else if (e.Length >= 5 && e.Length <= 11 && "buchführend".StartsWith(e, StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => m.IsBuchführend);
                        filter.RemoveAt(i--);
                        filterNames.Add("buchführend");
                    } 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--);
                        filterNames.Add("pauschaliert");
                    } else if (e.Length >= 8 && e.Length <= 12 && "pauschaliert".StartsWith(e, StringComparison.CurrentCultureIgnoreCase)) {
                        memberQuery = memberQuery.Where(m => !m.IsBuchführend);
                        filter.RemoveAt(i--);
                        filterNames.Add("pauschaliert");
                    } 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--);
                        filterNames.Add("buchführend");
                    } else if (e.All(char.IsAsciiDigit) && mgnr.ContainsKey(e)) {
                        filterMgNr.Add(int.Parse(e));
                        filter.RemoveAt(i--);
                        filterNames.Add($"MgNr. {e}");
                    } else if (kgs.TryGetValue(e, out var kg)) {
                        filterKgNr.Add(kg.KgNr);
                        filter.RemoveAt(i--);
                        filterNames.Add($"Stammgemeinde {kg.Name}");
                    } else if (e.StartsWith("zwst:")) {
                        try {
                            var branch = branches.Where(b => b.Name.StartsWith(e[5..], StringComparison.CurrentCultureIgnoreCase)).Single();
                            filterZwst.Add(branch.ZwstId);
                            filter.RemoveAt(i--);
                            filterNames.Add($"Zweigstelle {branch.Name}");
                        } 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--);
                        filterNames.Add($"Tel.-Nr. {e}");
                    } else if (areaComs.ContainsKey(e.ToUpper())) {
                        filterAreaCom.Add(e.ToUpper());
                        filter.RemoveAt(i--);
                        filterNames.Add($"Flächenbindung {e.ToUpper()}");
                    } else if (Validator.CheckLfbisNr(e)) {
                        filterLfbisNr.Add(e);
                        filter.RemoveAt(i--);
                        filterNames.Add($"Betriebsnummer {e}");
                    } else if (Validator.CheckUstIdNr(e.ToUpper())) {
                        filterUstIdNr.Add(e.ToUpper());
                        filter.RemoveAt(i--);
                        filterNames.Add($"UID {e.ToUpper()}");
                    } 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.AsQueryable().Where(Utils.ActiveAreaCommitments()).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));
            }

            return (filterNames, memberQuery, filter);
        }

        private async Task RefreshMemberListQuery(bool updateSort = false) {
            using var ctx = new AppDbContext();
            var (_, memberQuery, filter) = await GetFilters(ctx);
            var members = await 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)
                .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,
                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(AppDbContext ctx) {
            await base.OnRenewContext(ctx);
            ControlUtils.RenewItemsSource(BranchInput, await ctx.Branches.OrderBy(b => b.Name).ToListAsync());
            ControlUtils.RenewItemsSource(DefaultKgInput, await ctx.WbKgs.Select(k => k.AtKg).OrderBy(k => k.Name).ToListAsync());

            Menu_DeliveryConfirmation.Items.Clear();
            foreach (var s in await ctx.Seasons.OrderByDescending(s => s.Year).ToListAsync()) {
                var i = new MenuItem { Header = $"Saison {s.Year}...", IsEnabled = MemberList.SelectedItem != null };
                var show = new MenuItem { Header = "...anzeigen (PDF)" };
                show.Click += Menu_DeliveryConfirmation_Show_Click;
                i.Items.Add(show);
                var pdf = new MenuItem { Header = "...speichern... (PDF)" };
                pdf.Click += Menu_DeliveryConfirmation_Email_Click;
                i.Items.Add(pdf);
                var print = new MenuItem { Header = "...drucken" };
                print.Click += Menu_DeliveryConfirmation_Print_Click;
                i.Items.Add(print);
                Menu_DeliveryConfirmation.Items.Add(i);
                var email = new MenuItem { Header = "...per E-Mail schicken" };
                email.Click += Menu_DeliveryConfirmation_Email_Click;
                i.Items.Add(email);
            }

            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();
        }

        protected override void ShortcutNew() {
            if (!NewMemberButton.IsEnabled || NewMemberButton.Visibility != Visibility.Visible)
                return;
            NewMemberButton_Click(null, null);
        }

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

        protected override void ShortcutEdit() {
            if (!EditMemberButton.IsEnabled || EditMemberButton.Visibility != Visibility.Visible)
                return;
            EditMemberButton_Click(null, null);
        }

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

            IsEditing = true;
            MemberList.IsEnabled = false;
            TransferPredecessorAreaComs = false;
            CancelAreaComs = false;

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

        protected override void ShortcutDelete() {
            if (!DeleteMemberButton.IsEnabled || DeleteMemberButton.Visibility != Visibility.Visible)
                return;
            DeleteMemberButton_Click(null, null);
        }

        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.OKCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel);
            if (r == MessageBoxResult.OK) {
                try {
                    using (var ctx = new AppDbContext()) {
                        ctx.Remove(m);
                        await ctx.SaveChangesAsync();
                    }
                    await App.HintContextChange();
                } catch (Exception exc) {
                    MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        protected override void ShortcutSave() {
            if (!SaveButton.IsEnabled || SaveButton.Visibility != Visibility.Visible)
                return;
            SaveButton_Click(null, null);
        }

        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);
        }

        protected override void ShortcutReset() {
            if (!ResetButton.IsEnabled || ResetButton.Visibility != Visibility.Visible)
                return;
            ResetButton_Click(null, null);
        }

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

        private void CancelButton_Click(object sender, RoutedEventArgs evt) {
            IsEditing = false;
            IsCreating = false;
            MemberList.IsEnabled = true;
            TransferPredecessorAreaComs = false;
            CancelAreaComs = false;
            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_Contact_Email_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_Contact_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) {
                    await doc.Print();
                } else {
                    doc.Show();
                }
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Mouse.OverrideCursor = null;
        }

        private async void Menu_MemberDataSheet_Show_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            await GenerateMemberDataSheet(m, ExportMode.Show);
        }

        private async void Menu_MemberDataSheet_SavePdf_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            await GenerateMemberDataSheet(m, ExportMode.SavePdf);
        }

        private async void Menu_MemberDataSheet_Print_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            await GenerateMemberDataSheet(m, ExportMode.Print);
        }

        private async void Menu_MemberDataSheet_Email_Click(object sender, RoutedEventArgs evt) {
            if (MemberList.SelectedItem is not Member m)
                return;
            await GenerateMemberDataSheet(m, ExportMode.Email);
        }

        private static async Task GenerateMemberDataSheet(Member m, ExportMode mode) {
            Mouse.OverrideCursor = Cursors.AppStarting;
            try {
                using var ctx = new AppDbContext();
                using var doc = new MemberDataSheet(m, ctx);
                await Utils.ExportDocument(doc, mode, emailData: (m, MemberDataSheet.Name, "Im Anhang finden Sie das aktuelle Stammdatenblatt"));
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Mouse.OverrideCursor = null;
        }

        private async void Menu_DeliveryConfirmation_Show_Click(object sender, RoutedEventArgs evt) {
            var season = ((sender as MenuItem)?.Parent as MenuItem)?.Header?.ToString()?.Split(' ')[^1].Split('.')[0];
            if (MemberList.SelectedItem is not Member m || season == null || !int.TryParse(season, out var s))
                return;
            await GenerateDeliveryConfirmation(m, s, ExportMode.Show);
        }

        private async void Menu_DeliveryConfirmation_SavePdf_Click(object sender, RoutedEventArgs evt) {
            var season = ((sender as MenuItem)?.Parent as MenuItem)?.Header?.ToString()?.Split(' ')[^1].Split('.')[0];
            if (MemberList.SelectedItem is not Member m || season == null || !int.TryParse(season, out var s))
                return;
            await GenerateDeliveryConfirmation(m, s, ExportMode.SavePdf);
        }

        private async void Menu_DeliveryConfirmation_Print_Click(object sender, RoutedEventArgs evt) {
            var season = ((sender as MenuItem)?.Parent as MenuItem)?.Header?.ToString()?.Split(' ')[^1].Split('.')[0];
            if (MemberList.SelectedItem is not Member m || season == null || !int.TryParse(season, out var s))
                return;
            await GenerateDeliveryConfirmation(m, s, ExportMode.Print);
        }

        private async void Menu_DeliveryConfirmation_Email_Click(object sender, RoutedEventArgs evt) {
            var season = ((sender as MenuItem)?.Parent as MenuItem)?.Header?.ToString()?.Split(' ')[^1].Split('.')[0];
            if (MemberList.SelectedItem is not Member m || season == null || !int.TryParse(season, out var s))
                return;
            await GenerateDeliveryConfirmation(m, s, ExportMode.Email);
        }

        private static async Task GenerateDeliveryConfirmation(Member m, int year, ExportMode mode) {
            Mouse.OverrideCursor = Cursors.AppStarting;
            try {
                using var ctx = new AppDbContext();
                var data = await DeliveryConfirmationDeliveryData.ForMember(ctx.DeliveryParts, year, m);
                using var doc = new DeliveryConfirmation(ctx, year, m, data);
                await Utils.ExportDocument(doc, mode, emailData: (m, $"{DeliveryConfirmation.Name} {year}", $"Im Anhang finden Sie die Anlieferungsbestätigung {year}"));
            } catch (Exception exc) {
                MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Mouse.OverrideCursor = null;
        }

        private async void Menu_List_SaveActive_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(0, ExportMode.SaveList); }
        private async void Menu_List_ShowActive_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(0, ExportMode.Show); }
        private async void Menu_List_SavePdfActive_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(0, ExportMode.SavePdf); }
        private async void Menu_List_PrintActive_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(0, ExportMode.Print); }
        private async void Menu_List_SaveFilters_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(1, ExportMode.SaveList); }
        private async void Menu_List_ShowFilters_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(1, ExportMode.Show); }
        private async void Menu_List_SavePdfFilters_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(1, ExportMode.SavePdf); }
        private async void Menu_List_PrintFilters_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(1, ExportMode.Print); }
        private async void Menu_List_SaveAll_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(2, ExportMode.SaveList); }
        private async void Menu_List_ShowAll_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(2, ExportMode.Show); }
        private async void Menu_List_SavePdfAll_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(2, ExportMode.SavePdf); }
        private async void Menu_List_PrintAll_Click(object sender, RoutedEventArgs evt) { await GenerateMemberList(2, ExportMode.Print); }

        private async void Menu_List_Order_Click(object sender, RoutedEventArgs evt) {
            Menu_List.IsSubmenuOpen = true;
            if (sender == Menu_List_OrderMgNr) {
                App.Client.OrderingMemberList = 0;
                Menu_List_OrderMgNr.IsChecked = true;
                Menu_List_OrderName.IsChecked = false;
                Menu_List_OrderOrt.IsChecked = false;
            } else if (sender == Menu_List_OrderName) {
                App.Client.OrderingMemberList = 1;
                Menu_List_OrderMgNr.IsChecked = false;
                Menu_List_OrderName.IsChecked = true;
                Menu_List_OrderOrt.IsChecked = false;
            } else if (sender == Menu_List_OrderOrt) {
                App.Client.OrderingMemberList = 2;
                Menu_List_OrderMgNr.IsChecked = false;
                Menu_List_OrderName.IsChecked = false;
                Menu_List_OrderOrt.IsChecked = true;
            }
            await App.Client.UpdateValues();
        }

        private async Task GenerateMemberList(int modeWho, ExportMode exportMode) {
            using var ctx = new AppDbContext();
            IQueryable<Member> query;
            List<string> filterNames = [];
            if (modeWho == 0) {
                query = ctx.Members.Where(m => m.IsActive);
                filterNames.Add("aktive Mitglieder");
            } else if (modeWho == 1) {
                var (f, q, _) = await GetFilters(ctx);
                query = q;
                filterNames.AddRange(f);
            } else {
                query = ctx.Members;
            }

            if (Menu_List_OrderMgNr.IsChecked) {
                query = query
                    .OrderBy(m => m.Branch!.Name)
                    .ThenBy(m => m.MgNr);
            } else if (Menu_List_OrderName.IsChecked) {
                query = query
                    .OrderBy(m => m.Branch!.Name)
                    .ThenBy(m => m.FamilyName)
                    .ThenBy(m => m.GivenName)
                    .ThenBy(m => m.MgNr);
            } else if (Menu_List_OrderOrt.IsChecked) {
                query = query
                    .OrderBy(m => m.Branch!.Name)
                    .ThenBy(m => m.DefaultWbKg!.AtKg.Name)
                    .ThenBy(m => m.FamilyName)
                    .ThenBy(m => m.GivenName)
                    .ThenBy(m => m.MgNr);
            }

            if (exportMode == ExportMode.SaveList) {
                var d = new SaveFileDialog() {
                    FileName = $"{Documents.MemberList.Name}.ods",
                    DefaultExt = "ods",
                    Filter = "OpenDocument Format Spreadsheet (*.ods)|*.ods",
                    Title = $"{Documents.MemberList.Name} speichern unter - Elwig"
                };
                if (d.ShowDialog() == true) {
                    Mouse.OverrideCursor = Cursors.AppStarting;
                    try {
                        var data = await MemberListData.FromQuery(query, filterNames);
                        using var ods = new OdsFile(d.FileName);
                        await ods.AddTable(data);
                    } catch (Exception exc) {
                        MessageBox.Show(exc.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    Mouse.OverrideCursor = null;
                }
            } else {
                Mouse.OverrideCursor = Cursors.AppStarting;
                try {
                    var data = await MemberListData.FromQuery(query, filterNames);
                    using var doc = new MemberList(string.Join(" / ", filterNames), data);
                    await Utils.ExportDocument(doc, exportMode);
                } 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 (TransferPredecessorAreaComs && m.PredecessorMgNr is int predecessor) {
                var year = Utils.FollowingSeason;
                var areaComs = await ctx.AreaCommitments
                    .Where(c => c.MgNr == predecessor && (c.YearTo == null || c.YearTo >= year))
                    .ToListAsync();

                var fbNr = await ctx.NextFbNr();
                ctx.AddRange(areaComs.Select((c, i) => new AreaCom {
                    FbNr = fbNr + i,
                    MgNr = m.MgNr,
                    VtrgId = c.VtrgId,
                    CultId = c.CultId,
                    Area = c.Area,
                    KgNr = c.KgNr,
                    GstNr = c.GstNr,
                    RdNr = c.RdNr,
                    YearFrom = year,
                    YearTo = c.YearTo,
                }));

                foreach (var ac in areaComs)
                    ac.YearTo = year - 1;
                ctx.UpdateRange(areaComs);
                await ctx.SaveChangesAsync();
            }
            TransferPredecessorAreaComs = false;

            if (CancelAreaComs) {
                var year = Utils.FollowingSeason;
                var areaComs = await ctx.AreaCommitments
                    .Where(c => c.MgNr == m.MgNr && (c.YearTo == null || c.YearTo >= year))
                    .ToListAsync();

                foreach (var ac in areaComs)
                    ac.YearTo = year - 1;
                ctx.UpdateRange(areaComs);
                await ctx.SaveChangesAsync();
            }
            CancelAreaComs = false;

            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.SelectItem(OrtInput, p);
            } 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.SelectItem(BillingOrtInput, b);
                }
            } 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.SelectItemWithPk(BranchInput, m.ZwstId);
            ControlUtils.SelectItemWithPk(DefaultKgInput, 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_Contact_Email.IsEnabled = m.EmailAddresses.Count > 0;
            Menu_Contact_Letterhead.IsEnabled = true;
            Menu_MemberDataSheet_Print.IsEnabled = true;
            Menu_MemberDataSheet_Email.IsEnabled = App.Config.Smtp != null && m.EmailAddresses.Count > 0;
            Menu_MemberDataSheet_Show.IsEnabled = true;
            Menu_MemberDataSheet_SavePdf.IsEnabled = true;
            foreach (var i in Menu_DeliveryConfirmation.Items.Cast<MenuItem>()) {
                i.IsEnabled = true;
                (i.Items[^1] as MenuItem)!.IsEnabled = App.Config.Smtp != null && m.EmailAddresses.Count > 0;
            }

            FinishInputFilling();
        }

        new protected void ClearInputs(bool validate = false) {
            Menu_Contact_Email.IsEnabled = false;
            Menu_Contact_Letterhead.IsEnabled = false;
            Menu_MemberDataSheet_Print.IsEnabled = false;
            Menu_MemberDataSheet_Email.IsEnabled = false;
            Menu_MemberDataSheet_Show.IsEnabled = false;
            Menu_MemberDataSheet_SavePdf.IsEnabled = false;
            foreach (var i in Menu_DeliveryConfirmation.Items.Cast<MenuItem>()) {
                i.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 async void PredecessorMgNrInput_LostFocus(object sender, RoutedEventArgs evt) {
            var valid = InputLostFocus((TextBox)sender, Validator.CheckPredecessorMgNr);
            if (valid && PredecessorMgNrInput.Text != "" && (IsEditing || IsCreating)) {
                var mgnr = int.Parse(PredecessorMgNrInput.Text);
                if (MemberList.SelectedItem is Member m && m.MgNr == mgnr)
                    return;
                using var ctx = new AppDbContext();
                var areaComs = await ctx.AreaCommitments
                    .Where(c => c.MgNr == mgnr && (c.YearTo == null || c.YearTo >= Utils.FollowingSeason))
                    .ToListAsync();
                if (areaComs.Count == 0)
                    return;
                var res = MessageBox.Show("Sollen die aktiven Flächenbindungen des angegebenen\n" +
                    $"Vorgängers übernommen werden? ({areaComs.Sum(c => c.Area)} m²)\n\n" +
                    "Die Änderungen werden erst beim Speichern übernommen!",
                    "Aktive Flächenbindungen übernehmen", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                if (res != MessageBoxResult.Yes)
                    return;
                TransferPredecessorAreaComs = true;
                SetOriginalValue(PredecessorMgNrInput, -1);  // hack to allow user to save
                UpdateButtons();
            }
        }

        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);
        }

        private async void ActiveInput_Changed(object sender, RoutedEventArgs evt) {
            if ((IsEditing || IsCreating) && ActiveInput.IsChecked == false && int.TryParse(MgNrInput.Text, out var mgnr)) {
                using var ctx = new AppDbContext();
                var areaComs = await ctx.AreaCommitments
                    .Where(c => c.MgNr == mgnr && (c.YearTo == null || c.YearTo >= Utils.FollowingSeason))
                    .ToListAsync();
                if (areaComs.Count >= 0) {
                    var res = MessageBox.Show($"Sollen die aktiven Flächenbindungen gekündigt werden? ({areaComs.Sum(c => c.Area)} m²)\n" +
                        "Falls die Flächenbindungen später an ein neues Mitglied\n" +
                        "übertragen werden sollen bitte \"Nein\" auswählen!\n\n" +
                        "Die Änderungen werden erst beim Speichern übernommen!",
                        "Flächenbindungen kündigen", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                    CancelAreaComs = res == MessageBoxResult.Yes;
                }
            }
            CheckBox_Changed(sender, evt);
        }
    }
}
