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

import cn.reghao.devops.web.mgr.app.db.query.AppBuildQuery;
import cn.reghao.devops.web.mgr.app.db.query.AppDeployQuery;
import cn.reghao.devops.web.mgr.app.model.po.config.AppConfig;
import cn.reghao.devops.web.mgr.app.model.po.config.AppDeployConfig;
import cn.reghao.devops.web.mgr.app.model.vo.AppDeployConfigVo;
import cn.reghao.devops.web.mgr.app.model.vo.KeyValue;
import cn.reghao.devops.web.mgr.machine.db.query.MachineQuery;
import cn.reghao.devops.web.util.PageSort;
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;

/**
 * @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 = "部署配置页面")
    @GetMapping("/{appId}")
    public String deployConfigIndexPage(@PathVariable("appId") String appId, Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<AppDeployConfigVo> page = appDeployQuery.indexPage(appId, pageRequest);
        List<AppDeployConfigVo> list = page.getContent();

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

    @GetMapping("/add/{appId}")
    public String addAppConfigPage(@PathVariable("appId") String appId, Model model) {
        AppConfig appConfig = appBuildQuery.getAppConfig(appId);
        List<KeyValue> machines = machineQuery.onlineMachines(appConfig.getEnv());

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

    @GetMapping("/edit/{appId}/{machineId}")
    public String editAppConfigPage(@PathVariable("appId") String appId,
                                    @PathVariable("machineId") String machineId,
                                    Model model) {
        AppDeployConfig appDeployConfig = appDeployQuery.findByAppIdAndMachineId(appId, machineId);
        model.addAttribute("appDeployConfig", appDeployConfig);
        return "/devops/app/deploy/edit";
    }

    @GetMapping("/detail/{appId}/{machineId}")
    public String appConfigPage(@PathVariable("appId") String appId, @PathVariable("machineId") String machineId,
                                Model model) {
        AppDeployConfig appDeployConfig = appDeployQuery.findByAppIdAndMachineId(appId, machineId);
        model.addAttribute("deployConfigVO", new AppDeployConfigVo(appDeployConfig));
        return "/devops/app/deploy/detail";
    }
}
