package com.yfd.platform.modules.storage.controller.base;

import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.yfd.platform.config.ResponseResult;
import com.yfd.platform.modules.config.model.request.SaveStorageSourceRequest;
import com.yfd.platform.modules.config.model.request.UpdateStorageSortRequest;
import com.yfd.platform.modules.storage.convert.StorageSourceConvert;
import com.yfd.platform.modules.storage.model.dto.StorageSourceDTO;
import com.yfd.platform.modules.storage.model.entity.StorageSource;
import com.yfd.platform.modules.storage.model.result.StorageSourceAdminResult;
import com.yfd.platform.modules.storage.service.StorageSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 存储源基础设置模块接口
 *
 * @author zhaojun
 */
@Api(tags = "存储源模块-基础")
@ApiSort(3)
@RestController
@RequestMapping("/admin")
public class StorageSourceController {

    @Resource
    private StorageSourceService storageSourceService;

    @Resource
    private StorageSourceConvert storageSourceConvert;


    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取所有存储源列表", notes = "获取所有添加的存储源列表，按照排序值由小到大排序")
    @GetMapping("/storages")
    public ResponseResult storageList() {
        List<StorageSource> list = storageSourceService.findAllOrderByOrderNum();
        List<StorageSourceAdminResult> storageSourceAdminResults = storageSourceConvert.entityToAdminResultList(list);
        return ResponseResult.successData(storageSourceAdminResults);
    }


    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "获取指定存储源参数", notes = "获取指定存储源基本信息及其参数")
    @ApiImplicitParam(paramType = "path", name = "storageId", value = "存储源 id", required = true, dataTypeClass = Integer.class)
    @GetMapping("/storage/{storageId}")
    public ResponseResult storageItem(@PathVariable Integer storageId) {
        StorageSourceDTO storageSourceDTO = storageSourceService.findDTOById(storageId);
        return ResponseResult.successData(storageSourceDTO);
    }


    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "保存存储源参数", notes = "保存存储源的所有参数")
    @PostMapping("/storage")
    public ResponseResult saveStorageItem(@RequestBody SaveStorageSourceRequest saveStorageSourceRequest) {
        Integer id = storageSourceService.saveStorageSource(saveStorageSourceRequest);
        return ResponseResult.successData(id);
    }


    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "删除存储源", notes = "删除存储源基本设置和拓展设置")
    @ApiImplicitParam(paramType = "path", name = "storageId", value = "存储源 id", required = true, dataTypeClass = Integer.class)
    @DeleteMapping("/storage/{storageId}")
    public ResponseResult deleteStorageItem(@PathVariable Integer storageId) {
        storageSourceService.deleteById(storageId);
        return ResponseResult.success();
    }


    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "启用存储源", notes = "开启存储源后可在前台显示")
    @ApiImplicitParam(paramType = "path", name = "storageId", value = "存储源 id", required = true, dataTypeClass = Integer.class)
    @PostMapping("/storage/{storageId}/enable")
    public ResponseResult enable(@PathVariable Integer storageId) {
        StorageSource storageSource = storageSourceService.findById(storageId);
        storageSource.setEnable(true);
        storageSourceService.updateById(storageSource);
        return ResponseResult.success();
    }


    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "停止存储源", notes = "停用存储源后不在前台显示")
    @ApiImplicitParam(paramType = "path", name = "storageId", value = "存储源 id", required = true, dataTypeClass = Integer.class)
    @PostMapping("/storage/{storageId}/disable")
    public ResponseResult disable(@PathVariable Integer storageId) {
        StorageSource storageSource = storageSourceService.findById(storageId);
        storageSource.setEnable(false);
        storageSourceService.updateById(storageSource);
        return ResponseResult.success();
    }


    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "更新存储源顺序")
    @PostMapping("/storage/sort")
    public ResponseResult updateStorageSort(@RequestBody List<UpdateStorageSortRequest> updateStorageSortRequestList) {
        storageSourceService.updateStorageSort(updateStorageSortRequestList);
        return ResponseResult.success();
    }


    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "校验存储源 key 是否重复")
    @ApiImplicitParam(paramType = "query", name = "storageKey", value = "存储源 key", required = true, dataTypeClass = String.class)
    @GetMapping("/storage/exist/key")
    public ResponseResult existKey(String storageKey) {
        boolean exist = storageSourceService.existByStorageKey(storageKey);
        return ResponseResult.successData(exist);
    }


    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "修改 readme 兼容模式", notes = "修改 readme 兼容模式是否启用")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "storageId", value = "存储源 id", required = true, dataTypeClass = Integer.class),
        @ApiImplicitParam(paramType = "path", name = "status", value = "存储源兼容模式状态", required = true, dataTypeClass = Boolean.class)
    })
    @PostMapping("/storage/{storageId}/compatibility_readme/{status}")
    public ResponseResult changeCompatibilityReadme(@PathVariable Integer storageId, @PathVariable Boolean status) {
        StorageSource storageSource = storageSourceService.findById(storageId);
        storageSource.setCompatibilityReadme(status);
        storageSourceService.updateById(storageSource);
        return ResponseResult.success();
    }

}
