package static

import (
	"embed"
	"fmt"
	"log"
	"net/http"
	"strings"

	"github.com/labstack/echo/v4"
)

// GetStatic returns static file content
func GetStatic(fs *embed.FS, c echo.Context) (err error) {
	var basedir = "static"
	var name = c.Param("*")
	if strings.HasSuffix(name, ".map") {
		return c.String(http.StatusNotFound, "")
	}

	cnt, err := fs.ReadFile(fmt.Sprintf("%s/%s", basedir, name))
	if err != nil {
		log.Println(err)
		return c.String(http.StatusNotFound, "")
	}

	if strings.HasSuffix(name, ".js") {
		c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJavaScript)
	} else if strings.HasSuffix(name, ".css") {
		c.Response().Header().Set(echo.HeaderContentType, "text/css")
	}
	c.Response().Header().Add("Cache-Control", "max-age=172800")
	return c.String(http.StatusOK, string(cnt))
}

// Static defines static file
type Static struct {
	Name    string
	Content string
}
