package processor_test

import (
	"reflect"
	"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 TestOSCMessageDecodeFromRegistry(t *testing.T) {
	registration, ok := processor.GetProcessorRegistration("osc.message.decode")
	if !ok {
		t.Fatalf("osc.message.decode processor not registered")
	}

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

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

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

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

func TestGoodOSCMessageDecode(t *testing.T) {
	processorInstance := processor.OSCMessageDecode{}
	tests := []struct {
		name     string
		payload  []byte
		expected *osc.OSCMessage
	}{
		{
			name:    "basic OSC message",
			payload: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 0, 0, 0},
			expected: &osc.OSCMessage{
				Address: "/test",
				Args:    []osc.OSCArg{},
			},
		},
		{
			name:    "basic OSC message with argument",
			payload: []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 105, 0, 0, 0, 0, 0, 42},
			expected: &osc.OSCMessage{
				Address: "/test",
				Args: []osc.OSCArg{
					{
						Type:  "i",
						Value: int32(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.decode processing failed: %s", err)
			}

			gotMessage, ok := got.Payload.(*osc.OSCMessage)
			if !ok {
				t.Fatalf("osc.message.decode returned a %T payload: %+v", got, got)
			}

			if !reflect.DeepEqual(gotMessage, test.expected) {
				t.Fatalf("osc.message.decode got %+v, expected %+v", gotMessage, test.expected)
			}
		})
	}
}

func TestBadOSCMessageDecode(t *testing.T) {
	processorInstance := processor.OSCMessageDecode{}
	tests := []struct {
		name        string
		payload     any
		errorString string
	}{
		{
			name:        "non []byte payload",
			payload:     "test",
			errorString: "osc.message.decode processor only accepts a []byte payload",
		},
		{
			name:        "empty []byte payload",
			payload:     []byte{},
			errorString: "osc.message.decode processor can't work on empty []byte",
		},
		{
			name:        "wrong start byte in payload",
			payload:     []byte{48, 116, 101, 115, 116, 0, 0, 0, 44, 105, 0, 0, 0, 0, 0, 42},
			errorString: "osc.message.decode processor needs an OSC looking []byte",
		},
		{
			name:        "invalid OSC payload",
			payload:     []byte{47, 116, 101, 115, 116, 0},
			errorString: "osc.message.decode processor failed to decode OSC message: OSC string is not properly padded",
		},
	}

	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.decode expected to fail but got payload: %+v", got)
			}
			if err.Error() != test.errorString {
				t.Fatalf("osc.message.decode got error '%s', expected '%s'", err.Error(), test.errorString)
			}
		})
	}
}

func BenchmarkOSCMessageDecode(b *testing.B) {
	processorInstance := processor.OSCMessageDecode{}
	payload := []byte{47, 116, 101, 115, 116, 0, 0, 0, 44, 105, 0, 0, 0, 0, 0, 42}

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