package com.bonus.common.houqin.framework.config.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class DateSerializer extends JsonSerializer<LocalDate> {
   private final String pattern;

   public DateSerializer(String pattern) {
      this.pattern = pattern;
   }

   @Override
   public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
      String format = value.format(DateTimeFormatter.ofPattern(this.pattern));
      format = format.replace("Mon", " 周一").replace("Tues", " 周二").replace("Wed", " 周三").replace("Thur", " 周四").replace("Fri", " 周五").replace("Sat", " 周六").replace("Sun", " 周日");
      gen.writeString(format);
   }

   public static class YYYY extends DateSerializer {
      public YYYY() {
         super("yyyy");
      }
   }

   public static class YYYY_MM extends DateSerializer {
      public YYYY_MM() {
         super("yyyy-MM");
      }
   }

   public static class YYYY_MM_DD_WEEK extends DateSerializer {
      public YYYY_MM_DD_WEEK() {
         super("yyyy-MM-dd E");
      }
   }

   public static class YYYY_MM_DD extends DateSerializer {
      public YYYY_MM_DD() {
         super("yyyy-MM-dd");
      }
   }
}
