package processor_test

import (
	"slices"
	"testing"

	"github.com/jwetzell/osc-go"
	"github.com/jwetzell/showbridge-go/config"
	"github.com/jwetzell/showbridge-go/internal/common"
	"github.com/jwetzell/showbridge-go/internal/processor"
)

func TestOSCMessageEncodeFromRegistry(t *testing.T) {
	registration, ok := processor.GetProcessorRegistration("osc.message.encode")
	if !ok {
		t.Fatalf("osc.message.encode processor not registered")
	}

	processorInstance, err := registration.New(config.ProcessorConfig{
		Id:   "test-id",
		Type: "osc.message.encode",
	})

	if err != nil {
		t.Fatalf("failed to create osc.message.encode processor: %s", err)
	}

	if processorInstance.Id() != "test-id" {
		t.Fatalf("osc.message.encode processor has wrong id: %s", processorInstance.Id())
	}

	if processorInstance.Type() != "osc.message.encode" {
		t.Fatalf("osc.message.encode processor has wrong type: %s", processorInstance.Type())
	}
}

func TestGoodOSCMessageEncode(t *testing.T) {
	processorInstance := processor.OSCMessageEncode{}
	tests := []struct {
		name     string
		payload  any
		expected []byte
	}{
		{
			name: "basic OSC message",
			payload: &osc.OSCMessage{
				Address: "/test",
			},
			expected: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 0, 0, 0},
		},
		{
			name: "basic OSC message with argument",
			payload: &osc.OSCMessage{
				Address: "/test",
				Args: []osc.OSCArg{
					{
						Type:  "i",
						Value: int32(42),
					},
				},
			},
			expected: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 105, 0, 0, 0, 0, 0, 42},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})
			if err != nil {
				t.Fatalf("osc.message.encode processing failed: %s", err)
			}

			gotBytes, ok := got.Payload.([]byte)
			if !ok {
				t.Fatalf("osc.message.encode returned a %T payload: %+v", got, got)
			}

			if !slices.Equal(gotBytes, test.expected) {
				t.Fatalf("osc.message.encode got %+v, expected %+v", got, test.expected)
			}
		})
	}
}

func TestBadOSCMessageEncode(t *testing.T) {
	processorInstance := processor.OSCMessageEncode{}
	tests := []struct {
		name        string
		payload     any
		errorString string
	}{
		{
			name:        "non-osc message input",
			payload:     "test",
			errorString: "osc.message.encode processor only accepts an *OSCMessage",
		},
		{
			name: "invalid OSC message argument",
			payload: &osc.OSCMessage{
				Address: "test",
				Args:    []osc.OSCArg{},
			},
			errorString: "osc.message.encode processor failed to encode OSCMessage: OSC Message address must start with /",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := processorInstance.Process(t.Context(), common.WrappedPayload{Payload: test.payload})

			if err == nil {
				t.Fatalf("osc.message.encode expected to fail but got payload: %+v", got)
			}
			if err.Error() != test.errorString {
				t.Fatalf("osc.message.encode got error '%s', expected '%s'", err.Error(), test.errorString)
			}
		})
	}
}

func BenchmarkOSCMessageEncode(b *testing.B) {
	processorInstance := processor.OSCMessageEncode{}
	payload := &osc.OSCMessage{
		Address: "/test",
		Args: []osc.OSCArg{
			{
				Type:  "i",
				Value: int32(42),
			},
		},
	}

	for b.Loop() {
		_, err := processorInstance.Process(b.Context(), common.WrappedPayload{Payload: payload})
		if err != nil {
			b.Fatalf("osc.message.encode processing failed: %s", err)
		}
	}
}
