package ui

import (
	_ "embed"
	"net/http"
	"strings"
)

//go:embed LICENSE
var license string

//go:embed LICENSE.logo
var licenseLogo string

var licenseHtml string
var licenseLogoHtml string

func textToHtml(text string) string {
	return strings.ReplaceAll(text, "\n", "<br/>")
}

func init() {
	licenseHtml = textToHtml(license)
	licenseLogoHtml = textToHtml(licenseLogo)
}

func PageHandler(w http.ResponseWriter, r *http.Request) {
	access := getConvergeAccess(r)

	switch r.URL.Path {
	case "":
		fallthrough
	case "/":
		fallthrough
	case "index.html":
		AboutTab().Render(r.Context(), w)
		// TODO use contexts later.
	case "usage.html":
		UsageTab(access).Render(r.Context(), w)
	case "downloads.html":
		DownloadsTab().Render(r.Context(), w)
	case "sessions.html":
		SessionsTab(nil, nil, access.Location).Render(r.Context(), w)
	case "license.html":
		LicenseTab(licenseHtml, licenseLogoHtml).Render(r.Context(), w)
	default:
		http.NotFound(w, r)
	}
}