package org.springblade.etl.enums;

import java.util.stream.Stream;

import org.apache.flink.api.common.JobStatus;

import com.baomidou.mybatisplus.annotation.EnumValue;

import cn.hutool.core.util.ArrayUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum InstanceStatus {

	WAITING(0, "待运行", new JobStatus[] {}),
	RUNNING(1, "运行中", new JobStatus[] {JobStatus.INITIALIZING, JobStatus.CREATED, JobStatus.RUNNING, JobStatus.RECONCILING, JobStatus.RESTARTING, JobStatus.CANCELLING, JobStatus.FAILING}),
	SUCCESS(2, "成功", new JobStatus[] {JobStatus.FINISHED}),
	FAILURE(3, "失败", new JobStatus[] {JobStatus.FAILED}),
	CANCELED(4, "中断", new JobStatus[] {JobStatus.CANCELED, JobStatus.SUSPENDED}),
	UNKNOWN(5, "未知", new JobStatus[0]),
    ;

    @EnumValue
    private Integer value;
    private String label;
    private JobStatus[] jobStatus;
    
    private final static InstanceStatus[] instanceStatusArray = InstanceStatus.values();
    
    /**
     * 
     * @param value
     * @return
     */
    public static InstanceStatus of(Integer value) {
    	return Stream.of(instanceStatusArray).filter(item -> item.getValue().equals(value)).findAny().orElse(InstanceStatus.UNKNOWN);
    }
    
    /**
     * 是否已结束
     * @param jobStatus
     * @return
     */
    public static boolean isDone(JobStatus jobStatus) {
    	return ArrayUtil.contains( new JobStatus[] {JobStatus.FAILED, JobStatus.CANCELED, JobStatus.FINISHED}, jobStatus);
    }
    
    /**
     * 
     * @param jobStatus
     * @return
     */
    public static InstanceStatus of(JobStatus jobStatus) {
    	return Stream.of(instanceStatusArray).filter(item -> ArrayUtil.contains(item.getJobStatus(), jobStatus)).findAny().orElse(InstanceStatus.UNKNOWN);
    }
}
