package routewatch

import (
	"git.eeqj.de/sneak/routewatch/internal/logger"
	"git.eeqj.de/sneak/routewatch/internal/ristypes"
)

// SimpleHandler is a basic implementation of streamer.MessageHandler
type SimpleHandler struct {
	logger       *logger.Logger
	messageTypes []string
	callback     func(*ristypes.RISMessage)
}

// NewSimpleHandler creates a handler that accepts specific message types
func NewSimpleHandler(
	logger *logger.Logger,
	messageTypes []string,
	callback func(*ristypes.RISMessage),
) *SimpleHandler {
	return &SimpleHandler{
		logger:       logger,
		messageTypes: messageTypes,
		callback:     callback,
	}
}

// WantsMessage returns true if this handler wants to process messages of the given type
func (h *SimpleHandler) WantsMessage(messageType string) bool {
	// If no specific types are set, accept all messages
	if len(h.messageTypes) == 0 {
		return true
	}

	for _, t := range h.messageTypes {
		if t == messageType {
			return true
		}
	}

	return false
}

// HandleMessage processes a RIS message
func (h *SimpleHandler) HandleMessage(msg *ristypes.RISMessage) {
	if h.callback != nil {
		h.callback(msg)
	}
}
