package cn.reghao.oss.console.app.controller.page;

import cn.reghao.oss.console.app.model.po.StoreNode;
import cn.reghao.oss.console.app.model.vo.StoreNodeVO;
import cn.reghao.oss.console.app.service.StoreNodeService;
import cn.reghao.oss.console.util.DefaultSetting;
import cn.reghao.oss.console.util.db.PageSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author reghao
 * @date 2024-02-23 11:29:14
 */
@Api(tags = "存储节点页面")
@Controller
@RequestMapping("/store/node")
public class StoreNodePageController {
    private final StoreNodeService storeNodeService;

    public StoreNodePageController(StoreNodeService storeNodeService) {
        this.storeNodeService = storeNodeService;
    }

    @ApiOperation(value = "存储节点列表页面")
    @GetMapping("/list")
    public String storeNodePage(@RequestParam(value = "env", required = false) String env,
                                @RequestParam(value = "type", required = false) String type,
                                @RequestParam(value = "appName", required = false) String appName,
                                Model model) {
        if (env == null) {
            env = DefaultSetting.getDefaultAppEnv();
        }

        if (type == null) {
            type = DefaultSetting.getDefaultAppType();
        }

        if (appName != null) {
            Map<String, String> map = new HashMap<>();
            map.put("appName", appName);
            //List<AppConfig> list = appConfigQuery.query(map);
            List<String> list = Collections.emptyList();
            Page<String> page = new PageImpl<>(list);

            model.addAttribute("env", env);
            model.addAttribute("type", type);
            model.addAttribute("page", page);
            model.addAttribute("list", page.getContent());
            return "/node/index";
        }

        PageRequest pageRequest = PageSort.pageRequest();
        Page<StoreNode> page = storeNodeService.getByPage(pageRequest);

        model.addAttribute("env", env);
        model.addAttribute("type", type);
        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/node/index";
    }

    @ApiOperation(value = "存储节点域名设置页面")
    @GetMapping("/config/{id}")
    public String editStoreNodePage(@PathVariable("id") Integer id, Model model) {
        StoreNode storeNode = storeNodeService.getById(id);
        model.addAttribute("storeNode", storeNode);
        return "/node/edit";
    }

    @ApiOperation(value = "存储节点详情页面")
    @GetMapping("/detail/{id}")
    public String storeNodePage(@PathVariable("id") Integer id, Model model) {
        StoreNodeVO storeNode = storeNodeService.getDetail(id);
        model.addAttribute("storeNode", storeNode);
        return "/node/detail";
    }
}
