package control

import (
	"jannex/robot-control-manager/modules/cache"
	"jannex/robot-control-manager/modules/request"
	"jannex/robot-control-manager/modules/robot"
	"jannex/robot-control-manager/modules/structs"
	"jannex/robot-control-manager/modules/utils"

	"git.ex.umbach.dev/Alex/roese-utils/rsutils"
	"github.com/gofiber/fiber/v2"
)

func ControlRex(c *fiber.Ctx) error {
	// swagger:operation POST /control/1 control controlRex
	// ---
	// summary: Control Rex.
	// description: |
	//   This is used to control Rex.
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// parameters:
	// - in: body
	//   name: body
	//   description: Control Rex body.
	//   required: true
	//   schema:
	//     "$ref": "#/definitions/ControlBody"
	// responses:
	//   "200":
	//    description: Control Rex
	//   "400":
	//    description: Invalid request body
	//   "422":
	//    description: Robot not found

	var body structs.ControlBody

	if err := rsutils.BodyParserHelper(c, &body); err != nil {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	/*
		if body.Task.Y == nil {
			fmt.Println("y not sent")
		} else {
			fmt.Printf("y-value: %d\n", *body.Task.Y)
		} */

	r := cache.GetRobotByName(body.RobotName)

	if r == nil {
		return c.SendStatus(fiber.StatusUnprocessableEntity)
	}

	if r.Status == utils.RobotStatusOffline {
		return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusOffline})
	}

	if r.Status != utils.RobotStatusIdle && r.Status != utils.RobotStatusRunning {
		
	} 

	robot.ProcessJobTask(r, body.JobId, body.JobName)
	robot.UpdateRobotStatus(r, utils.RobotStatusRunning)

	if err := request.Request(fiber.MethodPost, r.Address+":5000/api/v1/control", body.Task); err != nil {
		return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusError})
	}

	robot.UpdateRobotStatus(r, utils.RobotStatusIdle)

	return c.JSON(structs.StatusResponse{Status: utils.ResponseStatusOk})
}

func FinishControlRex(c *fiber.Ctx) error {
	// swagger:operation POST /control/1/finish control finishControlRex
	// ---
	// summary: Finish control Rex.
	// description: |
	//   This is used to finish control Rex.
	// consumes:
	// - application/json
	// produces:
	// - application/json
	// responses:
	//   "200":
	//    description: Finish control Rex
	//   "400":
	//    description: Invalid robot name
	//   "422":
	//    description: Robot not found

	var body structs.RobotFinishBody

	if err := rsutils.BodyParserHelper(c, &body); err != nil {
		return c.SendStatus(fiber.StatusBadRequest)
	}

	r := cache.GetRobotByName(body.RobotName)

	if r == nil || r.CurrentJobId != body.JobId {
		return c.SendStatus(fiber.StatusUnprocessableEntity)
	}

	robot.CountDownJobsWaiting(r, body.JobId)
	robot.UpdateRobotCurrentJob(r, "", "")

	return c.SendStatus(fiber.StatusOK)
}
