package at.procon.eventhub.tachographfilesession.service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;

final class TachographTimeParser {

    private TachographTimeParser() {
    }

    static OffsetDateTime combineUtc(LocalDate date, String timeText) {
        if (date == null || timeText == null || timeText.isBlank()) {
            return null;
        }

        String normalized = timeText.trim();
        try {
            OffsetTime offsetTime = OffsetTime.parse(normalized);
            return date.atTime(offsetTime.toLocalTime())
                    .atOffset(offsetTime.getOffset())
                    .withOffsetSameInstant(ZoneOffset.UTC);
        } catch (DateTimeParseException ignored) {
        }

        try {
            return date.atTime(LocalTime.parse(normalized)).atOffset(ZoneOffset.UTC);
        } catch (DateTimeParseException ignored) {
            return null;
        }
    }
}
