package api

import (
	"github.com/gorilla/mux"
	"source.gleipnir.technology/Gleipnir/nidus-sync/auth"
	"source.gleipnir.technology/Gleipnir/nidus-sync/platform/file"
	"source.gleipnir.technology/Gleipnir/nidus-sync/resource"
)

func AddRoutesRMO(r *mux.Router) {
	router := resource.NewRouter(r)

	compliance_request := resource.ComplianceRequest(router)
	district := resource.District(router)
	geocode := resource.Geocode(router)
	nuisance := resource.Nuisance(router)
	pr_compliance := resource.PublicReportCompliance(router)
	publicreport := resource.Publicreport(router)
	publicreport_notification := resource.PublicreportNotification(router)
	qrcode := resource.QRCode(router)
	water := resource.Water(router)

	r.HandleFunc("", handlerJSON(getRoot))
	r.HandleFunc("/compliance-request/image/pool/{public_id}", compliance_request.ImagePoolGet).Methods("GET").Name("compliance-request.image.pool.ByIDGet")
	r.Handle("/district", handlerJSONSlice(district.List)).Methods("GET")
	r.Handle("/district/{id}", handlerJSON(district.GetByID)).Methods("GET").Name("district.ByIDGet")
	r.HandleFunc("/district/{slug}/logo", apiGetDistrictLogo).Methods("GET").Name("district.logo.BySlug")
	r.Handle("/geocode/by-gid/{id:.*}", handlerJSON(geocode.ByGID)).Methods("GET")
	r.Handle("/geocode/reverse", handlerJSONPost(geocode.Reverse)).Methods("POST")
	r.Handle("/geocode/reverse/closest", handlerJSONPost(geocode.ReverseClosest)).Methods("POST")
	r.Handle("/geocode/suggestion", handlerJSONSlice(geocode.SuggestionList)).Methods("GET")

	r.Handle("/publicreport-notification", handlerJSONPost(publicreport_notification.Create)).Methods("POST")
	r.Handle("/qr-code/mailer/{code}", handlerBasic(qrcode.Mailer)).Methods("GET")
	r.Handle("/qr-code/marketing", handlerBasic(qrcode.Marketing)).Methods("GET")
	r.Handle("/qr-code/report/{code}", handlerBasic(qrcode.Report)).Methods("GET")
	r.HandleFunc("/rmo/compliance", handlerJSONPost(pr_compliance.Create)).Methods("POST")
	r.HandleFunc("/rmo/nuisance", handlerFormPost(nuisance.Create)).Methods("POST")
	r.Handle("/rmo/publicreport/{id}", handlerBasic(publicreport.ByIDPublic)).Methods("GET").Name("publicreport.ByIDGetPublic")
	r.Handle("/rmo/publicreport/compliance/{id}/image", handlerFormPost(publicreport.ImageCreate)).Methods("POST")
	r.Handle("/rmo/publicreport/compliance/{id}", handlerJSON(pr_compliance.ByIDPublic)).Methods("GET").Name("publicreport.compliance.ByIDGetPublic")
	r.Handle("/rmo/publicreport/compliance/{id}", handlerJSONPut(pr_compliance.Update)).Methods("PUT")
	r.Handle("/rmo/publicreport/nuisance/{id}", handlerJSON(nuisance.ByIDPublic)).Methods("GET").Name("publicreport.nuisance.ByIDGetPublic")
	r.Handle("/rmo/publicreport/water/{id}", handlerJSON(water.ByIDPublic)).Methods("GET").Name("publicreport.water.ByIDGetPublic")
	r.Handle("/rmo/publicreport/{id}", handlerBasic(publicreport.ByIDPublic)).Methods("GET").Name("publicreport.ByIDGetPublicPublic")
	r.HandleFunc("/rmo/water", handlerFormPost(water.Create)).Methods("POST")
}
func AddRoutesSync(r *mux.Router) {
	router := resource.NewRouter(r)

	contact := resource.Contact(router)
	compliance_request := resource.ComplianceRequest(router)
	district := resource.District(router)
	geocode := resource.Geocode(router)
	lob_hook := resource.LobHook(router)
	nuisance := resource.Nuisance(router)
	pr_compliance := resource.PublicReportCompliance(router)
	publicreport := resource.Publicreport(router)
	publicreport_notification := resource.PublicreportNotification(router)
	qrcode := resource.QRCode(router)
	service_request := resource.ServiceRequest(router)
	water := resource.Water(router)

	//r.Use(render.SetContentType(render.ContentTypeJSON))
	// Unauthenticated endpoints
	r.HandleFunc("", handlerJSON(getRoot))
	r.HandleFunc("/compliance-request/image/pool/{public_id}", compliance_request.ImagePoolGet).Methods("GET").Name("compliance-request.image.pool.ByIDGet")
	r.Handle("/district", handlerJSONSlice(district.List)).Methods("GET")
	r.Handle("/district/{id}", handlerJSON(district.GetByID)).Methods("GET").Name("district.ByIDGet")
	r.HandleFunc("/district/{slug}/logo", apiGetDistrictLogo).Methods("GET").Name("district.logo.BySlug")
	r.Handle("/geocode/by-gid/{id:.*}", handlerJSON(geocode.ByGID)).Methods("GET")
	r.Handle("/geocode/reverse", handlerJSONPost(geocode.Reverse)).Methods("POST")
	r.Handle("/geocode/reverse/closest", handlerJSONPost(geocode.ReverseClosest)).Methods("POST")
	r.Handle("/geocode/suggestion", handlerJSONSlice(geocode.SuggestionList)).Methods("GET")
	r.Handle("/lob/event", handlerBasic(lob_hook.Event)).Methods("POST")

	r.Handle("/publicreport-notification", handlerJSONPost(publicreport_notification.Create)).Methods("POST")
	r.Handle("/qr-code/mailer/{code}", handlerBasic(qrcode.Mailer)).Methods("GET")
	r.Handle("/qr-code/marketing", handlerBasic(qrcode.Marketing)).Methods("GET")
	r.Handle("/qr-code/report/{code}", handlerBasic(qrcode.Report)).Methods("GET")
	r.HandleFunc("/signin", handlerJSONPost(postSignin))
	r.Handle("/signout", authenticatedHandlerBasic(postSignout))
	r.HandleFunc("/signup", handlerJSONPost(postSignup))
	r.HandleFunc("/twilio/call", twilioCallPost).Methods("POST")
	r.HandleFunc("/twilio/call/status", twilioCallStatusPost).Methods("POST")
	r.HandleFunc("/twilio/message", twilioMessagePost).Methods("POST")
	r.HandleFunc("/twilio/text", twilioTextPost).Methods("POST")
	r.HandleFunc("/twilio/text/status", twilioTextStatusPost).Methods("POST")
	r.HandleFunc("/voipms/text", voipmsTextGet).Methods("GET")
	r.HandleFunc("/voipms/text", voipmsTextPost).Methods("POST")
	r.HandleFunc("/webhook/fieldseeker", webhookFieldseeker).Methods("GET")
	r.HandleFunc("/webhook/fieldseeker", webhookFieldseeker).Methods("POST")

	// Authenticated endpoints
	r.Handle("/audio/{uuid}", auth.NewEnsureAuth(apiAudioPost)).Methods("POST")
	r.Handle("/audio/{uuid}/content", auth.NewEnsureAuth(apiAudioContentPost)).Methods("POST")
	avatar := resource.Avatar(router)
	r.Handle("/avatar/{uuid}", authenticatedHandlerGetImage(avatar.ByUUIDGet)).Methods("GET").Name("avatar.ByUUIDGet")
	r.Handle("/avatar", authenticatedHandlerPostMultipart(avatar.Create, file.CollectionAvatar)).Methods("POST")
	r.Handle("/client/ios", auth.NewEnsureAuth(handleClientIos)).Methods("GET")
	communication := resource.Communication(router)
	r.Handle("/communication", authenticatedHandlerJSONSlice(communication.List)).Methods("GET")
	r.Handle("/communication/{id}", authenticatedHandlerJSON(communication.Get)).Methods("GET").Name("communication.ByIDGet")
	r.Handle("/communication/{id}/mark/invalid", authenticatedHandlerJSONPost(communication.MarkInvalid)).Methods("POST").Name("communication.MarkInvalid")
	r.Handle("/communication/{id}/mark/pending-response", authenticatedHandlerJSONPost(communication.MarkPendingResponse)).Methods("POST").Name("communication.MarkPendingResponse")
	r.Handle("/communication/{id}/mark/possible-issue", authenticatedHandlerJSONPost(communication.MarkPossibleIssue)).Methods("POST").Name("communication.MarkPossibleIssue")
	r.Handle("/communication/{id}/mark/possible-resolved", authenticatedHandlerJSONPost(communication.MarkPossibleResolved)).Methods("POST").Name("communication.MarkPossibleResolved")
	r.Handle("/compliance-request/mailer", authenticatedHandlerJSONPost(compliance_request.CreateMailer)).Methods("POST")
	//r.HandleFunc("/compliance-request/image/pool/{public_id}", getComplianceRequestImagePool).Methods("GET")
	r.Handle("/configuration/integration/arcgis", authenticatedHandlerJSONPost(postConfigurationIntegrationArcgis)).Methods("POST")
	r.Handle("/contact", authenticatedHandlerJSONSlice(contact.List)).Methods("GET")
	r.Handle("/contact/{id}", authenticatedHandlerJSON(contact.ByIDGet)).Methods("GET").Name("contact.ByIDGet")
	email := resource.Email(router)
	r.Handle("/email/{id}", authenticatedHandlerJSON(email.Get)).Methods("GET").Name("email.ByIDGet")
	r.Handle("/events", auth.NewEnsureAuth(streamEvents)).Methods("GET")
	r.Handle("/image/{uuid}", auth.NewEnsureAuth(apiImagePost)).Methods("POST")
	r.Handle("/image/{uuid}/content", auth.NewEnsureAuth(apiImageContentGet)).Methods("GET")
	r.Handle("/image/{uuid}/content", auth.NewEnsureAuth(apiImageContentPost)).Methods("POST")
	impersonation := resource.Impersonation(router)
	r.Handle("/impersonation", authenticatedHandlerJSONPost(impersonation.Create)).Methods("POST")
	r.Handle("/impersonation", authenticatedHandlerDelete(impersonation.Delete)).Methods("DELETE")
	lead := resource.Lead(r)
	r.Handle("/leads", authenticatedHandlerJSON(lead.List)).Methods("GET")
	r.Handle("/leads", authenticatedHandlerJSONPost(lead.Create)).Methods("POST")

	mailer := resource.Mailer(router)
	r.Handle("/mailer", authenticatedHandlerJSONSlice(mailer.List)).Methods("GET")
	r.Handle("/mailer/{id}", authenticatedHandlerJSONPost(mailer.ByIDGet)).Methods("GET").Name("mailer.ByIDGet")
	r.Handle("/mosquito-source", auth.NewEnsureAuth(apiMosquitoSource)).Methods("GET")

	r.Handle("/publicreport/invalid", authenticatedHandlerJSONPost(postPublicreportInvalid)).Methods("POST")
	r.Handle("/publicreport/signal", authenticatedHandlerJSONPost(postPublicreportSignal)).Methods("POST")
	r.Handle("/publicreport/message", authenticatedHandlerJSONPost(postPublicreportMessage)).Methods("POST")
	r.Handle("/publicreport/{id}", authenticatedHandlerBasic(publicreport.ByID)).Methods("GET").Name("publicreport.ByIDGet")
	r.Handle("/publicreport/compliance/{id}", authenticatedHandlerJSON(pr_compliance.ByID)).Methods("GET").Name("publicreport.compliance.ByIDGet")
	r.Handle("/publicreport/nuisance/{id}", authenticatedHandlerJSON(nuisance.ByID)).Methods("GET").Name("publicreport.nuisance.ByIDGet")
	r.Handle("/publicreport/water/{id}", authenticatedHandlerJSON(water.ByID)).Methods("GET").Name("publicreport.water.ByIDGet")

	r.Handle("/publicreport-notification", handlerJSONPost(publicreport_notification.Create)).Methods("POST")
	r.Handle("/review/pool", authenticatedHandlerJSONPost(postReviewPool)).Methods("POST")
	review_task := resource.ReviewTask(r)
	r.Handle("/review-task", authenticatedHandlerJSON(review_task.List)).Methods("GET")
	r.Handle("/service-request", authenticatedHandlerJSONSlice(service_request.List)).Methods("GET")
	session := resource.Session(router)
	r.Handle("/session", authenticatedHandlerJSON(session.Get)).Methods("GET").Name("session.get")
	signal := resource.Signal(r)
	r.Handle("/signal", authenticatedHandlerJSON(signal.List)).Methods("GET")
	site := resource.Site(router)
	r.Handle("/site", authenticatedHandlerJSONSlice(site.List)).Methods("GET")
	r.Handle("/site/{id}", authenticatedHandlerJSON(site.ByIDGet)).Methods("GET").Name("site.ByIDGet")
	sync := resource.Sync(r)
	r.Handle("/sync", authenticatedHandlerJSONSlice(sync.List)).Methods("GET")
	r.Handle("/sudo/email", authenticatedHandlerJSONPost(postSudoEmail)).Methods("POST")
	r.Handle("/sudo/sms", authenticatedHandlerJSONPost(postSudoSMS)).Methods("POST")
	r.Handle("/sudo/sse", authenticatedHandlerJSONPost(postSudoSSE)).Methods("POST")
	text := resource.Text(router)
	r.Handle("/text/{id}", authenticatedHandlerJSON(text.Get)).Methods("GET").Name("text.ByIDGet")
	r.Handle("/trap-data", auth.NewEnsureAuth(apiTrapData)).Methods("GET")
	r.Handle("/tile/{z}/{y}/{x}", auth.NewEnsureAuth(getTile)).Methods("GET")
	upload := resource.Upload(r)
	r.Handle("/upload/pool/custom", authenticatedHandlerPostMultipart(upload.PoolCustomCreate, file.CollectionCSV)).Methods("POST")
	r.Handle("/upload/pool/flyover", authenticatedHandlerPostMultipart(upload.PoolFlyoverCreate, file.CollectionCSV)).Methods("POST")
	r.Handle("/upload", authenticatedHandlerJSON(upload.List)).Methods("GET")
	r.Handle("/upload/{id}", authenticatedHandlerJSON(upload.ByIDGet)).Methods("GET")
	r.Handle("/upload/{id}/commit", authenticatedHandlerJSONPost(upload.Commit)).Methods("POST")
	r.Handle("/upload/{id}/discard", authenticatedHandlerJSONPost(upload.Discard)).Methods("POST")

	user := resource.User(router)
	r.Handle("/user/self", authenticatedHandlerJSON(user.SelfGet)).Methods("GET")
	r.Handle("/user/suggestion", authenticatedHandlerJSON(user.SuggestionGet)).Methods("GET")
	r.Handle("/user", authenticatedHandlerJSONSlice(user.List)).Methods("GET")
	r.Handle("/user/{id}", authenticatedHandlerJSON(user.ByIDGet)).Methods("GET").Name("user.ByIDGet")
	r.Handle("/user/{id}", authenticatedHandlerJSONPut(user.ByIDPut)).Methods("PUT")

	// Unauthenticated endpoints
}
