package application_test

import (
	"context"
	"strings"
	"testing"
	"time"

	"git.ali33.ru/fcg-xvii/rest"
	"git.ali33.ru/fcg-xvii/rest/application"
	"git.ali33.ru/fcg-xvii/rest/commands"
)

func init() {
	commands.Register("hello", func() rest.IExecuter {
		return &Hello{}
	})
}

type Hello struct {
	Name string `rest:"required"`
}

func (s *Hello) Validate(req rest.IRequestIn) rest.IRequestOut {
	if s.Name = strings.TrimSpace(s.Name); len(s.Name) == 0 {
		return req.OutError(rest.ErrorFiled("name", "expected not empty string"))
	}
	return nil
}

func (s *Hello) Execute(req rest.IRequestIn) rest.IRequestOut {
	response := HelloResponse{
		Message: "Hello, " + s.Name,
	}
	return rest.OutFields(req, &response, nil, nil)
}

type HelloResponse struct {
	Message string `rest:"default"`
}

////////////////////////////

var (
	commandsList = map[string]func() rest.IExecuter{
		"/api/hello": func() rest.IExecuter { return &Hello{} },
	}
)

func Command(cName string) (rest.IExecuter, bool) {
	if command, check := commandsList[cName]; check {
		return command(), true
	}
	return nil, false
}

func TestApp(t *testing.T) {
	conf := &application.AppConfig{
		Addr:       "192.168.100.3:5000",
		Http:       true,
		Websocket:  false,
		Secret:     []byte("secret"),
		HttpPrefix: "/api/",
	}

	app := application.New(conf, nil, context.Background())
	t.Log(app)
	<-time.After(time.Hour)
}