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

import cn.reghao.oss.web.app.db.repository.StoreNodeRepository;
import cn.reghao.oss.web.app.model.po.StoreNode;
import cn.reghao.oss.web.util.DefaultSetting;
import cn.reghao.oss.web.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 StoreNodeRepository storeNodeRepository;

    public StoreNodePageController(StoreNodeRepository storeNodeRepository) {
        this.storeNodeRepository = storeNodeRepository;
    }

    @ApiOperation(value = "存储节点列表页面")
    @GetMapping("/list")
    public String appConfigPage(@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 = storeNodeRepository.findAll(pageRequest);

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

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

    @ApiOperation(value = "存储节点详情页面")
    @GetMapping("/detail/{id}")
    public String appConfigPage(@PathVariable("id") Integer id, Model model) {
        StoreNode storeNode = storeNodeRepository.findById(id).orElse(null);
        model.addAttribute("storeNode", storeNode);
        return "/node/detail";
    }
}
