package renderer

import (
	"encoding/json"
	"html/template"
	"net/http"

	agendastorage "git.coopgo.io/coopgo-platform/agenda/storage"
	fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
	mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
)

const administrationMenu = "administration"

func (renderer *Renderer) Administration(w http.ResponseWriter, r *http.Request, accounts any, beneficiaries any, groups any, bookings any, events []agendastorage.Event) {
	files := renderer.ThemeConfig.GetStringSlice("views.administration.home.files")
	state := NewState(r, renderer.ThemeConfig, administrationMenu)
	state.ViewState = map[string]any{
		"accounts":      accounts,
		"beneficiaries": beneficiaries,
		"bookings":      bookings,
		"groups":        groups,
		"events":        events,
	}

	renderer.Render("administration", w, r, files, state)
}

func (renderer *Renderer) AdministrationCreateGroup(w http.ResponseWriter, r *http.Request) {
	files := renderer.ThemeConfig.GetStringSlice("views.administration.create_group.files")
	state := NewState(r, renderer.ThemeConfig, administrationMenu)

	renderer.Render("administration", w, r, files, state)
}

func (renderer *Renderer) AdministrationGroupDisplay(w http.ResponseWriter, r *http.Request, group any, groupmembers []mobilityaccountsstorage.Account, admins []mobilityaccountsstorage.Account) {
	files := renderer.ThemeConfig.GetStringSlice("views.administration.display_group.files")
	state := NewState(r, renderer.ThemeConfig, administrationMenu)
	state.ViewState = map[string]any{
		"group":   group,
		"members": groupmembers,
		"admins":  admins,
	}

	renderer.Render("administration", w, r, files, state)
}

type BeneficiariesState struct {
	Count         int                               `json:"count"`
	CacheId       string                            `json:"cache_id"`
	Beneficiaries []mobilityaccountsstorage.Account `json:"beneficiaries"`
}

func (s BeneficiariesState) JSON() template.JS {
	result, _ := json.Marshal(s)
	return template.JS(result)
}

func (s BeneficiariesState) JSONWithLimits(a int, b int) template.JS {
	if b < len(s.Beneficiaries) {
		s.Beneficiaries = s.Beneficiaries[a:b]
	}
	return s.JSON()
}

func (renderer *Renderer) AdminStatBeneficaires(w http.ResponseWriter, r *http.Request, Beneficiaries []mobilityaccountsstorage.Account, cacheid string) {
	files := renderer.ThemeConfig.GetStringSlice("views.administration.beneficaires_list.files")

	state := NewState(r, renderer.ThemeConfig, administrationMenu)
	state.ViewState = BeneficiariesState{
		Count:         len(Beneficiaries),
		CacheId:       cacheid,
		Beneficiaries: Beneficiaries,
	}

	renderer.Render("beneficiaries_State", w, r, files, state)
}

func (renderer *Renderer) AdminStatEvents(w http.ResponseWriter, r *http.Request, events []agendastorage.Event, groups map[string]any) {
	files := renderer.ThemeConfig.GetStringSlice("views.administration.events_list.files")

	state := NewState(r, renderer.ThemeConfig, administrationMenu)
	state.ViewState = map[string]any{
		"events": events,
		"groups": groups,
	}

	renderer.Render("beneficiaries_State", w, r, files, state)
}

func (renderer *Renderer) AdminStatVehicles(w http.ResponseWriter, r *http.Request, vehicles []fleetsstorage.Vehicle, bookings []fleetsstorage.Booking, admingroups map[string]any) {
	files := renderer.ThemeConfig.GetStringSlice("views.administration.vehicles_list.files")
	state := NewState(r, renderer.ThemeConfig, administrationMenu)
	state.ViewState = map[string]any{
		"vehicles":    vehicles,
		"bookings":    bookings,
		"admingroups": admingroups,
	}

	renderer.Render("vehicles_state", w, r, files, state)
}

func (renderer *Renderer) AdminStatBookings(w http.ResponseWriter, r *http.Request, vehicles map[string]fleetsstorage.Vehicle, bookings []fleetsstorage.Booking, admingroups map[string]any, beneficiaries map[string]any) {
	files := renderer.ThemeConfig.GetStringSlice("views.administration.bookings_list.files")
	state := NewState(r, renderer.ThemeConfig, administrationMenu)
	state.ViewState = map[string]any{
		"vehicles_map":      vehicles,
		"bookings":          bookings,
		"admingroups":       admingroups,
		"beneficiaries_map": beneficiaries,
	}

	renderer.Render("bookings_stats", w, r, files, state)
}
