package com.mojang.launcher.updater;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class LowerCaseEnumTypeAdapterFactory implements TypeAdapterFactory {
   public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
      Class<T> rawType = type.getRawType();
      if (!rawType.isEnum()) {
         return null;
      } else {
         final Map<String, T> lowercaseToConstant = new HashMap();
         Object[] var5 = rawType.getEnumConstants();
         int var6 = var5.length;

         for(int var7 = 0; var7 < var6; ++var7) {
            T constant = var5[var7];
            lowercaseToConstant.put(this.toLowercase(constant), constant);
         }

         return new TypeAdapter<T>() {
            public void write(JsonWriter out, T value) throws IOException {
               if (value == null) {
                  out.nullValue();
               } else {
                  out.value(LowerCaseEnumTypeAdapterFactory.this.toLowercase(value));
               }

            }

            public T read(JsonReader reader) throws IOException {
               if (reader.peek() == JsonToken.NULL) {
                  reader.nextNull();
                  return null;
               } else {
                  return lowercaseToConstant.get(reader.nextString());
               }
            }
         };
      }
   }

   private String toLowercase(Object o) {
      return o.toString().toLowerCase(Locale.US);
   }
}
