package net.minecraft.launcher.profile;

import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mojang.authlib.AuthenticationService;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.UserAuthentication;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import net.minecraft.launcher.Launcher;

public class AuthenticationDatabase {
   public static final String DEMO_UUID_PREFIX = "demo-";
   private final Map<String, UserAuthentication> authById;
   private final AuthenticationService authenticationService;

   public AuthenticationDatabase(AuthenticationService authenticationService) {
      this(new HashMap(), authenticationService);
   }

   public AuthenticationDatabase(Map<String, UserAuthentication> authById, AuthenticationService authenticationService) {
      this.authById = authById;
      this.authenticationService = authenticationService;
   }

   public UserAuthentication getByName(String name) {
      if (name == null) {
         return null;
      } else {
         Iterator var2 = this.authById.entrySet().iterator();

         Entry entry;
         GameProfile profile;
         do {
            if (!var2.hasNext()) {
               return null;
            }

            entry = (Entry)var2.next();
            profile = ((UserAuthentication)entry.getValue()).getSelectedProfile();
            if (profile != null && profile.getName().equals(name)) {
               return (UserAuthentication)entry.getValue();
            }
         } while(profile != null || !getUserFromDemoUUID((String)entry.getKey()).equals(name));

         return (UserAuthentication)entry.getValue();
      }
   }

   public UserAuthentication getByUUID(String uuid) {
      return (UserAuthentication)this.authById.get(uuid);
   }

   public Collection<String> getKnownNames() {
      List<String> names = new ArrayList();
      Iterator var2 = this.authById.entrySet().iterator();

      while(var2.hasNext()) {
         Entry<String, UserAuthentication> entry = (Entry)var2.next();
         GameProfile profile = ((UserAuthentication)entry.getValue()).getSelectedProfile();
         if (profile != null) {
            names.add(profile.getName());
         } else {
            names.add(getUserFromDemoUUID((String)entry.getKey()));
         }
      }

      return names;
   }

   public void register(String uuid, UserAuthentication authentication) {
      this.authById.put(uuid, authentication);
   }

   public Set<String> getknownUUIDs() {
      return this.authById.keySet();
   }

   public void removeUUID(String uuid) {
      this.authById.remove(uuid);
   }

   public AuthenticationService getAuthenticationService() {
      return this.authenticationService;
   }

   public static String getUserFromDemoUUID(String uuid) {
      return uuid.startsWith("demo-") && uuid.length() > "demo-".length() ? "Demo User " + uuid.substring("demo-".length()) : "Demo User";
   }

   public static class Serializer implements JsonDeserializer<AuthenticationDatabase>, JsonSerializer<AuthenticationDatabase> {
      private final Launcher launcher;

      public Serializer(Launcher launcher) {
         this.launcher = launcher;
      }

      public AuthenticationDatabase deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
         Map<String, UserAuthentication> services = new HashMap();
         Map<String, Map<String, Object>> credentials = this.deserializeCredentials((JsonObject)json, context);
         YggdrasilAuthenticationService authService = new YggdrasilAuthenticationService(this.launcher.getLauncher().getProxy(), this.launcher.getClientToken().toString());
         Iterator var7 = credentials.entrySet().iterator();

         while(var7.hasNext()) {
            Entry<String, Map<String, Object>> entry = (Entry)var7.next();
            UserAuthentication auth = authService.createUserAuthentication(this.launcher.getLauncher().getAgent());
            auth.loadFromStorage((Map)entry.getValue());
            services.put(entry.getKey(), auth);
         }

         return new AuthenticationDatabase(services, authService);
      }

      protected Map<String, Map<String, Object>> deserializeCredentials(JsonObject json, JsonDeserializationContext context) {
         Map<String, Map<String, Object>> result = new LinkedHashMap();
         Iterator var4 = json.entrySet().iterator();

         while(var4.hasNext()) {
            Entry<String, JsonElement> authEntry = (Entry)var4.next();
            Map<String, Object> credentials = new LinkedHashMap();
            Iterator var7 = ((JsonObject)authEntry.getValue()).entrySet().iterator();

            while(var7.hasNext()) {
               Entry<String, JsonElement> credentialsEntry = (Entry)var7.next();
               credentials.put(credentialsEntry.getKey(), this.deserializeCredential((JsonElement)credentialsEntry.getValue()));
            }

            result.put(authEntry.getKey(), credentials);
         }

         return result;
      }

      private Object deserializeCredential(JsonElement element) {
         Iterator var3;
         if (element instanceof JsonObject) {
            Map<String, Object> result = new LinkedHashMap();
            var3 = ((JsonObject)element).entrySet().iterator();

            while(var3.hasNext()) {
               Entry<String, JsonElement> entry = (Entry)var3.next();
               result.put(entry.getKey(), this.deserializeCredential((JsonElement)entry.getValue()));
            }

            return result;
         } else if (!(element instanceof JsonArray)) {
            return element.getAsString();
         } else {
            List<Object> result = new ArrayList();
            var3 = ((JsonArray)element).iterator();

            while(var3.hasNext()) {
               JsonElement entry = (JsonElement)var3.next();
               result.add(this.deserializeCredential(entry));
            }

            return result;
         }
      }

      public JsonElement serialize(AuthenticationDatabase src, Type typeOfSrc, JsonSerializationContext context) {
         Map<String, UserAuthentication> services = src.authById;
         Map<String, Map<String, Object>> credentials = new HashMap();
         Iterator var6 = services.entrySet().iterator();

         while(var6.hasNext()) {
            Entry<String, UserAuthentication> entry = (Entry)var6.next();
            credentials.put(entry.getKey(), ((UserAuthentication)entry.getValue()).saveForStorage());
         }

         return context.serialize(credentials);
      }
   }
}
