package main

import (
	"brainminder.speedtech.it/assets"
	"github.com/alexedwards/flow"
	"net/http"
)

func (app *application) routes() http.Handler {
	mux := flow.New()
	mux.NotFound = http.HandlerFunc(app.notFound)

	mux.Use(app.logAccess)
	mux.Use(app.recoverPanic)
	mux.Use(app.securityHeaders)

	fileServer := http.FileServer(http.FS(assets.EmbeddedFiles))

	mux.Handle("/static/...", fileServer, "GET")

	mux.Group(func(mux *flow.Mux) {
		mux.Use(app.preventCSRF)
		mux.Use(app.authenticate)

		mux.Group(func(mux *flow.Mux) {
			mux.Use(app.requireAnonymousUser)
			mux.HandleFunc("/login", app.login, "GET", "POST")
			mux.HandleFunc("/item/shared/:token", app.itemShared, "GET", "POST")
		})

		mux.Group(func(mux *flow.Mux) {
			mux.Use(app.requireAuthenticatedUser)
			mux.HandleFunc("/logout", app.logout, "POST", "GET")
			// To move
			mux.HandleFunc("/signup", app.signup, "GET", "POST")
			mux.HandleFunc("/forgotten-password", app.forgottenPassword, "GET", "POST")
			mux.HandleFunc("/forgotten-password-confirmation", app.forgottenPasswordConfirmation, "GET")
			mux.HandleFunc("/password-reset/:plaintextToken", app.passwordReset, "GET", "POST")
			mux.HandleFunc("/password-reset-confirmation", app.passwordResetConfirmation, "GET")
			//End to move

			mux.HandleFunc("/", app.home, "GET", "POST")
			mux.HandleFunc("/item/relation/add/:related_item_id", app.itemsRelationAdd, "GET")
			mux.HandleFunc("/items", app.items, "GET", "POST")
			mux.HandleFunc("/items/search", app.items, "GET")
			mux.HandleFunc("/items/search", app.itemsSearch, "POST")
			mux.HandleFunc("/items/search-for-relations/:item_id", app.itemsSearchForRelations, "GET")
			mux.HandleFunc("/items/type/:type_id", app.itemsType, "GET", "POST")
			mux.HandleFunc("/item/create/:type_id", app.itemCreate, "GET", "POST")
			mux.HandleFunc("/item/read/:item_id", app.itemRead, "GET")
			mux.HandleFunc("/item/view/:item_id", app.itemView, "GET")
			mux.HandleFunc("/item/update/:item_id", app.itemUpdate, "GET", "POST")
			mux.HandleFunc("/item/delete/:item_id", app.itemDelete, "DELETE")
			mux.HandleFunc("/item/share/:item_id", app.itemShare, "GET", "POST")
			mux.HandleFunc("/item/share/edit/:id", app.itemShareEdit, "GET", "POST")
			mux.HandleFunc("/item/add-to-dashboard/:item_id", app.itemAddToDashboard, "POST")
			mux.HandleFunc("/item/remove-from-dashboard/:item_id", app.itemRemoveFromDashboard, "POST", "DELETE")

			mux.HandleFunc("/types", app.types, "GET")
			mux.HandleFunc("/type/create", app.typeCreate, "GET", "POST")
			mux.HandleFunc("/type/update/:type_id", app.typeUpdate, "GET", "POST")
			mux.HandleFunc("/type/delete/:type_id", app.typeDelete, "DELETE")
			mux.HandleFunc("/type/field-new", app.typeFieldNew, "GET")

			mux.HandleFunc("/categories", app.categories, "GET")
			mux.HandleFunc("/category/create", app.categoryCreate, "GET", "POST")
			mux.HandleFunc("/category/update/:category_id", app.categoryUpdate, "GET", "POST")
			mux.HandleFunc("/category/delete/:category_id", app.categoryDelete, "DELETE")

			mux.HandleFunc("/notebooks", app.notebooks, "GET")
			mux.HandleFunc("/notebook/create", app.notebookCreate, "GET", "POST")
			mux.HandleFunc("/notebook/update/:notebook_id", app.notebookUpdate, "GET", "POST")
			mux.HandleFunc("/notebook/delete/:notebook_id", app.notebookDelete, "DELETE")

			mux.HandleFunc("/quickbox/update/:quicknote_id", app.quickboxUpdate, "GET", "POST")
			mux.HandleFunc("/quickbox/delete/:quicknote_id", app.quickboxDelete, "DELETE")
			mux.HandleFunc("/quickbox/transform/:quicknote_id", app.quickboxTransform, "GET", "POST")
			mux.HandleFunc("/quickbox/add", app.quickboxAdd, "POST")
			mux.HandleFunc("/quickbox/all", app.quickboxAll, "GET")
		})
	})

	return mux
}
