package main

import (
	"errors"
	"fmt"
	"html/template"
	"net/http"
	"strconv"
	"sync"

	"github.com/go-kit/kit/log/level"

	"git.c3pb.de/gbe/invinoveritas/auth"
	"git.c3pb.de/gbe/invinoveritas/log"
	"git.c3pb.de/gbe/invinoveritas/vino"
)

func (h Handler) index(write WriteStorage, read ReadStorage) http.Handler {
	var (
		once sync.Once
		tpl  *template.Template
	)

	type templateData struct {
		Wines []vino.Vino
		User  auth.User
	}

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		once.Do(func() {
			tpl = template.Must(template.ParseFS(templateFS, "templates/base.tpl", "templates/index.tpl"))
		})

		if r.Method == "GET" {
			wines, err := read.ListWines(r.Context())
			if err != nil {
				httpError(w, r, "can't list wines", err, http.StatusInternalServerError)
				return
			}

			w.Header().Add("content-type", "text/html")

			data := templateData{
				Wines: wines,
				User:  auth.Get(r),
			}

			err = tpl.ExecuteTemplate(w, "index.tpl", data)
			if err != nil {
				level.Error(log.Get(r)).
					Log("error", err,
						"msg", "can't execute index template")
			}

			return
		}

		if r.Method != "POST" {
			httpError(w, r, "invalid method", errors.New(r.Method), http.StatusMethodNotAllowed)
			return
		}

		name := r.FormValue("name")
		if len(name) > 80 || len(name) == 0 {
			httpError(w, r, "bad name", errors.New("name must be between 1 and 80 chars"), http.StatusBadRequest)
			return
		}

		ratingVal := r.FormValue("rating")

		var (
			rating int
			err    error
		)

		if ratingVal != "" {
			rating, err = strconv.Atoi(ratingVal)
			if err != nil {
				httpError(w, r, "can't convert rating", err, http.StatusBadRequest)
				return
			}
		}

		err = r.ParseMultipartForm(16 * 1024 * 1024)
		if err != nil {
			level.Warn(log.Get(r)).Log("error", err, "msg", "can't parse multipart form data")
		}

		vino := vino.Vino{
			Name:   name,
			Rating: rating,
		}

		// See if there's an image file uploaded
		if r.MultipartForm != nil && len(r.MultipartForm.File["picture"]) != 0 {
			picFile, picHdr, err := r.FormFile("picture")
			if err != nil {
				httpError(w, r, "can't open picture file", err, http.StatusInternalServerError)
				return
			}
			defer picFile.Close()

			err = vino.AddPicture(picFile, picHdr.Header.Get("Content-Type"))
			if err != nil {
				httpError(w, r, "can't load picture", err, http.StatusBadRequest)
				return
			}
		}

		err = write.Store(r.Context(), &vino)
		if err != nil {
			httpError(w, r, fmt.Sprintf("can't store wine %q", vino), err, http.StatusInternalServerError)
			return
		}

		http.Redirect(w, r, "/details?id="+strconv.Itoa(vino.ID), http.StatusSeeOther) // TODO: Is this the correct status?
	})
}