/*
 * SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package app

import (
	"net/http"

	"dev.tandem.ws/tandem/camper/pkg/amenity"
	"dev.tandem.ws/tandem/camper/pkg/auth"
	"dev.tandem.ws/tandem/camper/pkg/booking"
	"dev.tandem.ws/tandem/camper/pkg/campsite"
	"dev.tandem.ws/tandem/camper/pkg/database"
	"dev.tandem.ws/tandem/camper/pkg/home"
	httplib "dev.tandem.ws/tandem/camper/pkg/http"
	"dev.tandem.ws/tandem/camper/pkg/legal"
	"dev.tandem.ws/tandem/camper/pkg/location"
	"dev.tandem.ws/tandem/camper/pkg/payment"
	"dev.tandem.ws/tandem/camper/pkg/services"
	"dev.tandem.ws/tandem/camper/pkg/surroundings"
	"dev.tandem.ws/tandem/camper/pkg/template"
)

type publicHandler struct {
	home         *home.PublicHandler
	amenity      *amenity.PublicHandler
	booking      *booking.PublicHandler
	campsite     *campsite.PublicHandler
	legal        *legal.PublicHandler
	location     *location.PublicHandler
	payment      *payment.PublicHandler
	services     *services.PublicHandler
	surroundings *surroundings.PublicHandler
}

func newPublicHandler() *publicHandler {
	return &publicHandler{
		home:         home.NewPublicHandler(),
		amenity:      amenity.NewPublicHandler(),
		booking:      booking.NewPublicHandler(),
		campsite:     campsite.NewPublicHandler(),
		legal:        legal.NewPublicHandler(),
		location:     location.NewPublicHandler(),
		payment:      payment.NewPublicHandler(),
		services:     services.NewPublicHandler(),
		surroundings: surroundings.NewPublicHandler(),
	}
}

func (h *publicHandler) Handler(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var head string
		head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
		switch head {
		case "":
			h.home.Handler(user, company, conn).ServeHTTP(w, r)
		case "amenities":
			h.amenity.Handler(user, company, conn).ServeHTTP(w, r)
		case "booking":
			h.booking.Handler(user, company, conn).ServeHTTP(w, r)
		case "campground":
			campgroundHandler(user, company, conn).ServeHTTP(w, r)
		case "campsites":
			h.campsite.Handler(user, company, conn).ServeHTTP(w, r)
		case "legal":
			h.legal.Handler(user, company, conn).ServeHTTP(w, r)
		case "location":
			h.location.Handler(user, company, conn).ServeHTTP(w, r)
		case "payments":
			h.payment.Handler(user, company, conn).ServeHTTP(w, r)
		case "services":
			h.services.Handler(user, company, conn).ServeHTTP(w, r)
		case "surroundings":
			h.surroundings.Handler(user, company, conn).ServeHTTP(w, r)
		default:
			http.NotFound(w, r)
		}
	})
}

func campgroundHandler(user *auth.User, company *auth.Company, conn *database.Conn) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var head string
		head, r.URL.Path = httplib.ShiftPath(r.URL.Path)
		switch head {
		case "":
			switch r.Method {
			case http.MethodGet:
				page := template.NewPublicPage()
				page.Setup(r, user, company, conn)
				template.MustRenderPublicFiles(w, r, user, company, page, "campground.gohtml", "web/templates/campground_map.svg")
			default:
				httplib.MethodNotAllowed(w, r, http.MethodGet)
			}
		default:
			http.NotFound(w, r)
		}
	})
}
