using CommunityToolkit.Mvvm.ComponentModel;
using Elwig.Helpers;
using Elwig.Models.Entities;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Elwig.ViewModels {
    public partial class MemberAdminViewModel : ObservableObject {

        public int? TransferPredecessorAreaComs = null;
        public bool MaintainAreaComYearFrom = false;
        public int? CancelAreaComs = null;

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

        [ObservableProperty]
        private string? _searchQuery = "";
        public List<string> TextFilter => [.. SearchQuery?.ToLower().Split(' ').ToList().FindAll(e => e.Length > 0) ?? []];

        [ObservableProperty]
        private bool _showOnlyActiveMembers;

        [ObservableProperty]
        private Member? _selectedMember;
        [ObservableProperty]
        private IEnumerable<Member> _members = [];

        [ObservableProperty]
        private bool _isMemberSelected;
        [ObservableProperty]
        private bool _memberHasEmail;
        [ObservableProperty]
        private bool _memberCanSendEmail;
        [ObservableProperty]
        private bool _enableMemberReferenceButton;
        [ObservableProperty]
        private bool _enableSearchInputs = true;
        [ObservableProperty]
        private IEnumerable<bool> _memberHasDeliveries = [.. Enumerable.Range(0, 9999).Select(i => false)];

        [ObservableProperty]
        private bool _memberListOrderByMgNr;
        [ObservableProperty]
        private bool _memberListOrderByName;
        [ObservableProperty]
        private bool _memberListOrderByOrt;

        [ObservableProperty]
        private string? _mgNrString;
        public int? MgNr {
            get => int.TryParse(MgNrString, out var mgnr) ? mgnr : null;
            set => MgNrString = $"{value}";
        }
        [ObservableProperty]
        private string? _predecessorMgNrString;
        public int? PredecessorMgNr {
            get => int.TryParse(PredecessorMgNrString, out var mgnr) ? mgnr : null;
            set => PredecessorMgNrString = $"{value}";
        }
        [ObservableProperty]
        private bool _isJuridicalPerson;
        [ObservableProperty]
        private string? _prefix;
        [ObservableProperty]
        private string? _givenName;
        [ObservableProperty]
        private string? _name;
        [ObservableProperty]
        private string? _suffix;
        [ObservableProperty]
        private string? _forTheAttentionOf;
        [ObservableProperty]
        private string? _birthday;
        [ObservableProperty]
        private string? _age;
        [ObservableProperty]
        private bool _isDeceased;
        [ObservableProperty]
        private string? _address;
        [ObservableProperty]
        private string? _plzString;
        public int? Plz {
            get => int.TryParse(PlzString, out var plz) ? plz : null;
            set => PlzString = $"{value}";
        }
        [ObservableProperty]
        private AT_PlzDest? _ort;
        [ObservableProperty]
        private IEnumerable<AT_PlzDest> _ortSource = [];

        [ObservableProperty]
        private string? _billingName;
        [ObservableProperty]
        private string? _billingAddress;
        [ObservableProperty]
        private string? _billingPlzString;
        public int? BillingPlz {
            get => int.TryParse(BillingPlzString, out var plz) ? plz : null;
            set => BillingPlzString = $"{value}";
        }
        [ObservableProperty]
        private AT_PlzDest? _billingOrt;
        [ObservableProperty]
        private IEnumerable<AT_PlzDest> _billingOrtSource = [];

        [ObservableProperty]
        private string? _iban;
        [ObservableProperty]
        private string? _bic;

        [ObservableProperty]
        private string? _ustIdNr;
        [ObservableProperty]
        private string? _lfbisNr;
        [ObservableProperty]
        private bool _isBuchführend;
        [ObservableProperty]
        private bool _isOrganic;

        [ObservableProperty]
        private string? _entryDate;
        [ObservableProperty]
        private string? _exitDate;
        [ObservableProperty]
        private string? _businessSharesString;
        public int? BusinessShares {
            get => int.TryParse(BusinessSharesString, out var bs) ? bs : null;
            set => BusinessSharesString = $"{value}";
        }
        [ObservableProperty]
        private string? _accountingNr;
        [ObservableProperty]
        private bool _isActive;
        [ObservableProperty]
        private bool _isVollLieferant;
        [ObservableProperty]
        private bool _isFunktionär;
        [ObservableProperty]
        private Branch? _branch;
        [ObservableProperty]
        private IEnumerable<Branch> _branchSource = [];
        [ObservableProperty]
        private AT_Kg? _defaultKg;
        [ObservableProperty]
        private IEnumerable<AT_Kg> _defaultKgSource = [];
        [ObservableProperty]
        private string? _comment;
        [ObservableProperty]
        private bool _contactViaPost;
        [ObservableProperty]
        private bool _contactViaEmail;

        [ObservableProperty]
        private string _createdAt = "-";
        [ObservableProperty]
        private string _modifiedAt = "-";
        [ObservableProperty]
        private string _modifiedAtShort = "-";
        [ObservableProperty]
        private string _exportedAt = "-";
        [ObservableProperty]
        private string _importedAt = "-";

        public ObservableCollection<string?> EmailAddresses { get; private set; } = [null, null, null, null, null, null, null, null, null];

        public partial class PhoneNr(int? type = null, string? number = null, string? comment = null) : ObservableObject {
            [ObservableProperty]
            public int _type = type ?? -1;
            [ObservableProperty]
            public string? _number = number;
            [ObservableProperty]
            public string? _comment = comment;

            public override bool Equals(object? obj) {
                return obj is PhoneNr nr && Type == nr.Type && Number == nr.Number && Comment == nr.Comment;
            }

            public override int GetHashCode() {
                return Type ^ (Number?.GetHashCode() ?? 0) ^ (Comment?.GetHashCode() ?? 0);
            }
        }
        public ObservableCollection<PhoneNr> PhoneNrs { get; private set; } = [new(), new(), new(), new(), new(), new(), new(), new(), new()];

        [ObservableProperty]
        private string _statusMembers = "-";
        [ObservableProperty]
        private string? _statusMembersToolTip;
        [ObservableProperty]
        private string _statusBusinessShares = "-";
        [ObservableProperty]
        private string? _statusBusinessSharesToolTip;
        [ObservableProperty]
        private string _statusDeliveriesLastSeason = "-";
        [ObservableProperty]
        private string _statusDeliveriesLastSeasonInfo = "letzte Saison";
        [ObservableProperty]
        private Grid? _statusDeliveriesLastSeasonToolTip;
        [ObservableProperty]
        private string _statusDeliveriesThisSeason = "-";
        [ObservableProperty]
        private string _statusDeliveriesThisSeasonInfo = "aktuelle Saison";
        [ObservableProperty]
        private Grid? _statusDeliveriesThisSeasonToolTip;
        [ObservableProperty]
        private string _statusAreaCommitment = "-";
        [ObservableProperty]
        private Grid? _statusAreaCommitmentToolTip;
        [ObservableProperty]
        private string _statusAreaCommitmentInfo = "aktuelle Saison";

        [ObservableProperty]
        private Visibility _controlButtonsVisibility = Visibility.Visible;
        [ObservableProperty]
        private Visibility _editingButtonsVisibility = Visibility.Hidden;
        [ObservableProperty]
        private Visibility _personalNameVisibility = Visibility.Visible;
        [ObservableProperty]
        private Visibility _juridicalNameVisibility = Visibility.Hidden;
    }
}
