package handlers

import (
	"net/http"

	"git.haelnorr.com/h/oslstats/internal/view/page"
	"github.com/pkg/errors"

	"git.haelnorr.com/h/golib/hws"
)

// Handles responses to the / path. Also serves a 404 Page for paths that
// don't have explicit handlers
func Test(server *hws.Server) http.Handler {
	return http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			if r.Method == "GET" {
				page.Test().Render(r.Context(), w)
			} else {
				r.ParseForm()
				notifytype := r.Form.Get("type")
				title := r.Form.Get("title")
				message := r.Form.Get("message")
				switch notifytype {
				case "error":
					err := errors.New(message)
					notifyInternalServiceError(server, w, title, err)
					return
				case "warn":
					notifyWarning(w, title, message)
					return
				case "info":
					notifyInfo(w, title, message)
					return
				case "success":
					notifySuccess(w, title, message)
					return
				}
			}
		},
	)
}
