using Elwig.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;

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

        protected readonly IEnumerable<RawVaribute> Vaributes;

        public EditBillingData(JsonObject data, IEnumerable<RawVaribute> vaributes) :
            base(data) {
            Vaributes = vaributes;
        }

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

        private (Dictionary<int, Curve>, Dictionary<int, List<RawVaribute>>) GetGraphEntries(JsonNode root) {
            Dictionary<int, List<string>> dict1 = [];
            Dictionary<decimal, List<string>> dict2 = [];
            if (root is JsonObject paymentObj) {
                foreach (var (selector, node) in paymentObj) {
                    var val = node?.AsValue();
                    if (val == null) {
                        continue;
                    } else if (val.TryGetValue<decimal>(out var price)) {
                        if (!dict2.ContainsKey(price)) dict2[price] = [];
                        dict2[price].Add(selector);
                    } else if (val.TryGetValue<string>(out var curve)) {
                        var idx = int.Parse(curve.Split(":")[1] ?? "0");
                        if (!dict1.ContainsKey(idx)) dict1[idx] = [];
                        dict1[idx].Add(selector);
                    }
                }
            } else if (root is JsonValue paymentVal) {
                if (paymentVal.TryGetValue<decimal>(out var price)) {
                    if (!dict2.ContainsKey(price)) dict2[price] = [];
                    dict2[price].Add("default");
                } else if (paymentVal.TryGetValue<string>(out var curve)) {
                    var idx = int.Parse(curve.Split(":")[1] ?? "0");
                    if (!dict1.ContainsKey(idx)) dict1[idx] = [];
                    dict1[idx].Add("default");
                }
            }

            var virtOffset = dict1.Count > 0 ? dict1.Max(e => e.Key) + 1 : 1;
            Dictionary<int, Curve> curves = GetCurves();
            decimal[] virtCurves = [.. dict2.Keys.Order()];
            for (int i = 0; i < virtCurves.Length; i++) {
                var idx = virtCurves[i];
                dict1[i + virtOffset] = dict2[idx];
                curves[i + virtOffset] = new Curve(CurveMode.Oe, new() { { 73, idx } }, null);
            }

            Dictionary<int, List<RawVaribute>> dict3 = curves.ToDictionary(c => c.Key, _ => new List<RawVaribute>());
            foreach (var (selector, value) in GetSelection(root, Vaributes)) {
                int? idx = null;
                if (value.TryGetValue<decimal>(out var val)) {
                    idx = Array.IndexOf(virtCurves, val) + virtOffset;
                } else if (value.TryGetValue<string>(out var str)) {
                    idx = int.Parse(str.Split(":")[1]);
                }
                if (idx != null)
                    dict3[(int)idx].Add(selector);
            }

            return (curves, dict3);
        }

        private static List<GraphEntry> CreateGraphEntries(
            AppDbContext ctx, int precision,
            Dictionary<int, Curve> curves,
            Dictionary<int, List<RawVaribute>> entries
        ) {
            var vars = ctx.WineVarieties.ToDictionary(v => v.SortId, v => v);
            var attrs = ctx.WineAttributes.ToDictionary(a => a.AttrId, a => a);
            var cults = ctx.WineCultivations.ToDictionary(c => c.CultId, c => c);
            return entries
                .Select(e => new GraphEntry(e.Key, precision, curves[e.Key], e.Value
                    .Select(s => new Varibute(s, vars, attrs, cults))
                    .ToList()))
                .ToList();
        }

        public IEnumerable<GraphEntry> GetPaymentGraphEntries(AppDbContext ctx, Season season) {
            var root = GetPaymentEntry();
            var (curves, entries) = GetGraphEntries(root);
            return CreateGraphEntries(ctx, season.Precision, curves, entries).Where(e => e.Vaributes.Count > 0);
        }

        public IEnumerable<GraphEntry> GetQualityGraphEntries(AppDbContext ctx, Season season, int idOffset = 0) {
            var root = GetQualityEntry();
            if (root == null || root["WEI"] is not JsonNode qualityWei)
                return [];
            var (curves, entries) = GetGraphEntries(qualityWei);
            var list = CreateGraphEntries(ctx, season.Precision, curves, entries).Where(e => e.Vaributes.Count > 0);
            foreach (var e in list) {
                e.Id += idOffset;
                e.Abgewertet = true;
            }
            return list;
        }
    }
}
