package web

import (
	"fmt"
	"log"
	"net/http/httputil"
	"net/url"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"

	"git.jmbit.de/jmb/podterminal/pods"
)

func createReverseProxy(backendService string) (*httputil.ReverseProxy, error) {
	var err error
	log.Println("Creating reverse Proxy for ", backendService)

	backendURL, err := url.Parse(backendService)
	if err != nil {
		log.Printf("Could not parse backend URL: %v", err)
	}

	proxy := &httputil.ReverseProxy{
		Rewrite: func(request *httputil.ProxyRequest) {
			request.SetURL(backendURL)
			request.Out.Host = request.In.Host
		},
	}

	return proxy, err
}

func containerProxy(c *gin.Context) {
	session := sessions.Default(c)
	session.Save()
	sessionID := session.ID()
	if session.Get("ct") == nil {
		log.Println("Creating Container for Session ", sessionID)
		ct, err := pods.CreateContainer()
		if err != nil {
			c.HTML(500, "Error", fmt.Sprintf("[%s] Could not create Container: %v", sessionID, err))
			c.Abort()
		}
		err = pods.StartContainer(ct)
		if err != nil {
			c.HTML(500, "Error", fmt.Sprintf("[%s] Could not start Container: %v", sessionID, err))
			c.Abort()
		}
		// Hack to wait for Container to start up and get assigned an IP
		time.Sleep(3 * time.Second)
		ctip, err := pods.GetContainerIP(ct)

		if err != nil {
			c.HTML(500, "Error", fmt.Sprintf("[%s] Could not get Container ip: %v", sessionID, err))
			c.Abort()
		}

		// Soft fail Skel
		_ = pods.CopySkelToContainer(ct)

		proxies[ct], err = createReverseProxy(fmt.Sprintf("http://%s:3000", ctip))

		if err != nil {
			c.HTML(
				500,
				"Error",
				fmt.Sprintf("[%s] Could not create Container Proxy: %v", sessionID, err),
			)
			c.Abort()
		}
		session.Set("ct", ct)
		session.Save()
		c.Redirect(301, "/")
	} else {
		sessionCT := session.Get("ct")
		switch sessionCT.(type) {
		case string:

		default:
			c.HTML(500, "Error", "Session Container ID is not a string")
			c.Abort()
		}
		id := session.Get("ct").(string)
		proxy := proxies[id]
		proxy.ServeHTTP(c.Writer, c.Request)
	}

}
