package cloudbrainTask

import (
	"fmt"
	"strconv"

	"code.gitea.io/gitea/models"
)

type StatusInfo struct {
	CloudBrainTypes  []int
	JobType          []models.JobType
	NotFinalStatuses []string
	ComputeResource  string
}

var CloudbrainOneNotFinalStatuses = []string{string(models.JobWaiting), string(models.JobRunning)}
var CloudbrainTwoNotFinalStatuses = []string{string(models.ModelArtsTrainJobInit), string(models.ModelArtsTrainJobImageCreating), string(models.ModelArtsTrainJobSubmitTrying), string(models.ModelArtsTrainJobWaiting), string(models.ModelArtsTrainJobRunning), string(models.ModelArtsTrainJobScaling), string(models.ModelArtsTrainJobCheckInit), string(models.ModelArtsTrainJobCheckRunning), string(models.ModelArtsTrainJobCheckRunningCompleted)}
var GrampusNotFinalStatuses = []string{models.GrampusStatusWaiting, models.GrampusStatusRunning}
var StatusInfoDict = map[string]StatusInfo{string(models.JobTypeDebug) + "-" + strconv.Itoa(models.TypeCloudBrainOne): {
	CloudBrainTypes:  []int{models.TypeCloudBrainOne},
	JobType:          []models.JobType{models.JobTypeDebug},
	NotFinalStatuses: CloudbrainOneNotFinalStatuses,
	ComputeResource:  models.GPUResource,
}, string(models.JobTypeTrain) + "-" + strconv.Itoa(models.TypeCloudBrainOne): {
	CloudBrainTypes:  []int{models.TypeCloudBrainOne},
	JobType:          []models.JobType{models.JobTypeTrain},
	NotFinalStatuses: CloudbrainOneNotFinalStatuses,
	ComputeResource:  models.GPUResource,
}, string(models.JobTypeInference) + "-" + strconv.Itoa(models.TypeCloudBrainOne): {
	CloudBrainTypes:  []int{models.TypeCloudBrainOne},
	JobType:          []models.JobType{models.JobTypeInference},
	NotFinalStatuses: CloudbrainOneNotFinalStatuses,
	ComputeResource:  models.GPUResource,
}, string(models.JobTypeBenchmark) + "-" + strconv.Itoa(models.TypeCloudBrainOne): {
	CloudBrainTypes:  []int{models.TypeCloudBrainOne},
	JobType:          []models.JobType{models.JobTypeBenchmark, models.JobTypeBrainScore, models.JobTypeSnn4imagenet, models.JobTypeSnn4Ecoset},
	NotFinalStatuses: CloudbrainOneNotFinalStatuses,
	ComputeResource:  models.GPUResource,
}, string(models.JobTypeDebug) + "-" + strconv.Itoa(models.TypeCloudBrainTwo): {
	CloudBrainTypes:  []int{models.TypeCloudBrainTwo, models.TypeCDCenter},
	JobType:          []models.JobType{models.JobTypeDebug},
	NotFinalStatuses: []string{string(models.ModelArtsCreateQueue), string(models.ModelArtsCreating), string(models.ModelArtsStarting), string(models.ModelArtsReadyToStart), string(models.ModelArtsResizing), string(models.ModelArtsStartQueuing), string(models.ModelArtsRunning), string(models.ModelArtsRestarting)},
	ComputeResource:  models.NPUResource,
}, string(models.JobTypeTrain) + "-" + strconv.Itoa(models.TypeCloudBrainTwo): {
	CloudBrainTypes:  []int{models.TypeCloudBrainTwo},
	JobType:          []models.JobType{models.JobTypeTrain},
	NotFinalStatuses: CloudbrainTwoNotFinalStatuses,
	ComputeResource:  models.NPUResource,
}, string(models.JobTypeInference) + "-" + strconv.Itoa(models.TypeCloudBrainTwo): {
	CloudBrainTypes:  []int{models.TypeCloudBrainTwo},
	JobType:          []models.JobType{models.JobTypeInference},
	NotFinalStatuses: CloudbrainTwoNotFinalStatuses,
	ComputeResource:  models.NPUResource,
}, string(models.JobTypeTrain) + "-" + strconv.Itoa(models.TypeC2Net) + "-" + models.GPUResource: {
	CloudBrainTypes:  []int{models.TypeC2Net},
	JobType:          []models.JobType{models.JobTypeTrain},
	NotFinalStatuses: GrampusNotFinalStatuses,
	ComputeResource:  models.GPUResource,
}, string(models.JobTypeTrain) + "-" + strconv.Itoa(models.TypeC2Net) + "-" + models.NPUResource: {
	CloudBrainTypes:  []int{models.TypeC2Net},
	JobType:          []models.JobType{models.JobTypeTrain},
	NotFinalStatuses: GrampusNotFinalStatuses,
	ComputeResource:  models.NPUResource,
}, string(models.JobTypeDebug) + "-" + strconv.Itoa(models.TypeC2Net) + "-" + models.GPUResource: {
	CloudBrainTypes:  []int{models.TypeC2Net},
	JobType:          []models.JobType{models.JobTypeDebug},
	NotFinalStatuses: GrampusNotFinalStatuses,
	ComputeResource:  models.GPUResource,
}, string(models.JobTypeDebug) + "-" + strconv.Itoa(models.TypeC2Net) + "-" + models.NPUResource: {
	CloudBrainTypes:  []int{models.TypeC2Net},
	JobType:          []models.JobType{models.JobTypeDebug},
	NotFinalStatuses: GrampusNotFinalStatuses,
	ComputeResource:  models.NPUResource,
}, string(models.JobTypeDebug) + "-" + strconv.Itoa(models.TypeC2Net) + "-" + models.GCUResource: {
	CloudBrainTypes:  []int{models.TypeC2Net},
	JobType:          []models.JobType{models.JobTypeDebug},
	NotFinalStatuses: GrampusNotFinalStatuses,
	ComputeResource:  models.GCUResource,
}}

func GetNotFinalStatusTaskCount(uid int64, cloudbrainType int, jobType string, computeResource ...string) (int, error) {
	jobNewType := jobType
	if models.IsModelBenchMarkJobType(jobType) {
		jobNewType = string(models.JobTypeBenchmark)
	}

	key := jobNewType + "-" + strconv.Itoa(cloudbrainType)
	if len(computeResource) > 0 && cloudbrainType == models.TypeC2Net {
		key = key + "-" + computeResource[0]
	}

	if statusInfo, ok := StatusInfoDict[key]; ok {

		return models.GetNotFinalStatusTaskCount(uid, statusInfo.NotFinalStatuses, statusInfo.JobType, statusInfo.CloudBrainTypes, statusInfo.ComputeResource)

	} else {
		return 0, fmt.Errorf("Can not find the status info.")
	}

}
