package com.utopiaindustries.qualitychecker.utils.jackson;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeDeserializer extends StdDeserializer<ZonedDateTime> {

	private static final long serialVersionUID = 1L;

	@RequiresApi(api = Build.VERSION_CODES.O)
    public ZonedDateTimeDeserializer() {
		super( LocalDateTime.class );
	}

	@RequiresApi(api = Build.VERSION_CODES.O)
    @Override
	public ZonedDateTime deserialize( JsonParser parser, DeserializationContext ctxt ) throws IOException, JsonProcessingException {
		JsonNode node = parser.getCodec().readTree( parser );
		String dateStr = node.asText();
		DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
		return ZonedDateTime.parse( dateStr, formatter );
	}
}
