package cn.reghao.autodop.dmaster.app.service.impl;

import cn.reghao.autodop.common.dagent.app.log.LogFile;
import cn.reghao.autodop.common.mqtt.DefaultMqttClient;
import cn.reghao.autodop.common.msg.rpc.dto.app.AppIdParam;
import cn.reghao.autodop.dmaster.app.db.query.config.AppConfigQuery;
import cn.reghao.autodop.dmaster.app.db.repository.AppRunningRepository;
import cn.reghao.autodop.dmaster.app.model.constant.StatusOps;
import cn.reghao.autodop.dmaster.app.model.po.AppRunning;
import cn.reghao.autodop.dmaster.app.model.po.config.AppConfig;
import cn.reghao.autodop.dmaster.app.service.AppStatusService;
import cn.reghao.jdkutil.serializer.JsonConverter;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 应用状态管理
 *
 * @author reghao
 * @date 2019-11-27 11:30:28
 */
@Service
public class AppStatusServiceImpl implements AppStatusService {
    private DefaultMqttClient mqttClient;
    private AppRunningRepository runningRepository;
    private AppConfigQuery appConfigQuery;

    public AppStatusServiceImpl(DefaultMqttClient mqttClient, AppRunningRepository runningRepository,
                                AppConfigQuery appConfigQuery) {
        this.mqttClient = mqttClient;
        this.runningRepository = runningRepository;
        this.appConfigQuery = appConfigQuery;
    }

    @Override
    public void changeStatus(String appId, String machineId, StatusOps statusOps) {
        AppConfig app = appConfigQuery.findByAppId(appId);
        switch (statusOps) {
            case restart:
                break;
            case stop:
                break;
            case start:
                break;
            default:
                ;
        }
    }

    @Override
    public void refreshAppStatus() {
        List<AppRunning> appRunnings = runningRepository.findAll();
        appRunnings.forEach(appRunning -> {
            String appId = appRunning.getAppId();
            AppConfig app = appConfigQuery.findByAppId(appId);
            String machineId = appRunning.getMachineId();
            getStatus(app, machineId);
        });
    }

    @Override
    public void refreshAppStatus(String appId) {
        List<AppRunning> appRunnings = runningRepository.findByAppId(appId);
    }

    @Override
    public void refreshAppStatus(AppRunning appRunning) {
        String appId = appRunning.getAppId();
        AppConfig app = appConfigQuery.findByAppId(appId);
        String machineId = appRunning.getMachineId();
        getStatus(app, machineId);
    }

    @Override
    public void getStatus(AppConfig app, String machineId) {
    }

    /**
     * 获取应用日志文件列表
     *
     * @param
     * @return
     * @date 2021-02-26 上午8:23
     */
    @Override
    public List<LogFile> logFiles(String appId, String machineId) {
        AppConfig app = appConfigQuery.findByAppId(appId);
        return null;
    }

    /**
     * 获取应用日志内容
     *
     * @param
     * @return
     * @date 2021-02-26 上午8:23
     */
    @Override
    public List<String> logContent(String appId, String logType, String logLevel, String machineId, String logFile) {
        AppConfig app = appConfigQuery.findByAppId(appId);
        return null;
    }

    private List<String> getAppLog(String appId, String logType, String logLevel, String machineId, String logFile) {
        return new ArrayList<>();
    }
}
