package view

import (
	"net/http"
	"strconv"

	"xengineering.eu/ceres/model"

	"github.com/gorilla/mux"
)

func Recipe(w http.ResponseWriter, r *http.Request) {
	id_str := mux.Vars(r)[`id`]
	id, _ := strconv.Atoi(id_str)

	recipe := model.Recipe{}
	recipe.Id = int64(id)

	err := recipe.Read()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	tmpl := "recipe"
	edit, ok := r.URL.Query()["edit"]
	if ok && len(edit) == 1 && edit[0] == "true" {
		tmpl = "recipe-edit"
	}

	err = html.ExecuteTemplate(w, tmpl, recipe)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}