package http

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
	"gitlink.org.cn/cloudream/common/consts/errorcode"
	"gitlink.org.cn/cloudream/common/pkgs/logger"
	cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api"
)

type UserSpaceService struct {
	*Server
}

func (s *Server) UserSpace() *UserSpaceService {
	return &UserSpaceService{
		Server: s,
	}
}

func (s *UserSpaceService) LoadPackage(ctx *gin.Context) {
	log := logger.WithField("HTTP", "UserSpace.LoadPackage")

	var req cliapi.UserSpaceLoadPackageReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		log.Warnf("binding body: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	err := s.svc.UserSpaceSvc().LoadPackage(req.PackageID, req.UserSpaceID, req.RootPath)
	if err != nil {
		log.Warnf("loading package: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "loading package failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceLoadPackageResp{}))
}

func (s *UserSpaceService) CreatePackage(ctx *gin.Context) {
	log := logger.WithField("HTTP", "UserSpace.CreatePackage")

	var req cliapi.UserSpaceCreatePackageReq
	if err := ctx.ShouldBindJSON(&req); err != nil {
		log.Warnf("binding body: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	pkg, err := s.svc.Uploader.UserSpaceUpload(req.UserSpaceID, req.Path, req.BucketID, req.Name, req.SpaceAffinity)
	if err != nil {
		log.Warnf("userspace create package: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("userspace create package: %v", err)))
		return
	}

	ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceCreatePackageResp{
		Package: *pkg,
	}))
}

func (s *UserSpaceService) Get(ctx *gin.Context) {
	log := logger.WithField("HTTP", "UserSpace.Get")

	var req cliapi.UserSpaceGet
	if err := ctx.ShouldBindQuery(&req); err != nil {
		log.Warnf("binding query: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	info, err := s.svc.UserSpaceSvc().Get(req.UserSpaceID)
	if err != nil {
		log.Warnf("getting info: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get userspace inf failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceGetResp{
		UserSpace: info,
	}))
}

func (s *UserSpaceService) SpaceToSpace(ctx *gin.Context) {
	log := logger.WithField("HTTP", "UserSpace.SpaceToSpace")

	var req cliapi.UserSpaceSpaceToSpace
	if err := ctx.ShouldBindJSON(&req); err != nil {
		log.Warnf("binding body: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	ret, err := s.svc.UserSpaceSvc().SpaceToSpace(req.SrcUserSpaceID, req.SrcPath, req.DstUserSpaceID, req.DstPath)
	if err != nil {
		log.Warnf("space2space: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "space2space failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cliapi.UserSpaceSpaceToSpaceResp{
		SpaceToSpaceResult: ret,
	}))
}
