package net.minecraft.launcher.updater;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.launcher.OperatingSystem;
import com.mojang.launcher.updater.DateTypeAdapter;
import com.mojang.launcher.updater.LowerCaseEnumTypeAdapterFactory;
import com.mojang.launcher.versions.CompleteVersion;
import com.mojang.launcher.versions.ReleaseType;
import com.mojang.launcher.versions.ReleaseTypeAdapterFactory;
import com.mojang.launcher.versions.Version;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.launcher.game.MinecraftReleaseType;
import net.minecraft.launcher.game.MinecraftReleaseTypeFactory;

public abstract class VersionList {
   protected final Gson gson;
   protected final Map<String, Version> versionsByName = new HashMap();
   protected final List<Version> versions = new ArrayList();
   protected final Map<MinecraftReleaseType, Version> latestVersions = Maps.newEnumMap(MinecraftReleaseType.class);

   public VersionList() {
      GsonBuilder builder = new GsonBuilder();
      builder.registerTypeAdapterFactory(new LowerCaseEnumTypeAdapterFactory());
      builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
      builder.registerTypeAdapter(ReleaseType.class, new ReleaseTypeAdapterFactory(MinecraftReleaseTypeFactory.instance()));
      builder.registerTypeAdapter(Argument.class, new Argument.Serializer());
      builder.enableComplexMapKeySerialization();
      builder.setPrettyPrinting();
      this.gson = builder.create();
   }

   public Collection<Version> getVersions() {
      return this.versions;
   }

   public Version getLatestVersion(MinecraftReleaseType type) {
      if (type == null) {
         throw new IllegalArgumentException("Type cannot be null");
      } else {
         return (Version)this.latestVersions.get(type);
      }
   }

   public Version getVersion(String name) {
      if (name != null && name.length() != 0) {
         return (Version)this.versionsByName.get(name);
      } else {
         throw new IllegalArgumentException("Name cannot be null or empty");
      }
   }

   public abstract CompleteMinecraftVersion getCompleteVersion(Version var1) throws IOException;

   protected void replacePartialWithFull(PartialVersion version, CompleteVersion complete) {
      Collections.replaceAll(this.versions, version, complete);
      this.versionsByName.put(version.getId(), complete);
      if (this.latestVersions.get(version.getType()) == version) {
         this.latestVersions.put(version.getType(), complete);
      }

   }

   protected void clearCache() {
      this.versionsByName.clear();
      this.versions.clear();
      this.latestVersions.clear();
   }

   public abstract void refreshVersions() throws IOException;

   public CompleteVersion addVersion(CompleteVersion version) {
      if (version.getId() == null) {
         throw new IllegalArgumentException("Cannot add blank version");
      } else if (this.getVersion(version.getId()) != null) {
         throw new IllegalArgumentException("Version '" + version.getId() + "' is already tracked");
      } else {
         this.versions.add(version);
         this.versionsByName.put(version.getId(), version);
         return version;
      }
   }

   public void removeVersion(String name) {
      if (name != null && name.length() != 0) {
         Version version = this.getVersion(name);
         if (version == null) {
            throw new IllegalArgumentException("Unknown version - cannot remove null");
         } else {
            this.removeVersion(version);
         }
      } else {
         throw new IllegalArgumentException("Name cannot be null or empty");
      }
   }

   public void removeVersion(Version version) {
      if (version == null) {
         throw new IllegalArgumentException("Cannot remove null version");
      } else {
         this.versions.remove(version);
         this.versionsByName.remove(version.getId());
         MinecraftReleaseType[] var2 = MinecraftReleaseType.values();
         int var3 = var2.length;

         for(int var4 = 0; var4 < var3; ++var4) {
            MinecraftReleaseType type = var2[var4];
            if (this.getLatestVersion(type) == version) {
               this.latestVersions.remove(type);
            }
         }

      }
   }

   public void setLatestVersion(Version version) {
      if (version == null) {
         throw new IllegalArgumentException("Cannot set latest version to null");
      } else {
         this.latestVersions.put((MinecraftReleaseType)version.getType(), version);
      }
   }

   public void setLatestVersion(String name) {
      if (name != null && name.length() != 0) {
         Version version = this.getVersion(name);
         if (version == null) {
            throw new IllegalArgumentException("Unknown version - cannot set latest version to null");
         } else {
            this.setLatestVersion(version);
         }
      } else {
         throw new IllegalArgumentException("Name cannot be null or empty");
      }
   }

   public String serializeVersion(CompleteVersion version) {
      if (version == null) {
         throw new IllegalArgumentException("Cannot serialize null!");
      } else {
         return this.gson.toJson((Object)version);
      }
   }

   public abstract boolean hasAllFiles(CompleteMinecraftVersion var1, OperatingSystem var2);

   public void uninstallVersion(Version version) {
      this.removeVersion(version);
   }
}
