package handler

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

func (h *Handler) showKeyView(w http.ResponseWriter, r *http.Request) {
	protectClickJacking(w)
	username, err := h.getUser(r)
	if err != nil {
		unauthorized(w, r)
		return
	}
	user, err := h.storage.UserByName(username)
	if err != nil {
		unauthorized(w, r)
		return
	}
	key := h.vpub.FindOrCreateKey(user.Name)
	h.renderLayout(w, "forum-key", map[string]interface{}{
		"key": key,
	}, username)
}

func (h *Handler) showSettingsView(w http.ResponseWriter, r *http.Request) {
	protectClickJacking(w)
	username, err := h.getUser(r)
	if err != nil {
		unauthorized(w, r)
		return
	}
	user, err := h.storage.UserByName(username)
	if err != nil {
		unauthorized(w, r)
		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, "settings", map[string]interface{}{
		"flash":          flash,
		"User":           user,
		csrf.TemplateTag: csrf.TemplateField(r),
	}, username)
}
