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

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 static String getComparableAction(String action) {
        return COMPARABLE_ACTIONS.getOrDefault(action, action);
    }

    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 String getAction() {
        return action;
    }

    public String getState() {
        return state;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public String getFileName() {
        return fileName;
    }

    public String getType() {
        return type;
    }

    public Long getFileTime() {
        return fileTime;
    }

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

    @Override
    public String toString() {
        return "HudiInstant{" +
                "action='" + action + '\'' +
                ", state='" + state + '\'' +
                ", timestamp='" + timestamp + '\'' +
                ", fileName='" + fileName + '\'' +
                ", type='" + type + '\'' +
                '}';
    }

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