package cn.reghao.bnt.web.devops.app.service.impl;

import cn.reghao.bnt.common.agent.app.dto.AppInfo;
import cn.reghao.bnt.common.msg.constant.AppStatOps;
import cn.reghao.bnt.common.msg.event.EvtAppStat;
import cn.reghao.bnt.web.devops.app.db.query.AppBuildQuery;
import cn.reghao.bnt.web.devops.app.model.po.config.AppConfig;
import cn.reghao.bnt.web.devops.app.service.AppRunService;
import cn.reghao.bnt.web.devops.app.service.PermissionCheck;
import cn.reghao.bnt.web.devops.machine.service.MachineQuery;
import cn.reghao.bnt.web.ws.WsSender;
import cn.reghao.jutil.jdk.event.message.EventMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * 应用状态管理
 *
 * @author reghao
 * @date 2019-11-27 11:30:28
 */
@Slf4j
@Service
public class AppRunServiceImpl implements AppRunService {
    private final WsSender wsSender;
    private final AppBuildQuery appBuildQuery;
    private final MachineQuery machineQuery;
    private final PermissionCheck permissionCheck;

    public AppRunServiceImpl(WsSender wsSender, AppBuildQuery appBuildQuery,
                             MachineQuery machineQuery, PermissionCheck permissionCheck) {
        this.wsSender = wsSender;
        this.appBuildQuery = appBuildQuery;
        this.machineQuery = machineQuery;
        this.permissionCheck = permissionCheck;
    }

    @Override
    public void changeStatus(String appId, String machineId, AppStatOps appStatOps) throws Exception {
        AppConfig appConfig = appBuildQuery.getAppConfig(appId);
        String packType = appConfig.getPackerConfig().getType();
        EvtAppStat evtAppStat = new EvtAppStat(packType, appId, appStatOps);
        EventMessage evtMsg = EventMessage.evt(evtAppStat);

        permissionCheck.check(appConfig.getEnv());
        publish(machineId, evtMsg);
    }

    @Override
    public void getStatus(String appId, String machineId) {
        AppConfig appConfig = appBuildQuery.getAppConfig(appId);
        String packType = appConfig.getPackerConfig().getType();
        EvtAppStat evtAppStat = new EvtAppStat(packType, appId, AppStatOps.stat);
        EventMessage evtMsg = EventMessage.evt(evtAppStat);
        publish(machineId, evtMsg);
    }

    @Override
    public void getStat(String machineId, List<AppInfo> appInfos) {
        appInfos.forEach(appInfo -> {
            String appId = appInfo.getAppId();
            String packType = appInfo.getPackType();

            EvtAppStat evtAppStat = new EvtAppStat(packType, appId, AppStatOps.stat);
            EventMessage evtMsg = EventMessage.evt(evtAppStat);
            publish(machineId, evtMsg);
        });
    }

    private void publish(String machineId, EventMessage evtMsg) {
        boolean online = machineQuery.isAgentOnline(machineId);
        if (!online) {
            log.error("{} 不在线", machineId);
            return;
        }

        try {
            wsSender.send(machineId, evtMsg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
