package cn.reghao.devops.web.mgr.machine.db.query.impl;

import cn.reghao.devops.common.msg.constant.NodeStatus;
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.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.vo.MachineHostVO;
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.jutil.jdk.db.PageList;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

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

/**
 * @author reghao
 * @date 2024-08-04 19:59:20
 */
@Service
public class MachineQueryImpl implements MachineQuery {
    private final MachineInfoRepository machineInfoRepository;
    private final MachineHostRepository machineHostRepository;

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

    @Override
    public PageList<MachineHostVO> getByPage(String env, int pageNumber, int pageSize) {
        PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
        Page<MachineInfo> machineInfos = machineInfoRepository.findAll(pageRequest);
        List<MachineHostVO> list1 = machineInfos.stream().map(machineInfo -> {
            MachineHost machineHost = new MachineHost(machineInfo);
            long bootTime = machineInfo.getBootTime();
            String bootTimeStr = DateTimeConverter.format(bootTime*1000);
            String status = NodeStatus.Online.name();
            int stat = machineInfo.getStat();
            if (stat == NodeStatus.Offline.getCode()) {
                status = NodeStatus.Offline.name();
            } else if (stat == NodeStatus.Timeout.getCode()) {
                status = NodeStatus.Timeout.name();
            } else if (stat == NodeStatus.Deprecated.getCode()) {
                status = NodeStatus.Deprecated.name();
            }

            return new MachineHostVO(machineHost, bootTimeStr, status);
        }).collect(Collectors.toList());
        return PageList.pageList(pageNumber, pageSize, (int) machineInfos.getTotalElements(), list1);
    }

    @Override
    public boolean isAgentOnline(String machineId) {
        MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
        if (machineInfo == null) {
            return false;
        }

        return machineInfo.getStat() == 1;
    }

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

    @Override
    public List<KeyValue> onlineMachines(String env) {
        return machineHostRepository.findByEnv(env).stream()
                .map(machineHost -> new KeyValue(machineHost.getMachineId(), machineHost.getMachineIpv4()))
                .collect(Collectors.toList());
    }
}
