package cn.reghao.bnt.web.devops.machine.controller;

import cn.reghao.bnt.common.machine.model.CPUInfo;
import cn.reghao.bnt.common.machine.model.DiskInfo;
import cn.reghao.bnt.common.machine.model.MemoryInfo;
import cn.reghao.bnt.common.machine.model.NetworkCard;
import cn.reghao.bnt.web.devops.machine.service.MachineSys;
import cn.reghao.bnt.web.devops.machine.model.vo.MachineDetail;
import cn.reghao.bnt.web.devops.machine.service.MachineQuery;
import cn.reghao.bnt.web.devops.machine.service.MachineService;
import cn.reghao.bnt.web.util.SelectOption;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.web.WebResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Tag(name = "机器节点页面和接口")
@RestController
@RequestMapping("/api/devops/machine/host")
public class MachineController {
    private final MachineQuery machineQuery;
    private final MachineService machineService;
    private final MachineSys machineSys;
    private int pageSize = 10;

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

    @Operation(summary = "机器节点页面", description = "N")
    @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String machineListPage(@RequestParam(value = "env") String env,
                                  @RequestParam(value = "pn") int pageNumber) {
        PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
        Page<MachineDetail> page = machineQuery.getMachineHostByPage(env, pageRequest);

        PageList<MachineDetail> pageList = getPageList(page);
        return WebResult.success(pageList);
    }

    private PageList<MachineDetail> getPageList(Page<MachineDetail> page) {
        int pageNumber = page.getNumber() + 1;
        int pageSize = page.getSize();
        long total = page.getTotalElements();
        List<MachineDetail> list = page.getContent();
        return PageList.pageList(pageNumber, pageSize, (int) total, list);
    }

    @Operation(summary = "设置机器为弃用状态", description = "N")
    @PostMapping(value = "/deprecate", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setMachineDeprecate(String machineId) {
        machineService.setMachineDeprecate(machineId);
        return WebResult.success();
    }

    @Operation(summary = "删除机器", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteMachine(String machineId) {
        if (machineQuery.isAgentOnline(machineId)) {
            return WebResult.failWithMsg("机器处于在线状态, 不能删除");
        }

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

    @Operation(summary = "使用某机器的应用列表页面", description = "N")
    @GetMapping("/app")
    public String machineAppPage(@RequestParam("machineId") String machineId) {
        List<SelectOption> list = machineQuery.getMachineApps(machineId);
        return WebResult.success(list);
    }

    @Operation(summary = "设置机器环境", description = "N")
    @PostMapping(value = "/env", produces = MediaType.APPLICATION_JSON_VALUE)
    public String setMachineEnv(String machineId, String env) {
        machineService.setMachineEnv(machineId, env);
        return WebResult.success();
    }

    @Operation(summary = "机器详情页面", description = "N")
    @GetMapping(value = "/info", produces = MediaType.APPLICATION_JSON_VALUE)
    public String machineInfoPage() {
        machineSys.get();
        CPUInfo cpuInfo = machineSys.getCPUInfo();
        MemoryInfo memoryInfo = machineSys.getMemoryInfo();
        List<DiskInfo> diskInfoList = machineSys.getDiskList();
        List<NetworkCard> networkCardList = machineSys.getNetworkCardList();
        return WebResult.success();
    }
}
