package parser;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public interface Parser {

	public static final String df = "yyyy-MM-dd";
	static final Pattern timePattern = Pattern.compile("\\d+[,]{0,1}\\d+");
	static final Pattern timeModifierPattern = Pattern.compile("[A-Za-z]+$");

	public static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(df);

	default String getTimeModifierFromTimeCellValue(final String timeCell) {
		final String timeModifier;
		final Matcher timeModifierMatcher = timeModifierPattern.matcher(timeCell);
		if (timeModifierMatcher.find()) {
			timeModifier = timeModifierMatcher.group();
		} else {
			timeModifier = "";
		}
		return timeModifier;
	}

	default float getTimeFromTimeCellValue(final String timeCell) {
		final float time;
		final Matcher timeMatcher = timePattern.matcher(timeCell);
		if (timeMatcher.find()) {
			time = Float.valueOf(timeMatcher.group().replace(",", "."));
		} else {
			time = -1f;
		}
		return time;
	}

	default String getTodaysDate() {
		return DATE_FORMAT.format(new Date());
	}

	default String getLastDayOfMonth() {
		final LocalDate lastDayOfMonth = LocalDate.parse(getTodaysDate(), DateTimeFormatter.ofPattern(df))
				.with(TemporalAdjusters.lastDayOfMonth());

		return DATE_FORMAT.format(Date.from(lastDayOfMonth.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
	}

}
