package main

import (
	"io"
	"os"

	"github.com/fgm/izidic"
	"github.com/gin-gonic/gin"

	"code.osinet.fr/fgm/sqs_demo/back/web"

	"code.osinet.fr/fgm/sqs_demo/back/services"
	"code.osinet.fr/fgm/sqs_demo/back/services/redriver"
)

func main() {
	os.Exit(int(main2(os.Stdout, os.Args[0], os.Args[1:])))
}

func main2(w io.Writer, name string, args []string) (exitCode byte) {
	dic := Resolve(w, name, args)
	h := dic.MustService(services.SvcHttp).(*gin.Engine)
	addr := dic.MustParam(services.PAddr).(string)
	if err := h.Run(addr); err != nil {
		return 1
	}
	return 0
}

func Resolve(w io.Writer, name string, args []string) *izidic.Container {
	dic := izidic.New()
	dic.Store(services.PName, name)
	dic.Store(services.PArgs, args)
	dic.Store(services.PWriter, w)

	dic.Register(services.SvcClient, services.SQSClientService)
	dic.Register(services.SvcFlags, services.FlagsService)
	dic.Register(services.SvcLister, services.ListerService)
	dic.Register(services.SvcLogger, services.LoggerService)
	dic.Register(services.SvcMessageStore, redriver.MessageStoreService)
	dic.Register(services.SvcProducer, services.ProducerService)
	dic.Register(services.SvcRedriver, redriver.RedriverService)
	dic.Register(services.SvcHttp, web.HttpRedriverService)
	dic.Register(services.SvcRenderer, web.RendererService)

	dic.MustService(services.SvcFlags) // Store generated params before freeze.
	dic.Freeze()
	return dic
}
