package handler

import (
	"github.com/gorilla/csrf"
	"net/http"
	"status/model"
)

type Update struct {
	UpdatedAgo string
	Author     string
}

func (h *Handler) showIndexView(w http.ResponseWriter, r *http.Request) {
	protectClickJacking(w)
	user, _ := h.sess.Get(r)
	statuses, err := h.storage.LatestStatuses()
	if err != nil {
		serverError(w, err)
		return
	}
	session, err := h.sess.Store.Get(r, "status")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	flash := ""
	if flashes := session.Flashes(); len(flashes) > 0 {
		flash = flashes[0].(string)
	}
	session.Save(r, w)
	h.renderLayout(w, "index", map[string]interface{}{
		"statuses":       statuses,
		"flash":          flash,
		"status":         &model.Status{},
		csrf.TemplateTag: csrf.TemplateField(r),
	}, user)
}
