package tools

import (
	"encoding/json"
	"strings"

	"cloud.o-forge.io/core/oc-lib/config"
	"cloud.o-forge.io/core/oc-lib/logs"
	"github.com/nats-io/nats.go"
)

// NATS Method Enum defines the different methods that can be used to interact with the NATS server
type NATSMethod int

const (
	REMOVE NATSMethod = iota
	CREATE
	DISCOVERY
)

// NameToMethod returns the NATSMethod enum value from a string
func NameToMethod(name string) NATSMethod {
	for _, v := range [...]NATSMethod{REMOVE, CREATE} {
		if strings.Contains(strings.ToLower(v.String()), strings.ToLower(name)) {
			return v
		}
	}
	return -1
}

// GenerateKey generates a key for the NATSMethod usefull for standard key based on data name & method
func (d NATSMethod) GenerateKey(name string) string {
	return name + "_" + d.String()
}

// String returns the string of the enum
func (d NATSMethod) String() string {
	return [...]string{"remove", "create", "discovery"}[d]
}

type natsCaller struct{}

// NewNATSCaller creates a new instance of the NATS Caller
func NewNATSCaller() *natsCaller {
	return &natsCaller{}
}

// on workflows' scheduling. Messages must contain
// workflow execution ID, to allow retrieval of execution infos
func (s *natsCaller) ListenNats(chanName string, exec func(msg map[string]interface{})) {
	log := logs.GetLogger()
	if config.GetConfig().NATSUrl == "" {
		log.Error().Msg(" -> NATS_SERVER is not set")
		return
	}
	nc, err := nats.Connect(config.GetConfig().NATSUrl)
	if err != nil {
		log.Error().Msg(" -> Could not reach NATS server : " + err.Error())
		return
	}
	ch := make(chan *nats.Msg, 64)
	subs, err := nc.ChanSubscribe(chanName, ch)
	if err != nil {
		log.Error().Msg("Error listening to NATS : " + err.Error())
	}
	defer subs.Unsubscribe()

	for msg := range ch {
		map_mess := map[string]interface{}{}
		json.Unmarshal(msg.Data, &map_mess)
		exec(map_mess)
	}
}

// SetNATSPub sets a message to the NATS server
func (o *natsCaller) SetNATSPub(dataName string, method NATSMethod, data interface{}) string {
	if config.GetConfig().NATSUrl == "" {
		return " -> NATS_SERVER is not set"
	}
	nc, err := nats.Connect(config.GetConfig().NATSUrl)
	if err != nil {
		return " -> Could not reach NATS server : " + err.Error()
	}
	defer nc.Close()
	js, err := json.Marshal(data)
	if err != nil {
		return " -> " + err.Error()
	}
	err = nc.Publish(method.GenerateKey(dataName), js) // Publish the message on the NATS server with a channel name based on the data name (or whatever start) and the method
	if err != nil {
		return " -> " + err.Error() // Return an error if the message could not be published
	}
	return ""
}