package cn.reghao.devops.mgr.app.controller.page;

import cn.reghao.devops.mgr.app.db.query.AppBuildQuery;
import cn.reghao.devops.mgr.app.db.query.AppDeployQuery;
import cn.reghao.devops.mgr.app.model.po.config.AppConfig;
import cn.reghao.devops.mgr.app.model.po.config.AppDeployConfig;
import cn.reghao.devops.mgr.app.model.vo.AppDeployConfigVO;
import cn.reghao.devops.common.util.KeyValue;
import cn.reghao.devops.mgr.machine.service.MachineQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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/app/deploy")
public class AppDeployConfigPageController {
    private final AppDeployQuery appDeployQuery;
    private final AppBuildQuery appBuildQuery;
    private final MachineQuery machineQuery;

    public AppDeployConfigPageController(AppDeployQuery appDeployQuery, AppBuildQuery appBuildQuery,
                                         MachineQuery machineQuery) {
        this.appDeployQuery = appDeployQuery;
        this.appBuildQuery = appBuildQuery;
        this.machineQuery = machineQuery;
    }

    @ApiOperation(value = "部署配置列表页面", notes = "N")
    @GetMapping("/{appId}")
    public String deployConfigIndexPage(@PathVariable("appId") String appId, Model model) {
        List<AppDeployConfigVO> list = appDeployQuery.getByAppId(appId).stream()
                .map(AppDeployConfigVO::new)
                .collect(Collectors.toList());

        model.addAttribute("appId", appId);
        model.addAttribute("list", list);
        return "/devops/app/deploy/index";
    }

    @ApiOperation(value = "添加部署配置页面", notes = "N")
    @GetMapping("/add/{appId}")
    public String addAppConfigPage(@PathVariable("appId") String appId, Model model) {
        AppConfig appConfig = appBuildQuery.getAppConfig(appId);
        List<KeyValue> machines = machineQuery.getOnlineMachines(appConfig.getEnv());

        model.addAttribute("appId", appId);
        model.addAttribute("packType", appConfig.getPackerConfig().getType());
        model.addAttribute("machines", machines);
        return "/devops/app/deploy/add";
    }

    @ApiOperation(value = "编辑部署配置页面", notes = "N")
    @GetMapping("/edit/{appId}/{machineId}")
    public String editAppConfigPage(@PathVariable("appId") String appId,
                                    @PathVariable("machineId") String machineId,
                                    Model model) {
        AppDeployConfig appDeployConfig = appDeployQuery.getByAppIdAndMachineId(appId, machineId);
        model.addAttribute("appDeployConfig", appDeployConfig);
        return "/devops/app/deploy/edit";
    }

    @ApiOperation(value = "部署配置详情页面", notes = "N")
    @GetMapping("/detail/{appId}/{machineId}")
    public String appConfigPage(@PathVariable("appId") String appId, @PathVariable("machineId") String machineId,
                                Model model) {
        AppDeployConfig appDeployConfig = appDeployQuery.getByAppIdAndMachineId(appId, machineId);
        model.addAttribute("deployConfigVO", new AppDeployConfigVO(appDeployConfig));
        return "/devops/app/deploy/detail";
    }
}
