using Elwig.Helpers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace Elwig.Models {
    [Table("member"), PrimaryKey("MgNr")]
    public class Member {
        [Column("mgnr")]
        public int MgNr { get; set; }

        [Column("predecessor_mgnr")]
        public int? PredecessorMgNr { get; set; }

        [Column("prefix")]
        public string? Prefix { get; set; }

        [Column("given_name")]
        public string GivenName { get; set; }

        [Column("middle_names")]
        public string? MiddleName { get; set; }

        [NotMapped]
        public string[] MiddleNames {
            get { return (MiddleName != null) ? MiddleName.Split(" ") : Array.Empty<string>(); }
            set { MiddleName = (value.Length > 0) ? string.Join(" ", value) : null; }
        }

        [Column("family_name")]
        public string FamilyName { get; set; }

        [Column("suffix")]
        public string? Suffix { get; set; }

        public string Name =>
            (Prefix != null ? Prefix + " " : "") +
            GivenName + " " +
            (MiddleName != null ? MiddleName + " " : "") +
            FamilyName +
            (Suffix != null ? " " + Suffix : "");

        public string ShortName => GivenName + " " + FamilyName;

        public string AdministrativeName => AdministrativeName1 + " " + AdministrativeName2;

        public string AdministrativeName1 => FamilyName.ToUpper();

        public string AdministrativeName2 =>
            (Prefix != null ? Prefix + " " : "") +
            GivenName +
            (MiddleName != null ? " " + MiddleName : "") +
            (Suffix != null ? " " + Suffix : "");

        [Column("birthday")]
        public string? Birthday { get; set; }

        [Column("entry_date")]
        public string? EntryDateString { get; set; }

        [NotMapped]
        public DateOnly? EntryDate {
            get {
                return EntryDateString != null ? DateOnly.ParseExact(EntryDateString, "yyyy-MM-dd") : null;
            }
            set {
                EntryDateString = value?.ToString("yyyy-MM-dd");
            }
        }

        [Column("exit_date")]
        public string? ExitDateString { get; set; }

        [NotMapped]
        public DateOnly? ExitDate {
            get {
                return ExitDateString != null ? DateOnly.ParseExact(ExitDateString, "yyyy-MM-dd") : null;
            }
            set {
                ExitDateString = value?.ToString("yyyy-MM-dd");
            }
        }

        [Column("business_shares")]
        public int BusinessShares { get; set; }

        [Column("accounting_nr")]
        public string? AccountingNr { get; set; }

        [Column("zwstid")]
        public string? ZwstId { get; set; }

        [Column("lfbis_nr")]
        public string? LfbisNr { get; set; }

        [Column("ustid")]
        public string? UstId { get; set; }

        [Column("volllieferant")]
        public bool IsVollLieferant { get; set; }

        [Column("buchführend")]
        public bool IsBuchführend { get; set; }

        [Column("funktionär")]
        public bool IsFunktionär { get; set; }

        [Column("active")]
        public bool IsActive { get; set; }

        [Column("iban")]
        public string? Iban { get; set; }

        [Column("bic")]
        public string? Bic { get; set; }

        [Column("country")]
        public string CountryCode { get; set; }

        [Column("postal_dest")]
        public string PostalDestId { get; set; }

        [Column("address")]
        public string Address { get; set; }

        [Column("email")]
        public string? Email { get; set; }

        [Column("phone_landline")]
        public string? PhoneLandline { get; set; }

        [Column("phone_mobile_1")]
        public string? PhoneMobile1 { get; set; }

        [Column("phone_mobile_2")]
        public string? PhoneMobile2 { get; set; }

        [Column("default_kgnr")]
        public int? DefaultKgNr { get; set; }

        [Column("contact_postal")]
        public bool ContactViaPost { get; set; }

        [Column("contact_email")]
        public bool ContactViaEmail { get; set; }

        [Column("comment")]
        public string? Comment { get; set; }

        [ForeignKey("PredecessorMgNr")]
        public virtual Member? Predecessor { get; private set; }

        [ForeignKey("CountryCode")]
        public virtual Country Country { get; private set; }

        [ForeignKey("CountryCode, PostalDestId")]
        public virtual PostalDest PostalDest { get; private set; }

        [ForeignKey("DefaultKgNr")]
        public virtual AT_Kg? DefaultKg { get; private set; }

        [ForeignKey("ZwstId")]
        public virtual Branch? Branch { get; private set; }

        [InverseProperty("Member")]
        public virtual ISet<AreaCom> AreaCommitments { get; private set; }

        [NotMapped]
        public IEnumerable<AreaCom> ActiveAreaCommitments => AreaCommitments
            .Where(c => c.YearFrom <= Utils.CurrentSeason && (c.YearTo ?? int.MaxValue) >= Utils.CurrentSeason);

        [InverseProperty("Member")]
        public virtual BillingAddr? BillingAddress { get; private set; }

        [InverseProperty("Member")]
        public virtual ISet<Delivery> Deliveries { get; private set; }

        public string FullAddress => $"{Address}, {PostalDest.AtPlz.Plz} {PostalDest.AtPlz.Ort.Name}";

        public int SearchScore(IEnumerable<string> keywords) {
            keywords = keywords.Where(s => s.Length >= 2 || (s.Length > 0 && s.All(c => char.IsAsciiDigit(c))));
            if (!keywords.Any())
                return 0;

            string?[] check = new string?[] {
                MgNr.ToString(),
                FamilyName.ToLower(), MiddleName?.ToLower(), GivenName.ToLower(),
                BillingAddress?.Name.ToLower(),
                Comment?.ToLower(),
            };

            int i = 0;
            foreach (string? c in check) {
                if (c == null) continue;
                var parts = c.Split(" ");
                if (keywords.Any(f => c == f)) {
                    i += 100;
                } else if (keywords.Any(f => parts.Any(a => a == f))) {
                    i += 90;
                } else if (keywords.Any(f => parts.Any(a => a.StartsWith(f)))) {
                    i += 50;
                } else if (keywords.Any(f => f != null && c.Contains(f))) {
                    i += 1;
                }
            }
            return i;
        }
    }
}