using Elwig.Models.Entities;
using Microsoft.Data.Sqlite;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Elwig.Helpers {
    public class ClientParameters {

        public enum Type { Matzen, Winzerkeller, Weinland, Baden };

        public bool IsMatzen => Client == Type.Matzen;
        public bool IsWinzerkeller => Client == Type.Winzerkeller;
        public bool IsWeinland => Client == Type.Weinland;
        public bool IsBaden => Client == Type.Baden;
        public bool IsWolkersdorf => IsWinzerkeller && App.ZwstId == "W";
        public bool IsHaugsdorf => IsWinzerkeller && App.ZwstId == "H";
        public bool IsSitzendorf => IsWinzerkeller && App.ZwstId == "S";
        public bool IsGrInzersdorf => IsWeinland;

        public bool HasNetWeighing(string? zwstId) => IsMatzen || (IsWinzerkeller && zwstId == "W");
        public bool HasNetWeighing(Branch? b) => HasNetWeighing(b?.ZwstId);
        public bool HasNetWeighing() => HasNetWeighing(App.ZwstId);

        public string NameToken;
        public string NameShort;
        public string Name;
        public string? NameSuffix;
        public string NameType;
        public string NameTypeFull => NameType.Replace(".", "").Replace(" ", "").ToLower() switch {
            "reggenmbh" => "registrierte Genossenschaft mit beschränkter Haftung",
            _ => NameType,
        };
        public string NameFull => NameSuffix == null ? $"{Name} {NameType}" : $"{Name}, {NameSuffix}, {NameType}";
        public Type? Client;

        public PostalDest PostalDest {
            set {
                Plz = value.AtPlz!.Plz;
                Ort = value.AtPlz!.Ort.Name;
            }
        }
        public int Plz;
        public string Ort;
        public string Address;
        public string Sender1 => $"{NameShort} | {Address} | {Plz} {Ort}";
        public string Sender2;

        public string? Iban;
        public string? Bic;
        public string? UstIdNr;
        public string? LfbisNr;

        public string? PhoneNr;
        public string? FaxNr;
        public string? EmailAddress;
        public string? Website;

        public int ModeDeliveryNoteStats;

        public string? TextDeliveryNote;
        public string? TextDeliveryConfirmation;
        public string? TextCreditNote;
        public string? TextEmailSubject;
        public string? TextEmailBody;

        public ClientParameters(AppDbContext ctx) : this(ctx.ClientParameters.ToDictionary(e => e.Param, e => e.Value)) { }

        public ClientParameters(Dictionary<string, string?> parameters) {
            try {
                NameToken = parameters["CLIENT_NAME_TOKEN"] ?? throw new KeyNotFoundException();
                NameShort = parameters["CLIENT_NAME_SHORT"] ?? throw new KeyNotFoundException();
                Name = parameters["CLIENT_NAME"] ?? throw new KeyNotFoundException();
                NameSuffix = parameters.GetValueOrDefault("CLIENT_NAME_SUFFIX");
                NameType = parameters["CLIENT_NAME_TYPE"] ?? throw new KeyNotFoundException();
                switch (Name) {
                    case "Winzergenossenschaft für Matzen und Umgebung":
                        Client = Type.Matzen; break;
                    case "Winzerkeller im Weinviertel":
                        Client = Type.Winzerkeller; break;
                    case "Winzergenossenschaft Weinland":
                        Client = Type.Weinland; break;
                    case "Winzergenossenschaft Baden - Bad Vöslau":
                        Client = Type.Baden; break;
                };

                Plz = int.Parse(parameters["CLIENT_PLZ"] ?? "");
                Ort = parameters["CLIENT_ORT"] ?? throw new KeyNotFoundException();
                Address = parameters["CLIENT_ADDRESS"] ?? throw new KeyNotFoundException();
                PhoneNr = parameters.GetValueOrDefault("CLIENT_PHONE");
                FaxNr = parameters.GetValueOrDefault("CLIENT_FAX");
                EmailAddress = parameters.GetValueOrDefault("CLIENT_EMAIL");
                Website = parameters.GetValueOrDefault("CLIENT_WEBSITE");
                LfbisNr = parameters.GetValueOrDefault("CLIENT_LFBISNR");
                UstIdNr = parameters.GetValueOrDefault("CLIENT_USTIDNR");
                Bic = parameters.GetValueOrDefault("CLIENT_BIC");
                Iban = parameters.GetValueOrDefault("CLIENT_IBAN");

                switch (parameters.GetValueOrDefault("MODE_DELIVERYNOTE_STATS", "SHORT")?.ToUpper()) {
                    case "NONE": ModeDeliveryNoteStats = 0; break;
                    case "GA_ONLY": ModeDeliveryNoteStats = 1; break;
                    case "SHORT": ModeDeliveryNoteStats = 2; break;
                    case "FULL": ModeDeliveryNoteStats = 3; break;
                }

                Sender2 = parameters.GetValueOrDefault("DOCUMENT_SENDER") ?? "";
                TextDeliveryNote = parameters.GetValueOrDefault("TEXT_DELIVERYNOTE");
                if (TextDeliveryNote == "") TextDeliveryNote = null;
                TextDeliveryConfirmation = parameters.GetValueOrDefault("TEXT_DELIVERYCONFIRMATION");
                if (TextDeliveryConfirmation == "") TextDeliveryConfirmation = null;
                TextCreditNote = parameters.GetValueOrDefault("TEXT_CREDITNOTE");
                if (TextCreditNote == "") TextCreditNote = null;
                TextEmailSubject = parameters.GetValueOrDefault("TEXT_EMAIL_SUBJECT");
                if (TextEmailSubject == "") TextEmailSubject = null;
                TextEmailBody = parameters.GetValueOrDefault("TEXT_EMAIL_BODY");
                if (TextEmailBody == "") TextEmailBody = null;
            } catch {
                throw new KeyNotFoundException();
            }
        }

        private IEnumerable<(string, string?)> GetParamValues() {
            string deliveryNoteStats = "SHORT";
            switch (ModeDeliveryNoteStats) {
                case 0: deliveryNoteStats = "NONE"; break;
                case 1: deliveryNoteStats = "GA_ONLY"; break;
                case 2: deliveryNoteStats = "SHORT"; break;
                case 3: deliveryNoteStats = "FULL"; break;
            }
            return [
                ("CLIENT_NAME_TOKEN", NameToken),
                ("CLIENT_NAME_SHORT", NameShort),
                ("CLIENT_NAME", Name),
                ("CLIENT_NAME_SUFFIX", NameSuffix),
                ("CLIENT_NAME_TYPE", NameType),
                ("CLIENT_PLZ", Plz.ToString()),
                ("CLIENT_ORT", Ort),
                ("CLIENT_ADDRESS", Address),
                ("CLIENT_PHONE", PhoneNr),
                ("CLIENT_FAX", FaxNr),
                ("CLIENT_EMAIL", EmailAddress),
                ("CLIENT_WEBSITE", Website),
                ("CLIENT_LFBISNR", LfbisNr),
                ("CLIENT_USTIDNR", UstIdNr),
                ("CLIENT_BIC", Bic),
                ("CLIENT_IBAN", Iban),
                ("MODE_DELIVERYNOTE_STATS", deliveryNoteStats),
                ("DOCUMENT_SENDER", Sender2),
                ("TEXT_DELIVERYNOTE", TextDeliveryNote),
                ("TEXT_DELIVERYCONFIRMATION", TextDeliveryConfirmation),
                ("TEXT_CREDITNOTE", TextCreditNote),
                ("TEXT_EMAIL_SUBJECT", TextEmailSubject),
                ("TEXT_EMAIL_BODY", TextEmailBody)
            ];
        }

        public async Task UpdateValues() {
            using var cnx = await AppDbContext.ConnectAsync();
            using var cmd = cnx.CreateCommand();
            var pv = GetParamValues();
            cmd.CommandText = "INSERT INTO client_parameter (param, value) VALUES " +
                string.Join(", ", pv.Select((pv, i) => $"(@p{i}, " + (pv.Item2 != null ? $"@v{i}" : "NULL") + ")")) +
                " ON CONFLICT DO UPDATE SET value = excluded.value";

            int i = 0;
            foreach (var (p, v) in pv) {
                cmd.Parameters.Add(new SqliteParameter($"@p{i}", p));
                if (v != null)
                    cmd.Parameters.Add(new SqliteParameter($"@v{i}", v));
                i++;
            }

            await cmd.ExecuteNonQueryAsync();
            await App.HintContextChange();
        }
    }
}
