package com.microservices.mon.k8s.domain;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.mon.k8s.domain.vo.RancherApplicationVo;
import lombok.Getter;

/**
 * @author OTTO
 */
@Getter
public enum ApplicationStartAndStopType {
    START("start", "启动", false),
    STOP("stop", "停止", false),
    FORCE_STOP("forceStop", "强制停止", false),
    RESTART("restart", "重启", true),
    PAUSED("paused", "暂停", true),
    RESUME("resume", "恢复", true);
    private final String key;
    private final String name;
    private final Boolean isDaemonSetOperation;

    ApplicationStartAndStopType(String key, String name, Boolean isDaemonSetOperation) {
        this.key = key;
        this.name = name;
        this.isDaemonSetOperation = isDaemonSetOperation;
    }

    public static JSONArray getOperationList(String kind, K8sApplication k8sApplication) {
        JSONArray jsonArray = new JSONArray();
        if (k8sApplication == null) {
            return jsonArray;
        }
        for (ApplicationStartAndStopType value : values()) {
            if ("DaemonSet".equals(kind) && !value.isDaemonSetOperation) {
                continue;
            }
            boolean shouldAdd = false;

            switch (value) {
                case START:
                    if (k8sApplication.getReplicas() == null || k8sApplication.getReplicas() == 0) {
                        shouldAdd = true;
                    }
                    break;
                case STOP:
                case FORCE_STOP:
                case RESTART:
                    if (k8sApplication.getReplicas() != null && k8sApplication.getReplicas() > 0) {
                        shouldAdd = true;
                    }
                    break;
                case PAUSED:
                    if (!k8sApplication.getPaused()) {
                        shouldAdd = true;
                    }
                    break;
                case RESUME:
                    if (k8sApplication.getPaused()) {
                        shouldAdd = true;
                    }
                    break;
            }

            if (shouldAdd) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("operationKey", value.getKey());
                jsonObject.put("operationName", value.getName());
                jsonArray.add(jsonObject);
            }
        }
        return jsonArray;
    }

    public static boolean containsKey(String operationKey) {
        for (ApplicationStartAndStopType value : values()) {
            if (StringUtils.equals(value.getKey(), operationKey)) {
                return true;
            }
        }
        return false;
    }

    public static ApplicationStartAndStopType getByKey(String operationKey) {
        for (ApplicationStartAndStopType value : values()) {
            if (StringUtils.equals(value.getKey(), operationKey)) {
                return value;
            }
        }
        return null;
    }
}
