package dev.dinauer.maven.maven.core.snapshot.metadata.utils;

import dev.dinauer.maven.maven.core.snapshot.SnapshotBundle;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Comparator;

public class SnapshotComparator implements Comparator<SnapshotBundle>
{
    private static final DateTimeFormatter MAVEN_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd.HHmmss");

    @Override
    public int compare(SnapshotBundle b0, SnapshotBundle b1)
    {
        long b0DateTime = LocalDateTime.parse(String.format("%s.%s", b0.getDate(), b0.getTime()), MAVEN_DATE_TIME_FORMATTER).toEpochSecond(ZoneOffset.UTC);
        long b1DateTime = LocalDateTime.parse(String.format("%s.%s", b1.getDate(), b1.getTime()), MAVEN_DATE_TIME_FORMATTER).toEpochSecond(ZoneOffset.UTC);
        if (b0DateTime == b1DateTime)
        {
            return Integer.compare(b1.getBuildNumber(), b0.getBuildNumber());
        }
        return Long.compare(b1DateTime, b0DateTime);
    }
}
