package cn.iocoder.yudao.framework.date;

import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

@JsonComponent
@Slf4j
public class DateJsonSerializerConfig {

    /**
     * 反序列化，其它时区的时间转为本地时间
     */
    public static class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            //1. String类型日期转为LocalDateTime类型
            LocalDateTime localDateTime = DateUtil.parseLocalDateTime(jsonParser.getText(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);

            /*if (请求头中含有timeZone‘时区’字段){


            }*/
            log.info("--------------------1------------------------");
            return localDateTime;
        }
    }

    public static class DateJsonDeserializer extends JsonDeserializer<Date> {
        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
            //1. String类型日期转为LocalDateTime类型
            Date date = DateUtil.parse(jsonParser.getText(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND);

            /*if (请求头中含有timeZone‘时区’字段){


            }*/
            log.info("--------------------2------------------------");

            return date;
        }
    }


    /**
     * 序列化，本地时间转为其它时区的时间
     */
    public static class LocalDateTimeJsonSerializer extends JsonSerializer<LocalDateTime> {
        @Override
        public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            // TODO 本地时间转对应时区的时间
            /*if (请求头中含有timeZone‘时区’字段){
                1. LocalDateTime日期时间转为timeZone时区时间
                2. localDateTime类型转为String类型 return;
            }*/
            log.info("--------------------3------------------------");
            jsonGenerator.writeString(DateUtil.format(localDateTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
        }
    }

    public static class DateJsonSerializer extends JsonSerializer<Date> {
        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            log.info("--------------------4------------------------");
            jsonGenerator.writeString(DateUtil.format(date, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
        }
    }


}
