package io.wdd.func.controller;

import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3Object;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.wdd.common.response.R;
import io.wdd.func.oss.config.OctopusObjectSummary;
import io.wdd.func.oss.config.OssConfig;
import io.wdd.func.oss.service.OSSCoreService;
import io.wdd.func.oss.service.OssBackendSelect;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/server/func/oss/")
@Api("Server对象存储的Controller")
public class OSSController {

    @Resource
    OSSCoreService OSSCoreService;

    @Resource
    OssBackendSelect ossBackendSelect;

    @GetMapping("/bucket/list/all/")
    @ApiOperation("[桶] - 所有的桶")
    public R<List<List<Bucket>>> bucketListAll() {

        return R.ok(OSSCoreService.listAllBucket());
    }

    @GetMapping("/bucket/list/one/")
    @ApiOperation("[桶] - 一个区域所有的桶")
    public R<List<Bucket>> bucketListOne(
            @RequestParam(value = "BackendPrefixName", required = false)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);

        return R.ok(OSSCoreService.listBucketList(ossConfig));
    }

    @GetMapping("/bucket/list/objects")
    @ApiOperation("[桶] - 一个区域一个桶中的对象")
    public R<ObjectListing> bucketObjectsListAll(
            @RequestParam(value = "BackendPrefixName", required = false)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = false)
            @ApiParam(value = "bucketName") String bucketName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);


        return R.ok(OSSCoreService.listBucketObjects(
                ossConfig,
                bucketName
        ));
    }


    @PostMapping("/bucket/delete")
    @ApiOperation("[桶] - 删除")
    public R<Boolean> bucketDelete(
            @RequestParam(value = "BackendPrefixName", required = true)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = true)
            @ApiParam(value = "bucketName") String bucketName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);


        return R.ok(OSSCoreService.deleteBucket(
                ossConfig,
                bucketName
        ));
    }

    @PostMapping("/bucket/create")
    @ApiOperation("[桶] - 创建")
    public R<Bucket> bucketCreate(
            @RequestParam(value = "BackendPrefixName", required = true)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = true)
            @ApiParam(value = "bucketName") String bucketName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);


        return R.ok(OSSCoreService.createBucket(
                ossConfig,
                bucketName
        ));
    }

    @PostMapping("/object/create")
    @ApiOperation("[对象] - 创建, 外部上传 [todo 分片上传]")
    public R<HashMap<String, String>> objectCreate(
            @RequestParam(value = "BackendPrefixName", required = true)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = true)
            @ApiParam(value = "bucketName") String bucketName,
            @RequestParam(value = "objectName", required = true)
            @ApiParam(value = "objectName") String objectName,
            @RequestParam("file") @RequestPart MultipartFile file
            ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);

        return R.ok(OSSCoreService.createObject(
                ossConfig,
                bucketName,
                objectName,
                file
        ));
    }

    @PostMapping("/object/simple")
    @ApiOperation("[对象] - 简单查询")
    public R<OctopusObjectSummary> objectDetailSimple(
            @RequestParam(value = "BackendPrefixName", required = true)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = true)
            @ApiParam(value = "bucketName") String bucketName,
            @RequestParam(value = "objectName", required = true)
            @ApiParam(value = "objectName") String objectName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);

        return R.ok(OSSCoreService.getObjectSimple(
                ossConfig,
                bucketName,
                objectName
        ));
    }


    @PostMapping("/object/one")
    @ApiOperation("[对象] - 默认查询")
    public R<S3Object> objectDetailOne(
            @RequestParam(value = "BackendPrefixName", required = true)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = true)
            @ApiParam(value = "bucketName") String bucketName,
            @RequestParam(value = "objectName", required = true)
            @ApiParam(value = "objectName") String objectName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);

        return R.ok(OSSCoreService.getObject(
                ossConfig,
                bucketName,
                objectName
        ));
    }

    @PostMapping("/object/head")
    @ApiOperation("[对象] - 查询头信息")
    public R<ObjectMetadata> objectDetailHead(
            @RequestParam(value = "BackendPrefixName", required = true)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = true)
            @ApiParam(value = "bucketName") String bucketName,
            @RequestParam(value = "objectName", required = true)
            @ApiParam(value = "objectName") String objectName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);

        return R.ok(OSSCoreService.headObject(
                ossConfig,
                bucketName,
                objectName
        ));
    }


    @PostMapping("/object/delete")
    @ApiOperation("[对象] - 删除")
    public R<Boolean> objectDelete(
            @RequestParam(value = "BackendPrefixName", required = true)
            @ApiParam(value = "BackendPrefixName") String BackendPrefixName,
            @RequestParam(value = "bucketName", required = true)
            @ApiParam(value = "bucketName") String bucketName,
            @RequestParam(value = "objectName", required = true)
            @ApiParam(value = "objectName") String objectName
    ) {

        OssConfig ossConfig = ossBackendSelect.one(BackendPrefixName);

        return R.ok(OSSCoreService.deleteObject(
                ossConfig,
                bucketName,
                objectName
        ));
    }



}
