package main

import (
	"log"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	scribble "github.com/nanobox-io/golang-scribble"
)

type Webapp struct {
	Engine *gin.Engine
	Stream *SseStream
	DB     *scribble.Driver
}

// const _MaxLen = 8

func InitHttp(d *scribble.Driver) *Webapp {

	router := gin.Default()
	router.Use(cors.Default())

	wapp := &Webapp{
		Engine: router,
		DB:     d,
	}

	wapp.initApi()
	// wapp.initAuth()

	displayH := router.Group("/displayh")
	displayH.Static("/", "./static/displayh")

	displayV := router.Group("/displayv")
	displayV.Static("/", "./static/displayv")

	priority := router.Group("/priority")
	priority.Static("/", "./static/priority")

	mobile := router.Group("/mobile")
	mobile.Static("/", "./static/mobile")

	setup := router.Group("/setup")
	setup.Static("/", "./static/setup")

	draws := router.Group("/draws")
	draws.Static("/", "./static/draws")

	sapp := router.Group("/_app")
	sapp.Static("/", "./static/_app")

	static := router.Group("/static")
	static.Static("/", "./static/static")

	router.StaticFile("/", "./static/index.html")
	router.StaticFile("/favicon.png", "./static/favicon.png")

	router.ForwardedByClientIP = true
	router.SetTrustedProxies([]string{"127.0.0.1"})

	log.Printf("WebApp: %+v", wapp)

	return wapp
}
