package renderer

import (
	"net/http"

	filestorage "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
	"git.coopgo.io/coopgo-platform/fleets/storage"
	mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
)

const vehiclesMenu = "vehicles"

func selectDocumentsDefaults(beneficiarydocuments []filestorage.FileInfo, mandatory_documents []string) map[string]string {
	res := map[string]string{}
	for _, v := range mandatory_documents {
		for _, d := range beneficiarydocuments {
			if d.Metadata["Type"] == v {
				res[v] = d.Key
			}
		}
	}
	return res
}

func (renderer *Renderer) VehiclesSearch(w http.ResponseWriter, r *http.Request, beneficiaries []mobilityaccountsstorage.Account, searched bool, vehicles []any, beneficiary any, startdate any, enddate any, mandatory_documents []string, file_types_map map[string]string, beneficiarydocuments []filestorage.FileInfo, selected_type string, automatic bool, vehicles_types []string, admingroups map[string]any) {
	files := renderer.ThemeConfig.GetStringSlice("views.vehicles.search.files")
	state := NewState(r, renderer.ThemeConfig, vehiclesMenu)
	viewstate := map[string]any{
		"beneficiaries":  beneficiaries,
		"searched":       searched,
		"vehicles_types": vehicles_types,
	}

	if searched {
		viewstate["search"] = map[string]any{
			"startdate":             startdate,
			"enddate":               enddate,
			"vehicles":              vehicles,
			"beneficiary":           beneficiary,
			"mandatory_documents":   mandatory_documents,
			"file_types_map":        file_types_map,
			"beneficiary_documents": beneficiarydocuments,
			"selected_type":         selected_type,
			"automatic":             automatic,
			"admingroups":           admingroups,
			"documents_defaults":    selectDocumentsDefaults(beneficiarydocuments, mandatory_documents),
		}
	}

	state.ViewState = viewstate

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

func (renderer *Renderer) VehicleBookingDisplay(w http.ResponseWriter, r *http.Request, booking any, vehicle any, beneficiary any, group any, documents []filestorage.FileInfo, file_types_map map[string]string) {
	files := renderer.ThemeConfig.GetStringSlice("views.vehicles.booking_display.files")
	state := NewState(r, renderer.ThemeConfig, vehiclesMenu)
	state.ViewState = map[string]any{
		"booking":        booking,
		"vehicle":        vehicle,
		"beneficiary":    beneficiary,
		"group":          group,
		"documents":      documents,
		"file_types_map": file_types_map,
	}

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

func (renderer *Renderer) VehicleBookingsList(w http.ResponseWriter, r *http.Request, bookings []storage.Booking, vehiclesMap any, groupsMap any) {
	files := renderer.ThemeConfig.GetStringSlice("views.vehicles.bookings_list.files")
	state := NewState(r, renderer.ThemeConfig, vehiclesMenu)
	state.ViewState = map[string]any{
		"bookings":     bookings,
		"vehicles_map": vehiclesMap,
		"groups_map":   groupsMap,
	}

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

// func (renderer *Renderer) VehicleUnbookingsList(w http.ResponseWriter, r *http.Request, bookings []storage.Booking, vehiclesMap any, groupsMap any) {
// 	files := renderer.ThemeConfig.GetStringSlice("views.vehicles.bookings_list.files")
// 	state := NewState(r, renderer.ThemeConfig, vehiclesMenu)
// 	state.ViewState = map[string]any{
// 		"bookings":     bookings,
// 		"vehicles_map": vehiclesMap,
// 		"groups_map":   groupsMap,
// 	}

// 	renderer.Render("vehicles search", w, r, files, state)
// }
