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.common.auth.UserContext;
import cn.reghao.tnb.file.app.zoss.model.dto.NodeAddDto;
import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.file.app.zoss.model.dto.NodeUpdateDto;
import cn.reghao.tnb.file.app.zoss.model.po.UserNode;
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 org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Tag(name = "用户节点接口")
@RestController
@RequestMapping("/api/oss/my")
public class UserNodeController {
    private final UserNodeService userNodeService;

    public UserNodeController(UserNodeService userNodeService) {
        this.userNodeService = userNodeService;
    }

    @Operation(summary = "用户节点列表", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String userNodesPage() {
        int loginUser = (int) UserContext.getUserId();
        List<UserNode> list = userNodeService.getUserNodes(loginUser);
        return WebResult.success(list);
    }

    @Operation(summary = "用户节点 kv 列表", description = "N")
    @GetMapping(value = "/kv", produces = MediaType.APPLICATION_JSON_VALUE)
    public String userNodeKeyValue() {
        long loginUser = UserContext.getUserId();
        List<SelectOption> storeNodes = userNodeService.getNodeKeyValues(loginUser);
        return WebResult.success(storeNodes);
    }

    @Operation(summary = "添加用户节点", description = "N")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public String addUserNode(@RequestBody @Validated NodeAddDto nodeAddDto) {
        Result result = userNodeService.add(nodeAddDto);
        return WebResult.result(result);
    }

    @Operation(summary = "更新用户节点", description = "N")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateNode(@RequestBody @Validated NodeUpdateDto nodeUpdateDto) {
        userNodeService.updateUserNode(nodeUpdateDto);
        return WebResult.success();
    }

    @Operation(summary = "删除用户节点", description = "N")
    @PostMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteUserNode(@PathVariable("id") Integer userNodeId) {
        Result result = userNodeService.delete(userNodeId);
        return WebResult.result(result);
    }
}
