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.log.BuildConfigSnapshot;
import cn.reghao.devops.mgr.app.model.po.log.BuildConsumed;
import cn.reghao.devops.mgr.app.model.po.log.BuildLog;
import cn.reghao.devops.mgr.app.model.vo.AppBuildingVO;
import cn.reghao.devops.mgr.app.model.vo.AppDeployingNodeVO;
import cn.reghao.devops.mgr.app.model.vo.BuildLogVO;
import cn.reghao.devops.mgr.app.model.vo.BuildTask;
import cn.reghao.devops.mgr.app.service.bd.BuildApp;
import cn.reghao.devops.mgr.util.DefaultSetting;
import cn.reghao.devops.mgr.util.PageSort;
import cn.reghao.devops.common.util.NotAvailable;
import cn.reghao.devops.mgr.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "构建部署页面")
@Controller
@RequestMapping("/app/bd")
public class BuildDeployPageController {
    private final AppBuildQuery appBuildQuery;
    private final AppDeployQuery appDeployQuery;
    private final BuildApp buildApp;

    public BuildDeployPageController(AppBuildQuery appBuildQuery, AppDeployQuery appDeployQuery, BuildApp buildApp) {
        this.appBuildQuery = appBuildQuery;
        this.appDeployQuery = appDeployQuery;
        this.buildApp = buildApp;
    }

    @ApiOperation(value = "应用构建页面", notes = "N")
    @GetMapping(value = "/build")
    public String index(@RequestParam(value = "env", required = false) String env,
                        @RequestParam(value = "type", required = false) String type,
                        Model model) throws Exception {
        if (env == null) {
            env = DefaultSetting.getDefaultEnv();
        }

        if (type == null) {
            type = DefaultSetting.getDefaultAppType();
        }
        PageRequest pageRequest = PageSort.pageRequest();
        Page<AppBuildingVO> page = appBuildQuery.getByPage(env, type, pageRequest);

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

    @ApiOperation(value = "构建任务页面", notes = "N")
    @GetMapping(value = "/task")
    public String buildTaskPage(Model model) {
        List<BuildTask> list = buildApp.getBuildTasks();
        model.addAttribute("list", list);
        return "/devops/app/bd/taskindex";
    }

    @ApiOperation(value = "应用部署页面", notes = "N")
    @GetMapping("/deploy")
    public String deployPage(@RequestParam("appId") String appId,
                             @RequestParam("buildLogId") String buildLogId,
                             Model model) {
        List<AppDeployingNodeVO> list = appDeployQuery.get(appId);
        model.addAttribute("appId", appId);
        model.addAttribute("buildLogId", buildLogId);
        model.addAttribute("list", list);
        return "/devops/app/bd/deploy";
    }

    @ApiOperation(value = "构建结果页面", notes = "N")
    @GetMapping("/build/result")
    public String buildLogResultPage(@RequestParam("buildLogId") String buildLogId, Model model) {
        String content;
        if (NotAvailable.na.getDesc().equals(buildLogId)) {
            content = "构建结果暂不可用";
        } else {
            content = appBuildQuery.getBuildResult(buildLogId);
        }

        String html = StringUtil.txtToHtml(content);
        model.addAttribute("content", html);
        return "/devops/app/bd/log/buildresult";
    }

    @ApiOperation(value = "历史构建页面", notes = "N")
    @GetMapping("/build/history")
    public String buildLogPage(@RequestParam("appId") String appId, Model model) {
        PageRequest pageRequest = PageSort.pageRequest();
        Page<BuildLogVO> buildLogs = appBuildQuery.buildLogVOs(appId, pageRequest);
        model.addAttribute("page", buildLogs);
        model.addAttribute("list", buildLogs.getContent());
        return "/devops/app/bd/log/buildlog";
    }

    @ApiOperation(value = "构建配置页面", notes = "N")
    @GetMapping("/build/config")
    public String buildConfigPage(@RequestParam("buildLogId") String buildLogId, Model model) {
        BuildLog buildLog = appBuildQuery.getBuildLog(buildLogId);
        BuildConfigSnapshot buildConfigSnapshot = buildLog.getBuildConfigSnapshot();
        model.addAttribute("buildConfigSnapshot", buildConfigSnapshot);
        return "/devops/app/bd/log/buildconfig";
    }

    @ApiOperation(value = "构建耗时页面", notes = "N")
    @GetMapping("/build/consumed")
    public String buildTimePage(@RequestParam("buildLogId") String buildLogId, Model model) {
        BuildLog buildLog = appBuildQuery.getBuildLog(buildLogId);
        BuildConsumed buildConsumed = buildLog.getBuildConsumed();
        model.addAttribute("buildConsumed", buildConsumed);
        return "/devops/app/bd/log/buildtime";
    }
}
