package http

import (
	"fmt"
	"mime/multipart"
	"net/http"
	"net/url"
	"path/filepath"

	"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"
	clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
)

// PackageService 包服务，负责处理包相关的HTTP请求。
type PackageService struct {
	*Server
}

// Package 返回PackageService的实例。
func (s *Server) Package() *PackageService {
	return &PackageService{
		Server: s,
	}
}

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

	var req cliapi.PackageGetReq
	if err := ctx.ShouldBindQuery(&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.PackageSvc().Get(req.PackageID)
	if err != nil {
		log.Warnf("getting package: %s", err.Error())
		ctx.JSON(http.StatusOK, FailedError(err))
		return
	}

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

func (s *PackageService) GetByFullName(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.GetByFullName")

	var req cliapi.PackageGetByFullName
	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
	}

	pkg, err := s.svc.PackageSvc().GetByFullName(req.BucketName, req.PackageName)
	if err != nil {
		log.Warnf("getting package by name: %s", err.Error())
		ctx.JSON(http.StatusOK, FailedError(err))
		return
	}

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

// Create 处理创建新包的HTTP请求。
func (s *PackageService) Create(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.Create")
	var req cliapi.PackageCreate
	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.PackageSvc().Create(req.BucketID, req.Name)
	if err != nil {
		log.Warnf("creating package: %s", err.Error())
		ctx.JSON(http.StatusOK, FailedError(err))
		return
	}

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

type PackageCreateLoad struct {
	Info  cliapi.PackageCreateLoadInfo `form:"info" binding:"required"`
	Files []*multipart.FileHeader      `form:"files"`
}

func (s *PackageService) CreateLoad(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.CreateLoad")

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

	if len(req.Info.LoadTo) != len(req.Info.LoadToPath) {
		log.Warnf("load to and load to path count not match")
		ctx.JSON(http.StatusOK, Failed(errorcode.BadArgument, "load to and load to path count not match"))
		return
	}

	up, err := s.svc.Uploader.BeginCreateLoad(req.Info.BucketID, req.Info.Name, req.Info.LoadTo, req.Info.LoadToPath)
	if err != nil {
		log.Warnf("begin package create load: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("begin package create load: %v", err)))
		return
	}
	defer up.Abort()

	var pathes []string
	for _, file := range req.Files {
		f, err := file.Open()
		if err != nil {
			log.Warnf("open file: %s", err.Error())
			ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("open file %v: %v", file.Filename, err)))
			return
		}

		path, err := url.PathUnescape(file.Filename)
		if err != nil {
			log.Warnf("unescape filename: %s", err.Error())
			ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("unescape filename %v: %v", file.Filename, err)))
			return
		}
		path = filepath.ToSlash(path)

		err = up.Upload(path, f)
		if err != nil {
			log.Warnf("uploading file: %s", err.Error())
			ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("uploading file %v: %v", file.Filename, err)))
			return
		}
		pathes = append(pathes, path)
	}

	ret, err := up.Commit()
	if err != nil {
		log.Warnf("commit create load: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, fmt.Sprintf("commit create load: %v", err)))
		return
	}

	objs := make([]clitypes.Object, len(pathes))
	for i := range pathes {
		objs[i] = ret.Objects[pathes[i]]
	}

	ctx.JSON(http.StatusOK, OK(cliapi.PackageCreateLoadResp{Package: ret.Package, Objects: objs}))

}
func (s *PackageService) Delete(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.Delete")

	var req cliapi.PackageDelete
	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.PackageSvc().DeletePackage(req.PackageID)
	if err != nil {
		log.Warnf("deleting package: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "delete package failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(nil))
}

func (s *PackageService) Clone(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.Clone")

	var req cliapi.PackageClone
	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.PackageSvc().Clone(req.PackageID, req.BucketID, req.Name)
	if err != nil {
		log.Warnf("cloning package: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "clone package failed"))
		return
	}

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

func (s *PackageService) ListBucketPackages(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.ListBucketPackages")

	var req cliapi.PackageListBucketPackages
	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
	}

	pkgs, err := s.svc.PackageSvc().GetBucketPackages(req.BucketID)
	if err != nil {
		log.Warnf("getting bucket packages: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get bucket packages failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cliapi.PackageListBucketPackagesResp{
		Packages: pkgs,
	}))
}

/*
// GetCachedStorages 处理获取包的缓存节点的HTTP请求。
func (s *PackageService) GetCachedStorages(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.GetCachedStorages")

	var req cdsapi.PackageGetCachedStoragesReq
	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
	}

	resp, err := s.svc.PackageSvc().GetCachedStorages(req.PackageID)
	if err != nil {
		log.Warnf("get package cached storages failed: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package cached storages failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cdsapi.PackageGetCachedStoragesResp{PackageCachingInfo: resp}))
}
*/
