package com.example.pc_spring_rest_jersey;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class TimeInfo {
	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private int second;
	
	private int dayOfWeek;
	private int dayOfYear;
	
	private String locale;
	private String textShort;
	private String textLong;
	private String textISO;
	
	public TimeInfo() {
		// just to be JAXB-compliant
	}
	
	private TimeInfo(int year, int month, int day, int hour, int minute, int second, int dayOfWeek, int dayOfYear,
			String locale, String textShort, String textLong, String textISO) {
		this.year = year;
		this.month = month;
		this.day = day;
		this.hour = hour;
		this.minute = minute;
		this.second = second;
		this.dayOfWeek = dayOfWeek;
		this.dayOfYear = dayOfYear;
		this.locale = locale;
		this.textShort = textShort;
		this.textLong = textLong;
		this.textISO = textISO;
	}
	
	public static TimeInfo ofZDT(ZonedDateTime zdt, Locale locale) {
		DateTimeFormatter formatShort = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT).withLocale(locale);
		DateTimeFormatter formatLong = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL).withLocale(locale);
		DateTimeFormatter formatIso = DateTimeFormatter.ISO_ZONED_DATE_TIME;

		return new TimeInfo(zdt.getYear(), zdt.getMonth().getValue(), zdt.getDayOfMonth(), zdt.getHour(), zdt.getMinute(), zdt.getSecond(), zdt.getDayOfWeek().getValue(), zdt.getDayOfYear(),
				locale.toLanguageTag(), zdt.format(formatShort), zdt.format(formatLong), zdt.format(formatIso));
	}

	public static TimeInfo ofZDT(ZonedDateTime zdt) {
		return ofZDT(zdt, Locale.getDefault());
	}

	public static TimeInfo now() {
		return ofZDT(ZonedDateTime.now());
	}

	public static TimeInfo now(Locale locale) {
		return ofZDT(ZonedDateTime.now(), locale);
	}

	public static TimeInfo now(ZoneId zone, Locale locale) {
		return ofZDT(ZonedDateTime.now(zone), locale);
	}

	public static TimeInfo now(ZoneId zone) {
		return ofZDT(ZonedDateTime.now(zone));
	}

	public static TimeInfo now(String zone) {
		return ofZDT(ZonedDateTime.now(ZoneId.of(zone)));
	}

	public static TimeInfo now(String zone, String languageTag) {
		ZoneId zoneId = ZoneId.of(zone);
		Locale locale = Locale.forLanguageTag(languageTag);
		return now(zoneId, locale);
	}

	public int getYear() {
		return year;
	}

	public int getMonth() {
		return month;
	}

	public int getDay() {
		return day;
	}

	public int getHour() {
		return hour;
	}

	public int getMinute() {
		return minute;
	}

	public int getSecond() {
		return second;
	}

	public int getDayOfWeek() {
		return dayOfWeek;
	}

	public int getDayOfYear() {
		return dayOfYear;
	}

	public String getLocale() {
		return locale;
	}

	public String getTextShort() {
		return textShort;
	}

	public String getTextLong() {
		return textLong;
	}

	public String getTextISO() {
		return textISO;
	}

	@Override
	public String toString() {
		return "TimeInformation [year=" + year + ", month=" + month + ", day=" + day + ", hour=" + hour + ", minute="
				+ minute + ", second=" + second + ", dayOfWeek=" + dayOfWeek + ", dayOfYear=" + dayOfYear + ", locale="
				+ locale + ", textShort=" + textShort + ", textLong=" + textLong + ", textISO=" + textISO + "]";
	}

}
