package space.nstart.pcp.pcp_request_service.configuration

import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import space.nstart.pcp.pcp_request_service.utils.UtcDateTimes
import tools.jackson.core.JsonGenerator
import tools.jackson.core.JsonParser
import tools.jackson.databind.DeserializationContext
import tools.jackson.databind.SerializationContext
import tools.jackson.databind.deser.std.StdDeserializer
import tools.jackson.databind.module.SimpleModule
import tools.jackson.databind.ser.std.StdSerializer
import java.time.LocalDateTime

@Configuration
class UtcTimeJsonConfiguration {

    @Bean
    fun ballisticsUtcLocalDateTimeCustomizer(): JsonMapperBuilderCustomizer = JsonMapperBuilderCustomizer { builder ->
        builder.addModule(
            SimpleModule("pcp-ballistics-utc-local-date-time")
                .addSerializer(LocalDateTime::class.java, UtcLocalDateTimeSerializer())
                .addDeserializer(LocalDateTime::class.java, UtcLocalDateTimeDeserializer())
        )
    }
}

private class UtcLocalDateTimeSerializer : StdSerializer<LocalDateTime>(LocalDateTime::class.java) {
    override fun serialize(value: LocalDateTime, gen: JsonGenerator, ctxt: SerializationContext) {
        gen.writeString(UtcDateTimes.format(value))
    }
}

private class UtcLocalDateTimeDeserializer : StdDeserializer<LocalDateTime>(LocalDateTime::class.java) {
    override fun deserialize(p: JsonParser, ctxt: DeserializationContext): LocalDateTime {
        val value = p.valueAsString ?: throw IllegalArgumentException("Expected ISO-8601 UTC date-time string")
        return UtcDateTimes.parse(value)
    }
}
