package itemoccurrences

import (
	"sort"

	"gitea.maximumdirect.net/eric/notarius/internal/core/source"
	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
	itemregistry "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/items/registry"
	"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd/shared"
)

type orderedItemOccurrenceResponse struct {
	value       itemOccurrenceResponse
	earliest    int
	hasEvidence bool
}

func canonicalizeResponse(response *extractionResponse, order shared.SourceRefOrder, sourceID string) {
	if response == nil {
		return
	}
	ordered := make([]orderedItemOccurrenceResponse, len(response.Occurrences))
	for index := range response.Occurrences {
		earliest, hasEvidence := canonicalizeItemOccurrence(&response.Occurrences[index], order, sourceID)
		ordered[index] = orderedItemOccurrenceResponse{
			value:       response.Occurrences[index],
			earliest:    earliest,
			hasEvidence: hasEvidence,
		}
	}
	sort.SliceStable(ordered, func(left, right int) bool {
		if ordered[left].hasEvidence != ordered[right].hasEvidence {
			return ordered[left].hasEvidence
		}
		if !ordered[left].hasEvidence {
			return false
		}
		return ordered[left].earliest < ordered[right].earliest
	})
	for index := range ordered {
		response.Occurrences[index] = ordered[index].value
	}
}

func canonicalizeItemOccurrence(occurrence *itemOccurrenceResponse, order shared.SourceRefOrder, sourceID string) (int, bool) {
	if occurrence == nil {
		return 0, false
	}
	refs := order.Canonicalize(itemOccurrenceSourceRefs(occurrence.SourceRefs, sourceID))
	occurrence.SourceRefs = itemOccurrenceResponseRefs(refs)
	return order.EarliestValid(refs)
}

func canonicalItemOccurrenceList(response extractionResponse, sourceID string, registry *itemregistry.Registry) dnd.ItemOccurrenceList {
	if response.Occurrences == nil {
		return dnd.ItemOccurrenceList{}
	}
	occurrences := make([]dnd.ItemOccurrence, 0, len(response.Occurrences))
	for _, occurrence := range response.Occurrences {
		item, found := registry.LookupID(occurrence.ItemID)
		if !found || item.Name != occurrence.Name {
			continue
		}
		occurrences = append(occurrences, dnd.ItemOccurrence{
			ItemID:     occurrence.ItemID,
			Name:       occurrence.Name,
			Kind:       dnd.ItemOccurrenceKind(occurrence.Kind),
			Quantity:   cloneQuantity(occurrence.Quantity),
			From:       occurrence.From,
			To:         occurrence.To,
			SourceRefs: itemOccurrenceSourceRefs(occurrence.SourceRefs, sourceID),
		})
	}
	return dnd.ItemOccurrenceList{Occurrences: occurrences}
}

func itemOccurrenceSourceRefs(refs []itemOccurrenceSourceRefResponse, sourceID string) []source.SourceRef {
	if refs == nil {
		return nil
	}
	values := make([]source.SourceRef, len(refs))
	for index, ref := range refs {
		values[index] = source.SourceRef{SourceID: sourceID, StartUnitID: ref.StartSegment, EndUnitID: ref.EndSegment}
	}
	return values
}

func itemOccurrenceResponseRefs(refs []source.SourceRef) []itemOccurrenceSourceRefResponse {
	if refs == nil {
		return nil
	}
	values := make([]itemOccurrenceSourceRefResponse, len(refs))
	for index, ref := range refs {
		values[index] = itemOccurrenceSourceRefResponse{StartSegment: ref.StartUnitID, EndSegment: ref.EndUnitID}
	}
	return values
}

func cloneQuantity(value *int) *int {
	if value == nil {
		return nil
	}
	quantity := *value
	return &quantity
}
