package api

import (
	"net/http"

	"gitlink.org.cn/cloudream/common/sdks"
	clitypes "gitlink.org.cn/cloudream/jcs-pub/client/types"
)

const UserSpaceLoadPackagePath = "/userspace/loadPackage"

type UserSpaceLoadPackageReq struct {
	PackageID   clitypes.PackageID   `json:"packageID" binding:"required"`
	UserSpaceID clitypes.UserSpaceID `json:"userSpaceID" binding:"required"`
	RootPath    string               `json:"rootPath"`
}

func (r *UserSpaceLoadPackageReq) MakeParam() *sdks.RequestParam {
	return sdks.MakeJSONParam(http.MethodPost, UserSpaceLoadPackagePath, r)
}

type UserSpaceLoadPackageResp struct{}

func (r *UserSpaceLoadPackageResp) ParseResponse(resp *http.Response) error {
	return sdks.ParseCodeDataJSONResponse(resp, r)
}

func (c *Client) UserSpaceLoadPackage(req UserSpaceLoadPackageReq) (*UserSpaceLoadPackageResp, error) {
	return JSONAPI(c.cfg, http.DefaultClient, &req, &UserSpaceLoadPackageResp{})
}

const UserSpaceCreatePackagePath = "/userspace/createPackage"

type UserSpaceCreatePackageReq struct {
	UserSpaceID   clitypes.UserSpaceID `json:"userSpaceID" binding:"required"`
	Path          string               `json:"path" binding:"required"`
	BucketID      clitypes.BucketID    `json:"bucketID" binding:"required"`
	Name          string               `json:"name" binding:"required"`
	SpaceAffinity clitypes.UserSpaceID `json:"spaceAffinity"`
}

func (r *UserSpaceCreatePackageReq) MakeParam() *sdks.RequestParam {
	return sdks.MakeJSONParam(http.MethodPost, UserSpaceCreatePackagePath, r)
}

type UserSpaceCreatePackageResp struct {
	Package clitypes.Package `json:"package"`
}

func (r *UserSpaceCreatePackageResp) ParseResponse(resp *http.Response) error {
	return sdks.ParseCodeDataJSONResponse(resp, r)
}

func (c *Client) UserSpaceCreatePackage(req UserSpaceCreatePackageReq) (*UserSpaceCreatePackageResp, error) {
	return JSONAPI(c.cfg, http.DefaultClient, &req, &UserSpaceCreatePackageResp{})
}

const UserSpaceGetPath = "/userspace/get"

type UserSpaceGet struct {
	UserSpaceID clitypes.UserSpaceID `form:"userSpaceID" url:"userSpaceID" binding:"required"`
}

func (r *UserSpaceGet) MakeParam() *sdks.RequestParam {
	return sdks.MakeQueryParam(http.MethodGet, UserSpaceGetPath, r)
}

type UserSpaceGetResp struct {
	clitypes.UserSpace
}

func (r *UserSpaceGetResp) ParseResponse(resp *http.Response) error {
	return sdks.ParseCodeDataJSONResponse(resp, r)
}

func (c *Client) UserSpaceGet(req UserSpaceGet) (*UserSpaceGetResp, error) {
	return JSONAPI(c.cfg, http.DefaultClient, &req, &UserSpaceGetResp{})
}

const UserSpaceSpaceToSpacePath = "/v1/userspace/spaceToSpace"

type UserSpaceSpaceToSpace struct {
	SrcUserSpaceID clitypes.UserSpaceID `json:"srcUserSpaceID" binding:"required"`
	DstUserSpaceID clitypes.UserSpaceID `json:"dstUserSpaceID" binding:"required"`
	SrcPath        string               `json:"srcPath" binding:"required"`
	DstPath        string               `json:"dstPath" binding:"required"`
}

func (r *UserSpaceSpaceToSpace) MakeParam() *sdks.RequestParam {
	return sdks.MakeJSONParam(http.MethodPost, UserSpaceSpaceToSpacePath, r)
}

type UserSpaceSpaceToSpaceResp struct {
	clitypes.SpaceToSpaceResult
}

func (r *UserSpaceSpaceToSpaceResp) ParseResponse(resp *http.Response) error {
	return sdks.ParseCodeDataJSONResponse(resp, r)
}

func (c *Client) UserSpaceSpaceToSpace(req UserSpaceSpaceToSpace) (*UserSpaceSpaceToSpaceResp, error) {
	return JSONAPI(c.cfg, http.DefaultClient, &req, &UserSpaceSpaceToSpaceResp{})
}
