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

import cn.reghao.devops.web.mgr.app.service.AppDeployService;
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.po.SshAuth;
import cn.reghao.devops.web.mgr.machine.service.MachineService;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.ResultStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author reghao
 * @date 2019-11-15 08:48:04
 */
@Slf4j
@Service
public class MachineServiceImpl implements MachineService {
    private final MachineHostRepository machineHostRepository;
    private final MachineInfoRepository machineInfoRepository;
    private final AppDeployService appDeployService;
    private final MachineQuery machineQuery;

    public MachineServiceImpl(MachineHostRepository machineHostRepository,
                              MachineInfoRepository machineInfoRepository,
                              AppDeployService appDeployService, MachineQuery machineQuery) {
        this.machineHostRepository = machineHostRepository;
        this.machineInfoRepository = machineInfoRepository;
        this.appDeployService = appDeployService;
        this.machineQuery = machineQuery;
    }

    @Override
    public void setEnv(String machineId, String env) {
        MachineHost machineHost = machineHostRepository.findByMachineId(machineId);
        machineHost.setEnv(env);
        machineHostRepository.save(machineHost);
    }

    @Override
    public void setSshAuth(String machineId, SshAuth sshAuth) {
        // TODO 检测主机可达, 端口可达, 协议正确
        String ipv4 = sshAuth.getIpv4();
        int port = sshAuth.getPort();

        MachineHost machineHost = machineHostRepository.findByMachineId(machineId);
        machineHost.setSshAuth(sshAuth);
        machineHostRepository.save(machineHost);
    }

    @Transactional
    public Result delete(String machineId) {
        if (machineQuery.isAgentOnline(machineId)) {
            return Result.result(ResultStatus.FAIL, "机器当前在线, 不能删除");
        }

        machineHostRepository.deleteByMachineId(machineId);
        machineInfoRepository.deleteByMachineId(machineId);
        return Result.result(ResultStatus.SUCCESS);
    }

    private void deleteMachine(String machineId) {
        MachineHost machineHost = machineHostRepository.findByMachineId(machineId);

        // AppDeploying -> AppDeployConfig -> MachineInfo
        // AppDeploying -> AppDeployConfig -> DeployLog
        appDeployService.deleteByMachineHost(machineHost);
        machineHostRepository.deleteByMachineId(machineId);
        machineInfoRepository.deleteByMachineId(machineId);
    }

    @Transactional
    @Override
    public Result updateMachineStat(List<String> machineIds) {
        for (String machineId : machineIds) {
            MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
            if (machineInfo != null && machineInfo.getStat() != 1) {
                machineInfoRepository.updateMachineInfoByMachineId(machineId, 4);
            }
        }

        return Result.result(ResultStatus.SUCCESS);
    }

    @Override
    public void agentShutdown(String machineId) {
        MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
        if (machineInfo != null) {
            machineInfo.setStat(2);
            machineInfoRepository.save(machineInfo);
        }
    }

    @Override
    public void shutdown() {
        List<MachineInfo> list = machineInfoRepository.findByStat(1);
        list.forEach(machineInfo -> {
            machineInfo.setStat(2);
            machineInfoRepository.save(machineInfo);
        });
    }
}
