package structs

import (
	"sync"
	"time"
)

type Robot struct {
	Id                  string
	Type                uint8
	Name                string
	Status              uint8
	PingRetries         uint8 `gorm:"-"`
	Address             string
	FirmwareVersion     string
	CurrentJobId        string
	CurrentJobName      string
	JobMutex            sync.Mutex       `gorm:"-"`
	JobsWaitingCount    int              `gorm:"-"`
	JobsWaitingNameList []JobWaitingName `gorm:"-"`
	LastTaskAt          time.Time        `gorm:"-"`
	ConnectedAt         time.Time        `gorm:"-"`
	CreatedAt           time.Time
}

type JobWaitingName struct {
	JobId string
	Name  string
}

type APIRobot struct {
	Id                  string
	Type                uint8
	Name                string
	Status              uint8
	Address             string
	CurrentJobName      string
	JobsWaitingCount    int
	JobsWaitingNameList []string
	FirmwareVersion     string
	ConnectedAt         time.Time
	CreatedAt           time.Time
}

/*
func (r *Robot) CountUpJobsWaiting() {
	r.JobMutex.Lock()
	defer r.JobMutex.Unlock()

	r.JobsWaitingCount++
} */
/*
func (r *Robot) CountDownJobsWaiting() {
	r.JobMutex.Lock()
	defer r.JobMutex.Unlock()

	if r.JobsWaitingCount > 0 {
		r.JobsWaitingCount--
	}
}
*/
/*
func (r *Robot) ProcessJobTask(jobId string) {
	fmt.Println("wait for job", jobId)

	if r.CurrentJobId == "" {
		robot.UpdateRobotCurrentJobId(r, jobId)
	}

	if r.CurrentJobId != "" && r.CurrentJobId != jobId {
		r.CountUpJobsWaiting()
	}

	for r.CurrentJobId != "" && r.CurrentJobId != jobId {
		// wait for current job to finish
		fmt.Println("job is processing", r.CurrentJobId, jobId, r.JobsWaitingCount)
		time.Sleep(2 * time.Second)
	}

	robot.UpdateRobotCurrentJobId(r, jobId)

	fmt.Println("processing job", jobId)
} */

/*
func (r *Robot) SetCurrentJobId(jobId string) {
	r.JobMutex.Lock()
	defer r.JobMutex.Unlock()

	r.CurrentJobId = jobId
} */

type UnauthorizedRobot struct {
	Id              string
	Type            uint8
	Address         string
	FirmwareVersion string
	ConnectedAt     time.Time
	CreatedAt       time.Time
}

// swagger:model FirstRequestBody
type FirstRequestBody struct {
	// robot id
	Id string
	// robot type
	Type uint8
	// used firmware version of the robot
	FirmwareVersion string
	// connected modul like a gripper
	//ConnectedModul uint8
}

// swagger:model StatusResponse
type StatusResponse struct {
	Status string
}

// swagger:model RobotsResponse
type RobotsResponse struct {
	Robots     []APIRobot
	TotalPages int
}

// swagger:model UnauthorizedRobotsResponse
type UnauthorizedRobotsResponse struct {
	UnauthorizedRobots []UnauthorizedRobot
	TotalPages         int
}

type RobotIdParams struct {
	RobotId string
}

type RobotNameParams struct {
	RobotName string
}

type RobotFinishBody struct {
	RobotName string
	JobId     string
}

type UpdateRobotBody struct {
	RobotId string
	Name    string
}
