using System.Text.Json;
using RolemasterDb.App.Features;

namespace RolemasterDb.App.Components.Shared;

public sealed class CriticalBranchEditorModel
{
    public string BranchKind { get; set; } = "conditional";
    public string? ConditionKey { get; set; }
    public string ConditionText { get; set; } = string.Empty;
    public string ConditionJson { get; set; } = "{}";
    public string RawText { get; set; } = string.Empty;
    public string DescriptionText { get; set; } = string.Empty;
    public string? RawAffixText { get; set; }
    public string ParsedJson { get; set; } = "{}";
    public int SortOrder { get; set; }
    public string? OriginKey { get; set; }
    public bool IsOverridden { get; set; }
    public bool AreEffectsOverridden { get; set; }
    public List<CriticalEffectEditorModel> Effects { get; set; } = [];

    public static CriticalBranchEditorModel FromItem(CriticalBranchEditorItem item) =>
        new()
        {
            BranchKind = item.BranchKind,
            ConditionKey = item.ConditionKey,
            ConditionText = item.ConditionText,
            ConditionJson = item.ConditionJson,
            RawText = item.RawText,
            DescriptionText = item.DescriptionText,
            RawAffixText = item.RawAffixText,
            ParsedJson = item.ParsedJson,
            SortOrder = item.SortOrder,
            OriginKey = item.OriginKey,
            IsOverridden = item.IsOverridden,
            AreEffectsOverridden = item.AreEffectsOverridden,
            Effects = item.Effects.Select(CriticalEffectEditorModel.FromItem).ToList()
        };

    public CriticalBranchEditorItem ToItem() =>
        new(
            BranchKind,
            ConditionKey,
            ConditionText,
            "{}",
            BuildRawText(),
            DescriptionText,
            RawAffixText,
            SerializeParsedEffects(Effects),
            SortOrder,
            OriginKey,
            IsOverridden,
            AreEffectsOverridden,
            Effects.Select(effect => effect.ToItem()).ToList());

    private string BuildRawText()
    {
        var condition = ConditionText.Trim();
        var description = DescriptionText.Trim();
        var firstLine = string.IsNullOrWhiteSpace(description)
            ? $"{condition}:"
            : $"{condition}: {description}";

        if (string.IsNullOrWhiteSpace(RawAffixText))
        {
            return firstLine;
        }

        return $"{firstLine}{Environment.NewLine}{RawAffixText.Trim()}";
    }

    private static string SerializeParsedEffects(IReadOnlyList<CriticalEffectEditorModel> effects) =>
        effects.Count == 0
            ? "{}"
            : JsonSerializer.Serialize(new { effects = effects.Select(effect => effect.ToItem()).ToList() });

    public CriticalBranchEditorModel Clone() =>
        new()
        {
            BranchKind = BranchKind,
            ConditionKey = ConditionKey,
            ConditionText = ConditionText,
            ConditionJson = ConditionJson,
            RawText = RawText,
            DescriptionText = DescriptionText,
            RawAffixText = RawAffixText,
            ParsedJson = ParsedJson,
            SortOrder = SortOrder,
            OriginKey = OriginKey,
            IsOverridden = IsOverridden,
            AreEffectsOverridden = AreEffectsOverridden,
            Effects = Effects.Select(effect => effect.Clone()).ToList()
        };
}
