using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;

namespace Elwig.Helpers.Billing {
    public class PaymentBillingData : BillingData {

        protected readonly Dictionary<int, Curve> Curves;
        protected readonly Dictionary<RawVaribute, Curve> PaymentData;
        protected readonly Dictionary<RawQualVaribute, Curve> QualityData;
        protected readonly IEnumerable<RawVaribute> Vaributes;

        public PaymentBillingData(JsonObject data, IEnumerable<RawVaribute> vaributes) :
            base(data) {
            Vaributes = vaributes;
            Curves = GetCurves();
            PaymentData = GetPaymentData();
            QualityData = GetQualityData();
        }

        public static PaymentBillingData FromJson(string json, IEnumerable<RawVaribute> vaributes) {
            return new(ParseJson(json), vaributes);
        }

        private Dictionary<RawVaribute, Curve> GetData(JsonNode data) {
            return GetSelection(data, Vaributes).ToDictionary(e => e.Key, e => LookupCurve(e.Value));
        }

        protected Dictionary<RawVaribute, Curve> GetPaymentData() {
            return GetData(GetPaymentEntry());
        }

        protected Dictionary<RawQualVaribute, Curve> GetQualityData() {
            Dictionary<RawQualVaribute, Curve> dict = [];
            var q = GetQualityEntry();
            if (q == null) return dict;

            foreach (var (qualid, data) in q) {
                foreach (var (idx, d) in GetData(data ?? throw new InvalidOperationException())) {
                    dict[new(qualid, idx.SortId, idx.AttrId, idx.CultId)] = d;
                }
            }

            return dict;
        }

        public decimal CalculatePrice(string sortid, string? attrid, string? cultid, string qualid, bool gebunden, double oe, double kmw) {
            var curve = GetQualityCurve(qualid, sortid, attrid, cultid) ?? GetCurve(sortid, attrid, cultid);
            return GetCurveValueAt((gebunden ? curve.Gebunden : null) ?? curve.Normal, curve.Mode == CurveMode.Oe ? oe : kmw);
        }

        private Curve LookupCurve(JsonValue val) {
            if (val.TryGetValue(out string? curve)) {
                var curveId = int.Parse(curve.Split(":")[1]);
                return Curves[curveId];
            } else if (val.TryGetValue(out decimal value)) {
                return new(CurveMode.Oe, new() { { 73, value } }, null);
            }
            throw new InvalidOperationException();
        }

        protected Curve GetCurve(string sortid, string? attrid, string? cultid) {
            return PaymentData[new(sortid, attrid ?? "", cultid ?? "")];
        }

        protected Curve? GetQualityCurve(string qualid, string sortid, string? attrid, string? cultid) {
            return QualityData.TryGetValue(new(qualid, sortid, attrid ?? "", cultid ?? ""), out var curve) ? curve : null;
        }
    }
}
