package module_test

import (
	"testing"
	"time"

	"github.com/jwetzell/showbridge-go/config"
	"github.com/jwetzell/showbridge-go/internal/module"
)

func TestTimeTimerFromRegistry(t *testing.T) {
	registration, ok := module.GetModuleRegistration("time.timer")
	if !ok {
		t.Fatalf("time.timer module not registered")
	}

	moduleInstance, err := registration.New(config.ModuleConfig{
		Id:   "test",
		Type: "time.timer",
		Params: map[string]any{
			"duration": 1000,
		},
	})

	if err != nil {
		t.Fatalf("failed to create time.timer module: %s", err)
	}

	if moduleInstance.Id() != "test" {
		t.Fatalf("time.timer module has wrong id: %s", moduleInstance.Id())
	}

	if moduleInstance.Type() != "time.timer" {
		t.Fatalf("time.timer module has wrong type: %s", moduleInstance.Type())
	}
}

func TestGoodTimeTimer(t *testing.T) {

	testCases := []struct {
		name   string
		params map[string]any
	}{
		{
			name: "minimal config",
			params: map[string]any{
				"duration": 2000,
			},
		},
	}

	for _, test := range testCases {
		t.Run(test.name, func(t *testing.T) {

			registration, ok := module.GetModuleRegistration("time.timer")
			if !ok {
				t.Fatalf("time.timer module not registered")
			}

			moduleInstance, err := registration.New(config.ModuleConfig{
				Id:     "test",
				Type:   "time.timer",
				Params: test.params,
			})

			if err != nil {
				t.Fatalf("time.timer failed to create module: %s", err)
			}
			// TODO(jwetzell) this is kind of hacky
			go func() {
				time.Sleep(1 * time.Second)
				moduleInstance.Stop()
			}()
			err = moduleInstance.Start(t.Context(), nil)

			if err != nil {
				t.Fatalf("time.timer failed to start: %s", err)
			}
		})
	}
}

func TestBadTimeTimer(t *testing.T) {
	tests := []struct {
		name        string
		params      map[string]any
		errorString string
	}{
		{
			name:        "no duration param",
			params:      map[string]any{},
			errorString: "time.timer duration error: not found",
		},
		{
			name: "non-number duration param",
			params: map[string]any{
				"duration": "8000",
			},
			errorString: "time.timer duration error: not a number",
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {

			registration, ok := module.GetModuleRegistration("time.timer")
			if !ok {
				t.Fatalf("time.timer module not registered")
			}

			moduleInstance, err := registration.New(config.ModuleConfig{
				Id:     "test",
				Type:   "time.timer",
				Params: test.params,
			})

			if err != nil {
				if test.errorString != err.Error() {
					t.Fatalf("time.timer got error '%s', expected '%s'", err.Error(), test.errorString)
				}
				return
			}

			err = moduleInstance.Start(t.Context(), nil)

			if err == nil {
				t.Fatalf("time.timer expected to fail")
			}

			if err.Error() != test.errorString {
				t.Fatalf("time.timer got error '%s', expected '%s'", err.Error(), test.errorString)
			}
		})
	}
}
