package renderer

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

	mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
	solidarity_service "git.coopgo.io/sbouaram/solidarity-service/servers/grpc/proto"
)

const solidarityserviceMenu = "solidarity_service"


type SolidarityListState struct {
	Count         int                               `json:"count"`
  	CacheId                string                            `json:"cache_id"`
	BeneficiariesSolidarity         *solidarity_service.GetAllPassengersResponse `json:"beneficiariessolidarity"`
	Drivers *solidarity_service.DriverJourneysResponse `json:"drivers"`
	Beneficiaries []mobilityaccountsstorage.Account   `json:"beneficiaries"`

}

type BeneficiariesSolidarityListState struct {
	Count                      int                               `json:"count"`
  	CacheId                    string                            `json:"cache_id"`
	BeneficiariesSolidarity    *solidarity_service.GetAllPassengersResponse `json:"beneficiariessolidarity"`
	Beneficiaries              []mobilityaccountsstorage.Account   `json:"beneficiaries"`
	Drivers                    *solidarity_service.GetAllDriversResponse `json:"drivers"`

}

type SolidarityBookingsState struct {
	Bookings *solidarity_service.CreateBookingSolidarityResponse `json:"bookings"`
	Beneficiaries []mobilityaccountsstorage.Account   `json:"beneficiaries"`
	BeneficiariesSolidarity          *solidarity_service.GetAllPassengersResponse `json:"beneficiariessolidarity"`

}

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

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

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

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

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

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

func (renderer *Renderer) SolidarityService(w http.ResponseWriter, r *http.Request) {
	files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.create.files")
	state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu)

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

func (renderer *Renderer) BeneficiariesSolidarity(w http.ResponseWriter, r *http.Request, accounts *solidarity_service.GetAllPassengersResponse, drivers *solidarity_service.GetAllDriversResponse, parcourmobAccounts []mobilityaccountsstorage.Account, cacheid string) {
	files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.list.files")
	state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu)
	state.ViewState = BeneficiariesSolidarityListState{
		Count:         len(parcourmobAccounts), 
		CacheId:       cacheid,
		Beneficiaries: parcourmobAccounts,
		BeneficiariesSolidarity: accounts,
		Drivers: drivers,

	}

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

func (renderer *Renderer) SolidarityServiceBooking(w http.ResponseWriter, r *http.Request, bookings *solidarity_service.CreateBookingSolidarityResponse) {
	files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.create.files")
	state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu)
	state.ViewState = SolidarityBookingsState{
		Bookings: bookings,
	}

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

func (renderer *Renderer) SolidarityServiceListAvailableDrivers(w http.ResponseWriter, r *http.Request, drivers *solidarity_service.DriverJourneysResponse, booking *solidarity_service.CreateBookingSolidarityRequest) {
	files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.create.files")
	state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu)
	state.ViewState = SolidarityListState {
		Drivers: drivers,
	}

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

func (renderer *Renderer) CreateDriver(w http.ResponseWriter, r *http.Request) {
	files := renderer.ThemeConfig.GetStringSlice("views.solidarity_service.create_driver.files")
	state := NewState(r, renderer.ThemeConfig, solidarityserviceMenu)

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