package test

import (
	"fmt"
	"git.sxidc.com/go-framework/baize"
	"git.sxidc.com/go-framework/baize/framework/core/application"
	"git.sxidc.com/go-framework/baize/framework/core/domain"
	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
	"git.sxidc.com/go-framework/baize/framework/core/mqtt_api"
	"git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_request"
	"git.sxidc.com/go-framework/baize/framework/core/mqtt_api/mqtt_response"
	"git.sxidc.com/go-framework/baize/framework/mqtt_binding"
	"testing"
)

type Hello struct {
	What  string `json:"what" assign:"toField:what"`
	Reply string `json:"reply"`
}

type Entity struct {
	entity.Base
	What string
}

func (e Entity) DomainCNName() string {
	return "实体"
}

func (e Entity) DomainCamelName() string {
	return "Entity"
}

func (e Entity) GetFieldMap() map[string]string {
	return map[string]string{
		"what": "什么",
	}
}

func TestMqttBinding(t *testing.T) {
	app := baize.NewApplication(application.Config{
		ApiConfig: application.ApiConfig{
			UrlPrefix: "test",
			Port:      "10080",
		},
		InfrastructureConfig: application.InfrastructureConfig{},
		MqttApiConfig: &application.MqttApiConfig{
			TopicPrefix:  "test",
			LogSkipPaths: []string{"test/version"},
			MqttConfig: application.MqttConfig{
				UserName:        "admin",
				Password:        "mtyzxhc1231",
				Address:         "emqx.sxidc.com:28739",
				ClientID:        "test",
				KeepAliveSec:    60,
				PingTimeoutSec:  60,
				WriteTimeoutSec: 60,
			},
		},
	})

	defer baize.DestroyApplication(app)

	err := app.StartMqttApi()
	if err != nil {
		t.Fatal(err)
	}

	defer func(app *application.App) {
		err := app.FinishMqttApi()
		if err != nil {
			t.Fatal(err)
		}
	}(app)

	app.MqttApi().Router().AddGlobalHandlers(func(c *mqtt_api.Context) {
		fmt.Println("Global")
	})

	mqttBinder := mqtt_binding.NewBinder(app.MqttApi().Router(), app.Infrastructure())

	mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[any]{
		Topic:            "/version",
		SendResponseFunc: mqtt_response.SendMsgResponse,
		ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (any, error) {
			fmt.Println("Version")
			return nil, nil
		},
	}, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) {
		fmt.Println("Version Middleware")
		c.Next()
		fmt.Println("Version Middleware After")
	})

	mqtt_binding.Bind(mqttBinder, &mqtt_binding.BindItem[map[string]any]{
		Topic:            "/hello",
		SendResponseFunc: mqtt_response.SendMapResponse,
		RequestParams:    &Hello{},
		ResponseIdentifierFunc: func(c *mqtt_api.Context, params any) (string, error) {
			req, err := mqtt_request.ToConcrete[*Hello](params)
			if err != nil {
				return "", err
			}

			return req.Reply, nil
		},
		Objects: []domain.Object{&Entity{}},
		ServiceFunc: func(c *mqtt_api.Context, params any, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
			fmt.Println("Hello")

			e, err := domain.ToConcrete[*Entity](objects[0])
			if err != nil {
				return make(map[string]any), err
			}

			return map[string]any{"message": "Hello " + e.What}, nil
		},
	}, func(c *mqtt_api.Context, i *infrastructure.Infrastructure) {
		fmt.Println("Hello Middleware")
		c.Next()
		fmt.Println("Hello Middleware After")
	})

	for {

	}
}