using Elwig.Models.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

namespace Elwig.Helpers.Billing {

    public record struct RawQualVaribute {
        public string QualId;
        public string? SortId;
        public string? AttrId;
        public string? CultId;

        public RawQualVaribute(string qualid, string? sortid, string? attrid, string? cultid) {
            QualId = qualid;
            SortId = sortid;
            AttrId = attrid;
            CultId = cultid;
        }
    }

    public record struct RawVaribute : IComparable<RawVaribute> {
        public string? SortId;
        public string? AttrId;
        public string? CultId;

        public RawVaribute(string? sortid, string? attrid, string? cultid) {
            SortId = sortid;
            AttrId = attrid;
            CultId = cultid;
        }

        public RawVaribute(string id) {
            var p1 = id.Split('/')[0].Split('-')[0];
            SortId = p1 == "" ? null : p1;
            AttrId = id.Contains('/') ? id.Split('/')[1].Split('-')[0] : null;
            CultId = id.Contains('-') ? id.Split('-')[1] : null;
        }

        public readonly override string ToString() {
            return $"{SortId}" + (AttrId != null ? $"/{AttrId}" : "") + (CultId != null ? $"-{CultId}" : "");
        }

        public readonly int CompareTo(RawVaribute other) {
            return $"{SortId}/{AttrId}-{CultId}".CompareTo($"{other.SortId}/{other.AttrId}-{other.CultId}");
        }
    }

    [PrimaryKey("Listing")]
    public class Varibute : IComparable<Varibute> {

        public WineVar? Variety { get; }
        public WineAttr? Attribute { get; }
        public WineCult? Cultivation { get; }
        public int? AssignedGraphId { get; set; }
        public int? AssignedAbgewGraphId { get; set; }

        public string Listing => $"{Variety?.SortId}" +
            (Attribute != null ? $"/{Attribute.AttrId}" : "") +
            (Cultivation != null ? $"-{Cultivation.CultId}" : "");
        public string FullName => $"{Variety?.Name}" +
            (Variety != null && Attribute != null ? " " : "") + $"{Attribute?.Name}" +
            ((Variety != null || Attribute != null) && Cultivation != null ? " " : "") + $"{Cultivation?.Name}";

        public Varibute(RawVaribute raw) :
            this(raw.SortId != null ? new WineVar(raw.SortId, raw.SortId) : null,
                 raw.AttrId != null ? new WineAttr() { AttrId = raw.AttrId, Name = raw.AttrId } : null,
                 raw.CultId != null ? new WineCult() { CultId = raw.CultId, Name = raw.CultId } : null) {
        }

        public Varibute(RawVaribute raw, Dictionary<string, WineVar> vars, Dictionary<string, WineAttr> attrs, Dictionary<string, WineCult> cults) :
            this(raw.SortId != null && raw.SortId != "" ? vars[raw.SortId] : null,
                 raw.AttrId != null && raw.AttrId != "" ? attrs[raw.AttrId] : null,
                 raw.CultId != null && raw.CultId != "" ? cults[raw.CultId] : null) {
        }

        public Varibute(WineVar? var, WineAttr? attr, WineCult? cult) {
            Variety = var;
            Attribute = attr;
            Cultivation = cult;
        }

        public override string ToString() {
            return Listing;
        }

        public int CompareTo(Varibute? other) {
            return Listing.CompareTo(other?.Listing);
        }
    }
}
