package renderer

import (
	"encoding/json"
	"html/template"
	"net/http"
	groupstorage "git.coopgo.io/coopgo-platform/groups-management/storage"
	mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
	models "git.coopgo.io/coopgo-apps/parcoursmob/utils/models"

)

const journeysMenu = "journeys"

type BeneficiariesCovoiturage struct {
	Group         string `json:"group"`
	Count         int    `json:"count"`
	CacheId       string `json:"cache_id"`
	Beneficiaries []any  `json:"beneficiaries"`
}
type BeneficiariesCovoiturageA struct {
	Count         int    `json:"count"`
	CacheId       string `json:"cache_id"`
	Beneficiaries []any  `json:"beneficiaries"`
}


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

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

func (renderer *Renderer) JourneysSearch(w http.ResponseWriter, r *http.Request, carpools any, vehicles []any, searched bool, departure any, destination any, departuredate string, departuretime string, solidarity_results models.SolidarityTransport ) { //transitjourneys any,
	files := renderer.ThemeConfig.GetStringSlice("views.journeys.search.files")
	state := NewState(r, renderer.ThemeConfig, journeysMenu)
	state.ViewState = map[string]any{
		"searched":      searched,
		"departuredate": departuredate,
		"departuretime": departuretime,
		"departure":     departure,
		"destination":   destination,
		//"journeys":      transitjourneys,
		"carpools":      carpools,
		"vehicles":      vehicles,
		"solidarity_results": solidarity_results,
	}
	renderer.Render("journeys", w, r, files, state)
}

type BeneficiariesListstate struct {
	Count         int                  `json:"count"`
	CacheId       string               `json:"cache_id"`
	Beneficiaries []groupstorage.Group `json:"beneficiaries"`
}

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

func (s BeneficiariesListstate) JSONWithLimits(a int, b int) template.JS {
	if b < len(s.Beneficiaries) {
		s.Beneficiaries = s.Beneficiaries[a:b]
	}
	return s.JSON()
}
func (renderer *Renderer) GroupsGestion(w http.ResponseWriter, r *http.Request, groups []groupstorage.Group, cacheid string) {
	files := renderer.ThemeConfig.GetStringSlice("views.journeys.list.files")
	state := NewState(r, renderer.ThemeConfig, journeysMenu)

	state.ViewState = BeneficiariesListstate{
		Count:         len(groups),
		CacheId:       cacheid,
		Beneficiaries: groups,
	}

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

func (renderer *Renderer) CreateGroup(w http.ResponseWriter, r *http.Request, depart any, arrive any, searched bool, beneficiary any, beneficiaries []mobilityaccountsstorage.Account, departure any, destination any) {
	files := renderer.ThemeConfig.GetStringSlice("views.journeys.create.files")
	state := NewState(r, renderer.ThemeConfig, journeysMenu)
	viewstate := map[string]any{
		"deeparture":    depart,
		"deestination":  arrive,
		"beneficiaries": beneficiaries,
		"searched":      searched,
		"departure":     departure,
		"destination":   destination,
	}

	if searched {
		viewstate["search"] = map[string]any{
			"beneficiary": beneficiary,
		}

	}

	state.ViewState = viewstate
	renderer.Render("journeys", w, r, files, state)
}

func (renderer *Renderer) DisplayGroupCovoiturage(w http.ResponseWriter, r *http.Request, number string, groupid string, depart any, arrive any, accounts []any, cacheid string, searched bool, beneficiary any, group any, beneficiaries []mobilityaccountsstorage.Account, groups map[string]any) {
	files := renderer.ThemeConfig.GetStringSlice("views.journeys.display.files")
	state := NewState(r, renderer.ThemeConfig, journeysMenu)

	viewstate := map[string]any{
		"beneficiaries": beneficiaries,
		"searched":      searched,
		"group":         group,
		"deeparture":    depart,
		"deestination":  arrive,
		"groups":        groups,
		"ben":           accounts,
		"number":        number,
		"list": BeneficiariesCovoiturage{
			Group:         groupid,
			Count:         len(accounts),
			CacheId:       cacheid,
			Beneficiaries: accounts,
		},
	}

	if searched {
		viewstate["search"] = map[string]any{
			"beneficiary": beneficiary,
		}

	}
	state.ViewState = viewstate

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

func (renderer *Renderer) UpdateGroupCovoiturage(w http.ResponseWriter, r *http.Request, groupid string, memberid string) {
	files := renderer.ThemeConfig.GetStringSlice("views.journeys.update.files")
	state := NewState(r, renderer.ThemeConfig, journeysMenu)

	state.ViewState = map[string]any{
		"groupid":  groupid,
		"memberid": memberid,
	}
	renderer.Render("journeys", w, r, files, state)

}
