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

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.DefaultSetting;
import cn.reghao.devops.web.util.PageSort;
import cn.reghao.devops.web.mgr.build.model.constant.EnvType;
import cn.reghao.devops.web.mgr.machine.model.SshConnData;
import cn.reghao.devops.web.mgr.machine.model.po.MachineHost;
import cn.reghao.devops.web.mgr.machine.model.po.SshAuth;
import cn.reghao.devops.web.mgr.machine.model.vo.MachineHostVO;
import cn.reghao.devops.web.mgr.machine.model.vo.MachineInfoVO;
import cn.reghao.devops.web.mgr.machine.model.vo.MachineSshAuth;
import cn.reghao.devops.web.mgr.machine.service.MachineHostPage;
import cn.reghao.devops.web.mgr.machine.util.Keys;
import cn.reghao.jutil.jdk.db.PageList;
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.PageImpl;
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 org.springframework.web.bind.annotation.RequestParam;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "机器节点页面")
@Controller
@RequestMapping("/machine/host")
public class MachineHostPageController {
    private final MachineHostPage machineHostPage;
    private final MachineQuery machineQuery;

    public MachineHostPageController(MachineHostPage machineHostPage, MachineQuery machineQuery) {
        this.machineHostPage = machineHostPage;
        this.machineQuery = machineQuery;
    }

    @ApiOperation(value = "机器节点页面")
    @GetMapping
    public String indexPage(@RequestParam(value = "env", required = false) String env,
                           @RequestParam(value = "machineIpv4", required = false) String machineIpv4,
                           Model model) {
        if (env == null) {
            env = DefaultSetting.getDefaultEnv();
        }

        PageRequest pageRequest = PageSort.pageRequest();
        Page<MachineHostVO> page;
        PageList<MachineHostVO> pageList;
        if (machineIpv4 != null) {
            //page = machineHostPage.searchResultPage(machineIpv4);
            pageList = PageList.empty();
        } else {
            int pageNumber = pageRequest.getPageNumber();
            int pageSize = pageRequest.getPageSize();
            pageList = machineQuery.getByPage(env, pageNumber+1, pageSize);
        }

        Page<MachineHostVO> page1 = new PageImpl<>(pageList.getList(), pageRequest, pageList.getTotalSize());
        model.addAttribute("env", env);
        model.addAttribute("page", page1);
        model.addAttribute("list", pageList.getList());
        return "/devops/machine/host/index";
    }

    @ApiOperation(value = "机器所属环境设置页面")
    @GetMapping("/env/{machineId}")
    public String hostEditPage(@PathVariable("machineId") String machineId, Model model) {
        List<KeyValue> envs = new ArrayList<>();
        for (EnvType envType : EnvType.values()) {
            envs.add(new KeyValue(envType.name(), envType.name()));
        }
        String env = machineHostPage.setEnvPage(machineId);
        model.addAttribute("machineId", machineId);
        model.addAttribute("environments", envs);
        model.addAttribute("env", env);
        return "/devops/machine/host/env";
    }

    @ApiOperation(value = "SSH 认证设置页面")
    @GetMapping("/sshauth/{machineId}")
    public String sshAuthPage(@PathVariable("machineId") String machineId, Model model) {
        MachineSshAuth machineSshAuth = machineHostPage.setSshAuthPage(machineId);
        List<KeyValue> ipv4List = machineSshAuth.getIpv4List().stream()
                .map(ipv4 -> new KeyValue(ipv4, ipv4)).collect(Collectors.toList());

        String machineIpv4;
        SshAuth sshAuth = machineSshAuth.getSshAuth();
        if (sshAuth != null) {
            machineIpv4 = sshAuth.getIpv4();
        } else {
            machineIpv4 = ipv4List.get(0).getValue();
        }

        model.addAttribute("machineId", machineId);
        model.addAttribute("machineIpv4", machineIpv4);
        model.addAttribute("ipv4List", ipv4List);
        model.addAttribute("sshAuth", machineSshAuth.getSshAuth());
        return "/devops/machine/host/sshauth";
    }

    @ApiOperation(value = "SSH 控制台页面")
    @GetMapping("/webssh/{machineId}")
    public String webSshPage(@PathVariable("machineId") String machineId, Model model) {
        MachineHost machineHost = machineHostPage.sshTerminalPage(machineId);
        SshAuth sshAuth = machineHost.getSshAuth();
        if (sshAuth == null) {
            String errMsg = "先设置 SSH 认证";
            model.addAttribute("errMsg", errMsg);
            return "/error";
        }

        SshConnData sshConnData = new SshConnData();
        sshConnData.setOps(Keys.OPS_CONNECT);
        sshConnData.setHost(sshAuth.getIpv4());
        sshConnData.setPort(sshAuth.getPort());
        sshConnData.setUsername(sshAuth.getUsername());
        sshConnData.setPassword(sshAuth.getPassword());

        model.addAttribute("sshConnData", sshConnData);
        return "/devops/machine/host/webssh";
    }

    @ApiOperation(value = "机器信息详情页面")
    @GetMapping("/detail/{machineId}")
    public String hostDetailPage(@PathVariable("machineId") String machineId, Model model) {
        MachineInfoVO machineInfoVO = machineHostPage.hostDetailPage(machineId);
        model.addAttribute("machine", machineInfoVO);
        return "/devops/machine/host/detail";
    }

    @ApiOperation(value = "dagnet 日志页面")
    @GetMapping("/agentlog/{machineId}")
    public String agentLogPage(@PathVariable("machineId") String machineId, Model model) {
        model.addAttribute("list", Collections.emptyList());
        return "/devops/machine/host/agentlog";
    }
}
