package repo

import (
	"net/http"

	"code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/convert"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/modules/storage"
	api "code.gitea.io/gitea/modules/structs"
	routerRepo "code.gitea.io/gitea/routers/repo"
)

type FileInfo struct {
	FileName string `json:"fileName"`
	ModTime  string `json:"modTime"`
	IsDir    bool   `json:"isDir"`
	Size     int64  `json:"size"`
	ParenDir string `json:"parenDir"`
	UUID     string `json:"uuid"`
}

func CreateNewModel(ctx *context.APIContext) {
	log.Info("CreateNewModel by api.")
	routerRepo.SaveModel(ctx.Context)
}

func ShowModelManageApi(ctx *context.APIContext) {
	log.Info("ShowModelManageApi by api.")
	routerRepo.ShowModelPageInfo(ctx.Context)
}

func DeleteModel(ctx *context.APIContext) {
	log.Info("DeleteModel by api.")
	routerRepo.DeleteModel(ctx.Context)
}

func DownloadModel(ctx *context.APIContext) {
	log.Info("DownloadModel by api.")
	routerRepo.DownloadMultiModelFile(ctx.Context)
}

func QueryModelById(ctx *context.APIContext) {
	log.Info("QueryModelById by api.")
	routerRepo.QueryModelById(ctx.Context)
}

func QueryModelByName(ctx *context.APIContext) {
	log.Info("QueryModelByName by api.")
	routerRepo.ShowSingleModel(ctx.Context)
}

func QueryModelListForPredict(ctx *context.APIContext) {
	log.Info("QueryModelListForPredict by api.")
	ctx.Context.SetParams("isOnlyThisRepo", "true")
	routerRepo.QueryModelListForPredict(ctx.Context)
}

func QueryTrainJobList(ctx *context.APIContext) {
	result, err := routerRepo.QueryTrainJobListApi(ctx.Context)
	if err != nil {
		log.Info("query error." + err.Error())
		ctx.JSON(http.StatusOK, nil)
	} else {
		re := make([]*api.Cloudbrain, 0)
		for _, task := range result {
			conRe := convert.ToCloudBrain(task)
			re = append(re, conRe)
		}
		ctx.JSON(http.StatusOK, re)
	}
}

func QueryTrainModelList(ctx *context.APIContext) {
	result, err := routerRepo.QueryTrainModelFileById(ctx.Context)
	if err != nil {
		log.Info("query error." + err.Error())
	}
	re := convertFileFormat(result)
	ctx.JSON(http.StatusOK, re)
}

func QueryTrainJobVersionList(ctx *context.APIContext) {
	result, err := routerRepo.QueryTrainJobVersionListApi(ctx.Context)
	if err != nil {
		log.Info("query error." + err.Error())
		ctx.JSON(http.StatusOK, nil)
	} else {
		re := make([]*api.Cloudbrain, 0)
		for _, task := range result {
			conRe := convert.ToCloudBrain(task)
			re = append(re, conRe)
		}
		ctx.JSON(http.StatusOK, re)
	}
}

func convertFileFormat(result []storage.FileInfo) []FileInfo {
	re := make([]FileInfo, 0)
	if result != nil {
		for _, file := range result {
			tmpFile := FileInfo{
				FileName: file.FileName,
				ModTime:  file.ModTime,
				IsDir:    file.IsDir,
				Size:     file.Size,
				ParenDir: file.ParenDir,
				UUID:     file.UUID,
			}
			re = append(re, tmpFile)
		}
	}
	return re
}

func QueryModelFileForPredict(ctx *context.APIContext) {
	log.Info("QueryModelFileForPredict by api.")
	id := ctx.Query("id")
	result := routerRepo.QueryModelFileByID(id)
	re := convertFileFormat(result)
	ctx.JSON(http.StatusOK, re)
}

func CreateModelConvert(ctx *context.APIContext) {
	log.Info("CreateModelConvert by api.")
	routerRepo.SaveModelConvert(ctx.Context)
}

func StopModelConvert(ctx *context.APIContext) {
	log.Info("StopModelConvert by api.")
	routerRepo.StopModelConvertApi(ctx.Context)
}

func ShowModelConvertPage(ctx *context.APIContext) {
	log.Info("ShowModelConvertPage by api.")
	modelResult, count, err := routerRepo.GetModelConvertPageData(ctx.Context)
	if err == nil {
		mapInterface := make(map[string]interface{})
		mapInterface["data"] = modelResult
		mapInterface["count"] = count
		ctx.JSON(http.StatusOK, mapInterface)
	} else {
		mapInterface := make(map[string]interface{})
		mapInterface["data"] = nil
		mapInterface["count"] = 0
		ctx.JSON(http.StatusOK, mapInterface)
	}

}

func QueryModelConvertById(ctx *context.APIContext) {
	modelResult, err := routerRepo.GetModelConvertById(ctx.Context)
	if err == nil {
		ctx.JSON(http.StatusOK, modelResult)
	} else {
		ctx.JSON(http.StatusOK, nil)
	}
}

func QueryModelConvertByName(ctx *context.APIContext) {
	modelResult, err := routerRepo.GetModelConvertByName(ctx.Context)
	if err == nil {
		ctx.JSON(http.StatusOK, modelResult)
	} else {
		ctx.JSON(http.StatusOK, nil)
	}
}
