package webserver

import (
	"bytes"
	"strings"
	"time"

	"encoding/json"
	"net/http"
	"text/template"

	"istheinternetonfire.app/assets"
	"istheinternetonfire.app/internal/cisa"
	"istheinternetonfire.app/internal/config"
)

type webErrStruct struct {
	Error    bool   `json:"error"         yaml:"error"`
	ErrorMsg string `json:"error_message" yaml:"errorMessage"`
}

func tmpltError(w http.ResponseWriter, serverStatus int, message string) {
	var (
		output []byte
		o      = webErrStruct{
			Error:    true,
			ErrorMsg: message,
		}
		err error
	)

	w.Header().Add("Content-Type", "application/json")
	output, err = json.MarshalIndent(o, "", "  ")
	if err != nil {
		config.Cfg.Log.Warn("marshal error", "error", err)
		w.WriteHeader(serverStatus)
		w.Write(output) //nolint:errcheck
	}
}

func tmpltWebRoot(w http.ResponseWriter, r *http.Request) {
	tmplt, err := template.New("index.tplt").Funcs(template.FuncMap{
		"ToUpper": strings.ToUpper,
	}).ParseFS(
		assets.EmbedHTML,
		"html/index.tplt",
		"html/css/style.css",
	)
	if err != nil {
		config.Cfg.Log.Debug("unable to parse html template", "error", err)
		tmpltError(w, http.StatusInternalServerError, "Template Parse Error.")
		return
	}

	var (
		msgBuffer bytes.Buffer
		cves      []cisa.VulStruct
	)

	c := cisa.Read()
	for _, i := range c.Vulnerabilities {
		t, _ := time.Parse("2006-01-02", i.DateAdded)
		if t.After(time.Now().Add(-time.Hour * 720)) {
			cves = append(cves, i)
		}
	}

	if err := tmplt.Execute(&msgBuffer, struct {
		CVEs []cisa.VulStruct
	}{
		CVEs: cves[len(cves)-3:],
	}); err != nil {
		config.Cfg.Log.Debug("unable to execute html template", err)
		tmpltError(w, http.StatusInternalServerError, "Template Parse Error.")
		return
	}

	w.Write(msgBuffer.Bytes()) //nolint:errcheck
}

func tmpltStatusNotFound(w http.ResponseWriter, path string) {
	tmplt, err := template.ParseFS(assets.EmbedHTML, "html/file-not-found.tplt")
	if err != nil {
		config.Cfg.Log.Debug("unable to parse html template", err)
		tmpltError(w, http.StatusInternalServerError, "Template Parse Error.")
		return
	}

	var msgBuffer bytes.Buffer
	if err := tmplt.Execute(&msgBuffer, struct {
		Title     string
		ErrorCode int
	}{
		Title:     path,
		ErrorCode: http.StatusNotFound,
	}); err != nil {
		config.Cfg.Log.Debug("unable to execute html template", err)
		tmpltError(w, http.StatusInternalServerError, "Template Parse Error.")
		return
	}
	w.Write(msgBuffer.Bytes()) //nolint:errcheck
}