package web

import (
	"net/http"
)

type NotFoundIndexResponseWriter struct {
	http.ResponseWriter
	status int
}

func (nfr *NotFoundIndexResponseWriter) WriteHeader(status int) {
	nfr.status = status
	if status != http.StatusNotFound {
		nfr.ResponseWriter.WriteHeader(status)
	}
}

func (nfr *NotFoundIndexResponseWriter) Write(p []byte) (int, error) {
	if nfr.status != http.StatusNotFound {
		return nfr.ResponseWriter.Write(p)
	}

	nfr.Header().Add("Content-Type", "text/html; charset=utf-8")
	return nfr.ResponseWriter.Write(indexBytes)
}

type StaticFSHandler struct {
	Handler http.Handler
}

func (s StaticFSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	srw := &NotFoundIndexResponseWriter{ResponseWriter: w}
	s.Handler.ServeHTTP(srw, r)
}
