package http

import (
	"net/http"
	"strconv"
	"strings"

	"git.arav.su/Arav/httpr"
	captcha "git.arav.su/Arav/justcaptcha/v2"
	"git.arav.su/Arav/justcaptcha/v2/inmemdb"
	"git.arav.su/Arav/justguestbook"
)

type GuestbookApiHandlers struct {
	Owner    string
	PageSize int64
	db       justguestbook.Guestbook
}

func NewGuestbookApiHandlers(owner string, pageSz int64, db justguestbook.Guestbook) *GuestbookApiHandlers {
	return &GuestbookApiHandlers{Owner: owner, PageSize: pageSz, db: db}
}

func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) {
	var entry *justguestbook.Entry
	var err error

	if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
		r.ParseForm()

		if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) {
			Error(w, http.StatusForbidden, "Wrong answer given.",
				"Here's your message:"+r.FormValue("message"), r.Referer())
			return
		}

		if r.FormValue("name") == "" {
			r.Form.Set("name", "Anonymous")
		}

		message := cleanupNewlines(r.FormValue("message"))

		entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
			r.FormValue("website"), r.FormValue("hide_website") != "")
		if err != nil {
			Error(w, http.StatusInternalServerError, err.Error(),
				"Here's your message:"+r.FormValue("message"), r.Referer())
			return
		}
	}

	if err = h.db.NewEntry(entry); err != nil {
		Error(w, http.StatusInternalServerError, err.Error(),
			"Here's your message:"+r.FormValue("message"), r.Referer())
		return
	}

	http.Redirect(w, r, "/guestbook", http.StatusMovedPermanently)
}

func (h *GuestbookApiHandlers) Edit(w http.ResponseWriter, r *http.Request) {
	var entry *justguestbook.Entry
	var err error

	if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
		r.ParseForm()

		if r.FormValue("name") == "" {
			r.Form.Set("name", "Anonymous")
		}

		message := cleanupNewlines(r.FormValue("message"))

		entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
			r.FormValue("website"), r.FormValue("hide_website") != "")
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		entry.ID, _ = strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
	}

	if err = h.db.EditEntry(entry); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

}

func (h *GuestbookApiHandlers) Delete(w http.ResponseWriter, r *http.Request) {
	id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
	if err := h.db.DeleteEntry(id); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func (h *GuestbookApiHandlers) Reply(w http.ResponseWriter, r *http.Request) {
	var reply *justguestbook.Reply
	var err error

	if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
		r.ParseForm()

		id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)

		message := cleanupNewlines(r.FormValue("message"))

		reply, err = justguestbook.NewReply(id, message)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	if err := h.db.NewReply(reply); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func (h *GuestbookApiHandlers) EditReply(w http.ResponseWriter, r *http.Request) {
	var reply *justguestbook.Reply
	var err error

	if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
		r.ParseForm()

		id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)

		message := cleanupNewlines(r.FormValue("message"))

		reply, err = justguestbook.NewReply(id, message)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}

	if err := h.db.EditReply(reply); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}

func (h *GuestbookApiHandlers) DeleteReply(w http.ResponseWriter, r *http.Request) {
	id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
	if err := h.db.DeleteReply(id); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
