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


import com.beust.jcommander.internal.Maps;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
   public static final String YYYY_MM_DD_HH_MM_SS = "yyyy$1MM$2dd$3HH$4mm$5ss$6";
   public static final String YYYY_MM_DD_HH_MM = "yyyy$1MM$2dd$3HH$4mm$5";
   public static final String YYYY_MM_DD_HH = "yyyy$1MM$2dd$3HH$4";
   public static final String YYYY_MM_DD = "yyyy$1MM$2dd$3";
   public static final String YYYY_MM = "yyyy$1MM$2";
   public static final String HH_MM_SS = "HH$1mm$2ss$3";
   public static final String HH_MM = "HH$1mm$2";
   private static final Map<String, Pattern> patternMap = Maps.newLinkedHashMap();

   public static void main(String[] args) {
      System.out.println(getPattern("2014/10/10 11:11:11"));
      System.out.println(getPattern("2014-10-10"));
      System.out.println(getPattern("2014-10-10 11:11"));
      System.out.println(getPattern("2014-10"));
      System.out.println(getPattern("2014年10月10日 11点11分11秒"));
      System.out.println(getPattern("11:11:11"));
      System.out.println(getPattern("11点11分"));
   }

   public static String getPattern(String dateString) {
      Iterator var1 = patternMap.entrySet().iterator();

      while(var1.hasNext()) {
         Map.Entry<String, Pattern> entry = (Map.Entry)var1.next();
         Matcher matcher = ((Pattern)entry.getValue()).matcher(dateString);
         if (matcher.find()) {
            if ("yyyy$1MM$2dd$3HH$4mm$5ss$6".equals(entry.getKey())) {
               return "yyyy$1MM$2dd$3HH$4mm$5ss$6".replace("$1", matcher.group(1)).replace("$2", matcher.group(2)).replace("$3", matcher.group(3)).replace("$4", matcher.group(4)).replace("$5", matcher.group(5)).replace("$6", matcher.group(6));
            }

            if ("yyyy$1MM$2dd$3HH$4mm$5".equals(entry.getKey())) {
               return "yyyy$1MM$2dd$3HH$4mm$5".replace("$1", matcher.group(1)).replace("$2", matcher.group(2)).replace("$3", matcher.group(3)).replace("$4", matcher.group(4)).replace("$5", matcher.group(5));
            }

            if ("yyyy$1MM$2dd$3HH$4".equals(entry.getKey())) {
               return "yyyy$1MM$2dd$3HH$4".replace("$1", matcher.group(1)).replace("$2", matcher.group(2)).replace("$3", matcher.group(3)).replace("$4", matcher.group(4));
            }

            if ("yyyy$1MM$2dd$3".equals(entry.getKey())) {
               return "yyyy$1MM$2dd$3".replace("$1", matcher.group(1)).replace("$2", matcher.group(2)).replace("$3", matcher.group(3));
            }

            if ("yyyy$1MM$2".equals(entry.getKey())) {
               return "yyyy$1MM$2".replace("$1", matcher.group(1)).replace("$2", matcher.group(2));
            }

            if ("HH$1mm$2ss$3".equals(entry.getKey())) {
               return "HH$1mm$2ss$3".replace("$1", matcher.group(1)).replace("$2", matcher.group(2)).replace("$3", matcher.group(3));
            }

            if ("HH$1mm$2".equals(entry.getKey())) {
               return "HH$1mm$2".replace("$1", matcher.group(1)).replace("$2", matcher.group(2));
            }
         }
      }

      return null;
   }

   @Override
   public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
      if (StringUtils.isBlank(p.getText())) {
         return null;
      } else {
         String pattern = getPattern(p.getText());
         if (pattern == null) {
            throw new RuntimeException("不支持的日期格式:" + p.getText());
         } else {
            return LocalDateTime.parse(p.getText(), DateTimeFormatter.ofPattern(pattern));
         }
      }
   }

   static {
      patternMap.put("yyyy$1MM$2dd$3HH$4mm$5ss$6", Pattern.compile("^\\d{4}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D?)$"));
      patternMap.put("yyyy$1MM$2dd$3HH$4mm$5", Pattern.compile("^\\d{4}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D?)$"));
      patternMap.put("yyyy$1MM$2dd$3HH$4", Pattern.compile("^\\d{4}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D?)$"));
      patternMap.put("yyyy$1MM$2dd$3", Pattern.compile("^\\d{4}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D?)$"));
      patternMap.put("yyyy$1MM$2", Pattern.compile("^\\d{4}(\\D+)\\d{1,2}(\\D?)$"));
      patternMap.put("HH$1mm$2ss$3", Pattern.compile("^\\d{1,2}(\\D+)\\d{1,2}(\\D+)\\d{1,2}(\\D?)$"));
      patternMap.put("HH$1mm$2", Pattern.compile("^\\d{1,2}(\\D+)\\d{1,2}(\\D?)$"));
   }
}
