package main

import (
	"context"
	"io"
	"log"
	"os"

	"github.com/fgm/izidic"

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

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) {
	ctx := context.Background()
	dic := Resolve(w, name, args)
	lister := dic.MustService(services.SvcLister).(func(ctx context.Context) string)
	qURL := lister(ctx)

	producer := dic.MustService(services.SvcProducer).(func(ctx context.Context, qName string))
	producer(ctx, qURL)

	log.Printf("exiting cleanly")
	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.SvcProducer, services.ProducerService)

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