package register

import (
	"slices"

	"gitea.maximumdirect.net/eric/notarius/internal/core/source"
	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
)

func appendSpellLists(values []dnd.SpellList) (dnd.SpellList, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.SpellCasts != nil {
			present = true
		}
		count += len(value.SpellCasts)
	}
	if !present {
		return dnd.SpellList{}, nil
	}
	combined := dnd.SpellList{SpellCasts: make([]dnd.SpellCast, 0, count)}
	for _, value := range values {
		for _, spellCast := range value.SpellCasts {
			combined.SpellCasts = append(combined.SpellCasts, cloneSpellCast(spellCast))
		}
	}
	return combined, nil
}

func appendNPCRegistries(values []dnd.NPCRegistry) (dnd.NPCRegistry, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.NPCs != nil {
			present = true
		}
		count += len(value.NPCs)
	}
	if !present {
		return dnd.NPCRegistry{}, nil
	}
	combined := dnd.NPCRegistry{NPCs: make([]dnd.NPC, 0, count)}
	for _, value := range values {
		for _, npc := range value.NPCs {
			combined.NPCs = append(combined.NPCs, cloneNPC(npc))
		}
	}
	return combined, nil
}

func appendCombatTurnLists(values []dnd.CombatTurnList) (dnd.CombatTurnList, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.CombatTurns != nil {
			present = true
		}
		count += len(value.CombatTurns)
	}
	if !present {
		return dnd.CombatTurnList{}, nil
	}
	combined := dnd.CombatTurnList{CombatTurns: make([]dnd.CombatTurn, 0, count)}
	for _, value := range values {
		for _, turn := range value.CombatTurns {
			combined.CombatTurns = append(combined.CombatTurns, cloneCombatTurn(turn))
		}
	}
	return combined, nil
}

func appendEnemyEventLists(values []dnd.EnemyEventList) (dnd.EnemyEventList, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.Events != nil {
			present = true
		}
		count += len(value.Events)
	}
	if !present {
		return dnd.EnemyEventList{}, nil
	}
	combined := dnd.EnemyEventList{Events: make([]dnd.EnemyEvent, 0, count)}
	for _, value := range values {
		for _, event := range value.Events {
			combined.Events = append(combined.Events, cloneEnemyEvent(event))
		}
	}
	return combined, nil
}

func appendItemOccurrenceLists(values []dnd.ItemOccurrenceList) (dnd.ItemOccurrenceList, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.Occurrences != nil {
			present = true
		}
		count += len(value.Occurrences)
	}
	if !present {
		return dnd.ItemOccurrenceList{}, nil
	}
	combined := dnd.ItemOccurrenceList{Occurrences: make([]dnd.ItemOccurrence, 0, count)}
	for _, value := range values {
		for _, occurrence := range value.Occurrences {
			combined.Occurrences = append(combined.Occurrences, cloneItemOccurrence(occurrence))
		}
	}
	return combined, nil
}

func appendItemRegistries(values []dnd.ItemRegistry) (dnd.ItemRegistry, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.Items != nil {
			present = true
		}
		count += len(value.Items)
	}
	if !present {
		return dnd.ItemRegistry{}, nil
	}
	combined := dnd.ItemRegistry{Items: make([]dnd.Item, 0, count)}
	for _, value := range values {
		for _, item := range value.Items {
			combined.Items = append(combined.Items, cloneItem(item))
		}
	}
	return combined, nil
}

func appendNPCOccurrenceLists(values []dnd.NPCOccurrenceList) (dnd.NPCOccurrenceList, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.Occurrences != nil {
			present = true
		}
		count += len(value.Occurrences)
	}
	if !present {
		return dnd.NPCOccurrenceList{}, nil
	}
	combined := dnd.NPCOccurrenceList{Occurrences: make([]dnd.NPCOccurrence, 0, count)}
	for _, value := range values {
		for _, occurrence := range value.Occurrences {
			combined.Occurrences = append(combined.Occurrences, cloneNPCOccurrence(occurrence))
		}
	}
	return combined, nil
}

func appendSceneDescriptionLists(values []dnd.SceneDescriptionList) (dnd.SceneDescriptionList, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.Scenes != nil {
			present = true
		}
		count += len(value.Scenes)
	}
	if !present {
		return dnd.SceneDescriptionList{}, nil
	}
	combined := dnd.SceneDescriptionList{Scenes: make([]dnd.SceneDescription, 0, count)}
	for _, value := range values {
		combined.Scenes = append(combined.Scenes, value.Scenes...)
	}
	return combined, nil
}

func appendLocationRegistries(values []dnd.LocationRegistry) (dnd.LocationRegistry, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.Locations != nil {
			present = true
		}
		count += len(value.Locations)
	}
	if !present {
		return dnd.LocationRegistry{}, nil
	}
	combined := dnd.LocationRegistry{Locations: make([]dnd.Location, 0, count)}
	for _, value := range values {
		for _, location := range value.Locations {
			combined.Locations = append(combined.Locations, cloneLocation(location))
		}
	}
	return combined, nil
}

func appendLocationOccurrenceLists(values []dnd.LocationOccurrenceList) (dnd.LocationOccurrenceList, error) {
	count := 0
	present := false
	for _, value := range values {
		if value.Occurrences != nil {
			present = true
		}
		count += len(value.Occurrences)
	}
	if !present {
		return dnd.LocationOccurrenceList{}, nil
	}
	combined := dnd.LocationOccurrenceList{Occurrences: make([]dnd.LocationOccurrence, 0, count)}
	for _, value := range values {
		for _, occurrence := range value.Occurrences {
			combined.Occurrences = append(combined.Occurrences, cloneLocationOccurrence(occurrence))
		}
	}
	return combined, nil
}

func cloneCombatTurn(value dnd.CombatTurn) dnd.CombatTurn {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneEnemyEvent(value dnd.EnemyEvent) dnd.EnemyEvent {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneItemOccurrence(value dnd.ItemOccurrence) dnd.ItemOccurrence {
	clone := value
	if value.Quantity != nil {
		quantity := *value.Quantity
		clone.Quantity = &quantity
	}
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneItem(value dnd.Item) dnd.Item {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneSpellCast(value dnd.SpellCast) dnd.SpellCast {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneNPC(value dnd.NPC) dnd.NPC {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneNPCOccurrence(value dnd.NPCOccurrence) dnd.NPCOccurrence {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneLocation(value dnd.Location) dnd.Location {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneLocationOccurrence(value dnd.LocationOccurrence) dnd.LocationOccurrence {
	clone := value
	clone.SourceRefs = cloneSourceRefs(value.SourceRefs)
	return clone
}

func cloneSourceRefs(refs []source.SourceRef) []source.SourceRef {
	return slices.Clone(refs)
}
