package cn.reghao.autodop.dmaster.app.controller.page;

import cn.reghao.autodop.dmaster.app.db.query.config.BuildDirQuery;
import cn.reghao.autodop.dmaster.app.model.po.config.build.BuildDir;
import cn.reghao.autodop.dmaster.machine.model.vo.DiskUsage;
import cn.reghao.autodop.dmaster.util.db.PageList;
import cn.reghao.autodop.dmaster.util.db.PageSort;
import cn.reghao.jdkutil.machine.Disk;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "构建目录配置页面")
@Controller
@RequestMapping("/app/config/build/dir")
public class BuildDirPageController {
    private BuildDirQuery buildDirQuery;

    public BuildDirPageController(BuildDirQuery buildDirQuery) {
        this.buildDirQuery = buildDirQuery;
    }

    @ApiOperation(value = "构建目录页面")
    @GetMapping
    public String buildDirPage(Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<BuildDir> page = buildDirQuery.findByPage(pageRequest);
        PageList<BuildDir> pageList = PageList.pageList(page);

        model.addAttribute("page", page);
        model.addAttribute("list", pageList.getList());
        return "/app/config/builddir/index";
    }

    @GetMapping("/diskinfo")
    public String diskInfoPage(Model model) {
        List<DiskUsage> diskUsages = Disk.details().stream().map(DiskUsage::new).collect(Collectors.toList());
        model.addAttribute("list", diskUsages);
        return "/app/config/builddir/diskinfo";
    }

    @GetMapping("/edit/{id}")
    public String editAppConfigPage(@PathVariable("id") BuildDir buildDir, Model model) {
        model.addAttribute("buildDir", buildDir);
        return "/app/config/builddir/add";
    }
}
