package org.texttechnologylab.project.gruppe_05_1.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public abstract class GeneralUtils {

    /**
     *
     * @param integer the integer to be parsed
     * @return the parsed integer or null if the integer could not be parsed
     */
    public static Integer parseInt(String integer) {

        try {
            return Integer.parseInt(integer);
        } catch (NumberFormatException ex) {
            return null;
        }
    }

    /**
     * Parse a date in the format used in Germany
     * @param date the date to be parsed
     * @return the parsed date or null if the date could not be parsed
     */
    public static LocalDate parseDate(String date) {

        try {
            return LocalDate.parse(date, DateTimeFormatter.ofPattern("dd.MM.yyyy"));
        } catch (DateTimeParseException ex) {
            return null;
        }
    }


    /**
     * Parse tiem in a give format
     * @param date the time to be parsed
     * @param timeFormat the format of the time
     * @return the parsed time or null if the time could not be parsed
     */

    public static LocalTime parseTime(String date, String timeFormat) {

        try {
            return LocalTime.parse(date, DateTimeFormatter.ofPattern(timeFormat));
        } catch (DateTimeParseException ex) {
            return null;
        }
    }

    /**
     * Parse a Formatiere Datumsfelder wie in Deutschland üblich ist.
     * @param date the date to be formatted
     * @return the formatted date or an empty string if the date is null
     */
    public static String formatDate(LocalDate date) {

        if (null== date) return "";
        return date.format(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
    }

    /**
     * Format time in the format used in Germany
     * @param time the time to be formatted
     * @return the formatted time or an empty string if the time is null
     */
    public static String formatTime(LocalTime time) {

        if (null== time) return "";
        return time.format(DateTimeFormatter.ofPattern("HH.mm"));
    }

    public static LocalDateTime parseDateTime(String dateTime, String timeFormat) {
        try {
            return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(timeFormat));
        } catch (DateTimeParseException ex) {
            return null;
        }
    }
}
