package main

import (
	"fmt"

	"github.com/gookit/goutil/dump"
	"github.com/zeevdiukman/go-config"
	"github.com/zeevdiukman/go-helper"
)

// type ReverseProxy struct {
// 	Proxy  *http.Handler
// 	Domain string
// 	Router *router.HostRouter
// }

const CERTS_PATH string = "./assets/certs/"

func main() {
	helper.AppRunner(true, func() {
		// ctx := context.Background()
		c := config.NewConfig("config", "yaml", "./assets/config")
		c.Load()

		// routersMap := make(map[string]*router.HostRouter)
		routersMap := make(map[string]map[string]string)

		firstPart, subKeys := c.GetSubKeys("http.routers", false)
		subKeys.ForEach(func(idx int, key config.Key) {
			fullKey := helper.AddDotBetween(firstPart, string(key))
			p := key.GetKeyPartByLevel(0)
			name := string(p)

			fmt.Println(name + ":")
			if _, ok := routersMap[string(name)]; !ok {
				routersMap[string(name)] = make(map[string]string)
			}

			key.ForEach(func(idx int, part config.KeyPart, isFirst bool) {
				if !isFirst {
					field := string(part)

					val := c.Get(fullKey).(string)

					routersMap[string(name)][field] = val
				}
			})

			// router.NewHostRouter("keycloak.z.com")
			// router.NewHostRouter("keycloak.z.com").Handler(proxy)

		})
		dump.Println(routersMap)

		// proxy := reverseproxy.New(ctx, name)

		/*******************************************************************************/

		// z := zgate.NewGate()
		// z.NewEntryPoint("https")
		// z.EntryPoints["https"].Port(443).CertKey(CERTS_PATH, "z.com.cert.pem", "z.com.key.pem")

		// if err := c.Load(); err != nil {
		// 	log.Fatalln("Error:", err.Error())
		// }
		/*-------------------------------------------------*/
		// rp1 := reverseproxy.New(ctx, "http://localhost:8080")
		// rp2 := reverseproxy.New(ctx, "http://localhost:3001")
		// rp3 := reverseproxy.New(ctx, "http://localhost:3002")

		// router := router.NewRouter()
		// pr1 := router.NewHostRouter("keycloak.z.com")
		// pr1.Handler(rp1)
		// pr2 := router.NewHostRouter("app1.z.com")
		// pr2.Handler(rp2)
		// pr3 := router.NewHostRouter("app2.z.com")
		// pr3.Handler(rp3)

		// go func() {
		// 	s := server.New().Name("proxy1").Port(443).Router(router)
		// 	s.CertKey(CERTS_PATH, "z.com.cert.pem", "z.com.key.pem")
		// 	err := s.ListenAndServeTLS()
		// 	if err != nil {
		// 		log.Println(err.Error())
		// 	}
		// }()
		/*-------------------------------------------------*/
		helper.StartTestHTTPServer(3001, "app1")
		helper.StartTestHTTPServer(3002, "app2")

	})

}
