package controllers

import (
	"github.com/google/uuid"
	"github.com/unrolled/render"
	"moretcgshop/app/models"
	"net/http"
)

func (server *Server) Login(w http.ResponseWriter, r *http.Request) {
	renderer := render.New(render.Options{
		Layout:     "layout",
		Extensions: []string{".html", ".tmpl"},
	})

	_ = renderer.HTML(w, http.StatusOK, "login", map[string]interface{}{
		"error": GetFlash(w, r, "error"),
	})
}

func (server *Server) DoLogin(w http.ResponseWriter, r *http.Request) {
	email := r.FormValue("email")
	password := r.FormValue("password")

	userModel := models.User{}

	user, err := userModel.FindByEmail(server.DB, email)

	if err != nil {
		SetFlash(w, r, "error", "Email oder Password sind nicht korrekt")
		http.Redirect(w, r, "/login", http.StatusSeeOther)
		return
	}

	if !ComparePassword(password, user.Password) {
		SetFlash(w, r, "error", "Email oder Password sind nicht korrekt")
		http.Redirect(w, r, "/login", http.StatusSeeOther)
		return
	}

	session, _ := store.Get(r, sessionUser)
	session.Values["id"] = user.ID
	session.Save(r, w)
	http.Redirect(w, r, "/", http.StatusSeeOther)
}

func (server *Server) Register(w http.ResponseWriter, r *http.Request) {
	renderer := render.New(render.Options{
		Layout:     "layout",
		Extensions: []string{".html", ",tmpl"},
	})

	_ = renderer.HTML(w, http.StatusOK, "register", map[string]interface{}{
		"error": GetFlash(w, r, "error"),
	})
}

func (server *Server) DoRegister(w http.ResponseWriter, r *http.Request) {
	firstName := r.FormValue("first_name")
	lastName := r.FormValue("last_name")
	email := r.FormValue("email")
	password := r.FormValue("password")
	password2 := r.FormValue("password_repeat")

	if firstName == "" || lastName == "" || email == "" || password == "" || password2 == "" {
		SetFlash(w, r, "error", "First name, last name, email and password are required!")
		http.Redirect(w, r, "/register", http.StatusSeeOther)
		return
	}

	userModel := models.User{}
	existUser, _ := userModel.FindByEmail(server.DB, email)
	if existUser != nil {
		SetFlash(w, r, "error", "Sorry, diese E-Mail wurde bereits registriert")
		http.Redirect(w, r, "/register", http.StatusSeeOther)
		return
	}

	if password != password2 {
		SetFlash(w, r, "error", "Passwörter stimmen nicht überein")
		http.Redirect(w, r, "/register", http.StatusSeeOther)
		return
	}

	hashedPassword, _ := MakePassword(password)

	params := &models.User{
		ID:        uuid.New().String(),
		FirstName: firstName,
		LastName:  lastName,
		Email:     email,
		Password:  hashedPassword,
	}

	user, err := userModel.CreateUser(server.DB, params)
	if err != nil {
		SetFlash(w, r, "error", "Sorry Registrierung war nicht erfolgreich")
		http.Redirect(w, r, "/register", http.StatusSeeOther)
		return
	}

	session, _ := store.Get(r, sessionUser)
	session.Values["id"] = user.ID
	session.Save(r, w)
	http.Redirect(w, r, "/", http.StatusSeeOther)
}

func (server *Server) Logout(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, sessionUser)

	session.Values["id"] = nil
	session.Save(r, w)

	http.Redirect(w, r, "/", http.StatusSeeOther)
}
