package cn.reghao.devops.mgr.machine.controller;

import cn.reghao.devops.common.util.KeyValue;
import cn.reghao.devops.mgr.machine.service.MachineQuery;
import cn.reghao.devops.mgr.machine.service.MachineService;
import cn.reghao.devops.mgr.util.DefaultSetting;
import cn.reghao.devops.mgr.util.PageSort;
import cn.reghao.devops.mgr.builder.model.constant.EnvType;
import cn.reghao.devops.mgr.machine.model.po.MachineHost;
import cn.reghao.devops.mgr.machine.model.vo.MachineDetail;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.web.WebResult;
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.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
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("/machine/host")
public class MachineController {
    private final MachineQuery machineQuery;
    private final MachineService machineService;

    public MachineController(MachineQuery machineQuery, MachineService machineService) {
        this.machineQuery = machineQuery;
        this.machineService = machineService;
    }

    @ApiOperation(value = "机器节点页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping
    public String machineListPage(@RequestParam(value = "env", required = false) String env, Model model) {
        if (env == null) {
            env = DefaultSetting.getDefaultEnv();
        }

        PageRequest pageRequest = PageSort.pageRequest();
        Page<MachineDetail> page = machineQuery.getMachineHostByPage(env, pageRequest);
        model.addAttribute("env", env);
        model.addAttribute("page", page);
        model.addAttribute("list", page.getContent());
        return "/devops/machine/host/index";
    }

    @ApiOperation(value = "设置机器为弃用状态", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/deprecate/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String setMachineDeprecate(@PathVariable("machineId") String machineId) {
        machineService.setMachineDeprecate(machineId);
        return WebResult.success();
    }

    @ApiOperation(value = "删除机器", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @DeleteMapping(value = "/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String deleteMachine(@PathVariable("machineId") String machineId) {
        if (machineQuery.isAgentOnline(machineId)) {
            return WebResult.failWithMsg("机器处于在线状态, 不能删除");
        }

        Result result = machineService.deleteMachine(machineId);
        return WebResult.result(result);
    }

    @ApiOperation(value = "使用某机器的应用列表页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/app/{machineId}")
    public String machineAppPage(@PathVariable("machineId") String machineId, Model model) {
        List<KeyValue> list = machineQuery.getMachineApps(machineId);
        model.addAttribute("list", list);
        return "/devops/machine/host/apps";
    }

    @ApiOperation(value = "机器所属环境设置页面", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @GetMapping("/env/{machineId}")
    public String machineEnvPage(@PathVariable("machineId") String machineId, Model model) {
        List<KeyValue> envs = new ArrayList<>();
        for (EnvType envType : EnvType.values()) {
            envs.add(new KeyValue(envType.name(), envType.name()));
        }

        MachineHost machineHost = machineQuery.getMachineHost(machineId);
        String env = machineHost.getEnv();
        model.addAttribute("machineId", machineId);
        model.addAttribute("environments", envs);
        model.addAttribute("env", env);
        return "/devops/machine/host/env";
    }

    @ApiOperation(value = "设置机器环境", notes = "N")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping(value = "/env/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String setMachineEnv(@PathVariable("machineId") String machineId, @RequestParam("env") String env) {
        machineService.setMachineEnv(machineId, env);
        return WebResult.success();
    }
}
