package cn.reghao.tnb.file.app.zoss.controller;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.oss.api.constant.ObjectSize;
import cn.reghao.tnb.oss.api.constant.ObjectScope;
import cn.reghao.tnb.oss.api.constant.ObjectType;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.file.app.zoss.model.dto.UploadChannelDto;
import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.file.app.zoss.model.po.UploadChannel;
import cn.reghao.tnb.file.app.zoss.model.po.UserNode;
import cn.reghao.tnb.file.app.zoss.model.vo.AddChannelAttr;
import cn.reghao.tnb.file.app.zoss.service.UploadChannelService;
import cn.reghao.tnb.file.app.zoss.service.UserNodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-02-23 15:05:30
 */
@Slf4j
@Tag(name = "上传通道接口")
@RestController
@RequestMapping("/api/oss/channel")
public class UploadChannelController {
    private final UserNodeService userNodeService;
    private final UploadChannelService uploadChannelService;

    public UploadChannelController(UserNodeService userNodeService, UploadChannelService uploadChannelService) {
        this.userNodeService = userNodeService;
        this.uploadChannelService = uploadChannelService;
    }

    @Operation(summary = "上传通道列表页面", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String index(@RequestParam("userNodeId") Integer userNodeId) {
        int loginUser = (int) UserContext.getUserId();
        List<UserNode> userNodes = userNodeService.getUserNodes(loginUser);
        if (userNodes.isEmpty()) {
            String errMsg = "没有可用节点";
            WebResult.failWithMsg(errMsg);
        }

        List<UploadChannel> list = uploadChannelService.getChannelsByUserNode(userNodeId);
        return WebResult.success(list);
    }

    @Operation(summary = "上传通道属性", description = "N")
    @GetMapping(value = "/attr", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addChannelPage() {
        List<SelectOption> objectTypes = new ArrayList<>();
        for (ObjectType objectType : ObjectType.values()) {
            if (objectType.getCode() == ObjectType.Dir.getCode()) {
                continue;
            }
            objectTypes.add(new SelectOption(objectType.name(), objectType.getCode()+""));
        }

        List<SelectOption> objectScopes = new ArrayList<>();
        for (ObjectScope objectScope : ObjectScope.values()) {
            objectScopes.add(new SelectOption(objectScope.name(), objectScope.getCode()+""));
        }

        List<SelectOption> sizeList = Arrays.stream(ObjectSize.values())
                .map(objectSize -> new SelectOption(objectSize.getDesc(), objectSize.getSize()+""))
                .collect(Collectors.toList());

        AddChannelAttr addChannelAttr = new AddChannelAttr(objectTypes, objectScopes, sizeList);
        return WebResult.success(addChannelAttr);
    }

    @Operation(summary = "初始化上传通道", description = "N")
    @PostMapping(value = "/init", produces = MediaType.APPLICATION_JSON_VALUE)
    public String initUploadChannel() throws Exception {
        uploadChannelService.initUploadChannel();
        return WebResult.success();
    }

    @Operation(summary = "添加上传通道", description = "N")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addUploadChannel(@RequestBody @Validated UploadChannelDto uploadChannelDto) throws Exception {
        Result result = uploadChannelService.addObjectChannel(uploadChannelDto);
        return WebResult.result(result);
    }

    @Operation(summary = "设置通道状态", description = "N")
    @PostMapping(value = "/channel_status/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setChannelStatus(@PathVariable("id") Integer id) {
        uploadChannelService.updateChannelStatus(id);
        return WebResult.success();
    }

    @Operation(summary = "删除通道", description = "N")
    @PostMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteUploadChannel(@PathVariable("id") Integer id) throws Exception {
        Result result = uploadChannelService.deleteChannel(id);
        return WebResult.result(result);
    }
}
