package com.lanyuanxiaoyao.service.configuration.entity.hudi;

import cn.hutool.core.util.StrUtil;
import java.util.Comparator;
import org.eclipse.collections.api.factory.Maps;
import org.eclipse.collections.api.map.ImmutableMap;

/**
 * Instant
 *
 * @author lanyuanxiaoyao
 * @date 2023-05-01
 */
public class HudiInstant implements Comparable<HudiInstant> {
    private static final ImmutableMap<String, String> COMPARABLE_ACTIONS = Maps.immutable.of("compaction", "commit");
    private static final Comparator<HudiInstant> ACTION_COMPARATOR =
            Comparator.comparing(instant -> getComparableAction(instant.getAction()));
    private static final Comparator<HudiInstant> COMPARATOR =
            Comparator.comparing(HudiInstant::getTimestamp)
                    .thenComparing(ACTION_COMPARATOR)
                    .thenComparing(HudiInstant::getState);
    private String action;
    // REQUESTED, INFLIGHT, COMPLETED, INVALID
    private String state;
    private String timestamp;
    private String fileName;
    private Long fileTime = 0L;
    // active or archive
    private String type;

    public HudiInstant() {
    }

    public HudiInstant(String action, String state, String timestamp, String fileName, String type) {
        this.action = action;
        this.state = state;
        this.timestamp = timestamp;
        this.fileName = fileName;
        this.type = type;
    }

    public HudiInstant(String action, String state, String timestamp, String fileName, Long fileTime, String type) {
        this.action = action;
        this.state = state;
        this.timestamp = timestamp;
        this.fileName = fileName;
        this.fileTime = fileTime;
        this.type = type;
    }

    private static String getComparableAction(String action) {
        return COMPARABLE_ACTIONS.getOrDefault(action, action);
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Long getFileTime() {
        return fileTime;
    }

    public void setFileTime(Long fileTime) {
        this.fileTime = fileTime;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return StrUtil.format("{},{},{},{},{},{}", action, state, timestamp, fileName, fileTime, type);
    }

    @Override
    public int compareTo(HudiInstant o) {
        return COMPARATOR.compare(this, o);
    }
}
