package cn.reghao.devops.web.mgr.machine.service.impl;

import cn.reghao.devops.web.mgr.machine.db.repository.MachineHostRepository;
import cn.reghao.devops.web.mgr.machine.db.repository.MachineInfoRepository;
import cn.reghao.devops.web.mgr.machine.model.po.MachineHost;
import cn.reghao.devops.web.mgr.machine.model.po.MachineInfo;
import cn.reghao.devops.web.mgr.machine.model.po.NetworkInfo;
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 org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-10-16 23:15:45
 */
@Service
public class MachineHostPageImpl implements MachineHostPage {
    private final MachineHostRepository machineHostRepository;
    private final MachineInfoRepository machineInfoRepository;

    public MachineHostPageImpl(MachineHostRepository machineHostRepository, MachineInfoRepository machineInfoRepository) {
        this.machineHostRepository = machineHostRepository;
        this.machineInfoRepository = machineInfoRepository;
    }

    @Override
    public Page<MachineHostVO> indexPage(String env, PageRequest pageRequest) {
        return new PageImpl<>(Collections.emptyList());
    }

    @Override
    public Page<MachineHostVO> searchResultPage(String machineIpv4) {
        return new PageImpl<>(Collections.emptyList());
    }

    @Override
    public String setEnvPage(String machineId) {
        return machineHostRepository.findByMachineId(machineId).getEnv();
    }

    @Override
    public MachineSshAuth setSshAuthPage(String machineId) {
        SshAuth sshAuth = machineHostRepository.findByMachineId(machineId).getSshAuth();
        MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
        List<String> ipv4List = machineInfo.getNetworkInfos().stream()
                .map(NetworkInfo::getIpv4).collect(Collectors.toList());
        List<String> ipv4List1 = machineInfo.getNetworkInfos().stream()
                .map(NetworkInfo::getPublicIpv4).collect(Collectors.toList());
        ipv4List.addAll(ipv4List1);
        return new MachineSshAuth(sshAuth, ipv4List);
    }

    @Override
    public void setNotifyPage(String machineId) {
    }

    @Override
    public MachineHost sshTerminalPage(String machineId) {
        return machineHostRepository.findByMachineId(machineId);
    }

    @Override
    public MachineInfoVO hostDetailPage(String machineId) {
        MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
        if (machineInfo != null) {
            return new MachineInfoVO(machineInfo);
        }

        return null;
    }
}
