package webserver

import (
	"fmt"
	"math/rand"
	"net/http"

	"git.ctdo.de/henne/blitzer-v2/camera"
	"git.ctdo.de/henne/blitzer-v2/db"
	"git.ctdo.de/henne/blitzer-v2/printer"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
)

func HandleTest(ctx *gin.Context) {
	//id := rand.Intn(500)
	speedingTicket := db.SpeedingTicket{
		Speed: 20 + rand.Intn(50),
		//ImagePath:    fmt.Sprintf("https://picsum.photos/id/%d/300/200", id),
		//KIImagePath:  fmt.Sprintf("https://picsum.photos/id/%d/300/200", id),
		AllowedSpeed: db.GetConfig().TriggerSpeed,
	}
	if err := db.DB.Save(&speedingTicket).Error; err != nil {
		logrus.Error(err)
	}
	if err := camera.TakePhoto(speedingTicket.ID.String()); err != nil {
		logrus.Error(err)
	}
	speedingTicket.ImagePath = fmt.Sprintf("/images/original/ticket-%s.jpg", speedingTicket.ID)
	if err := db.DB.Save(&speedingTicket).Error; err != nil {
		logrus.Error(err)
	}
	printer.PrintTicket(speedingTicket)
	ctx.Redirect(http.StatusTemporaryRedirect, "/")
}
