package api

import (
	"net/http"

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

type BucketService struct {
	*Client
}

func (c *Client) Bucket() *BucketService {
	return &BucketService{c}
}

const BucketGetByNamePath = "/bucket/getByName"

type BucketGetByName struct {
	Name string `url:"name" form:"name" binding:"required"`
}

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

type BucketGetByNameResp struct {
	Bucket clitypes.Bucket `json:"bucket"`
}

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

func (c *BucketService) GetByName(req BucketGetByName) (*BucketGetByNameResp, error) {
	return JSONAPI(c.cfg, http.DefaultClient, &req, &BucketGetByNameResp{})
}

const BucketCreatePath = "/bucket/create"

type BucketCreate struct {
	Name string `json:"name" binding:"required"`
}

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

type BucketCreateResp struct {
	Bucket clitypes.Bucket `json:"bucket"`
}

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

func (c *BucketService) Create(req BucketCreate) (*BucketCreateResp, error) {
	return JSONAPI(c.cfg, http.DefaultClient, &req, &BucketCreateResp{})
}

const BucketDeletePath = "/bucket/delete"

type BucketDelete struct {
	BucketID clitypes.BucketID `json:"bucketID" binding:"required"`
}

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

type BucketDeleteResp struct{}

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

func (c *BucketService) Delete(req BucketDelete) error {
	return JSONAPINoData(c.cfg, http.DefaultClient, &req)
}

const BucketListAllPath = "/bucket/listAll"

type BucketListAll struct {
}

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

type BucketListAllResp struct {
	Buckets []clitypes.Bucket `json:"buckets"`
}

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

func (c *BucketService) ListAll(req BucketListAll) (*BucketListAllResp, error) {
	return JSONAPI(c.cfg, http.DefaultClient, &req, &BucketListAllResp{})
}
