package net.mc.main;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Util {
   private static File workDir = null;

   public static File getWorkingDirectory() {
      if (workDir == null) {
         workDir = getWorkingDirectory("minecraft");
      }

      return workDir;
   }

   public static File getWorkingDirectory(String applicationName) {
      String userHome = System.getProperty("user.home", ".");
      File workingDirectory;
      switch(getPlatform().ordinal()) {
      case 0:
      case 1:
         workingDirectory = new File(userHome, '.' + applicationName + '/');
         break;
      case 2:
         String applicationData = System.getenv("APPDATA");
         if (applicationData != null) {
            workingDirectory = new File(applicationData, "." + applicationName + '/');
         } else {
            workingDirectory = new File(userHome, '.' + applicationName + '/');
         }
         break;
      case 3:
         workingDirectory = new File(userHome, "Library/Application Support/" + applicationName);
         break;
      default:
         workingDirectory = new File(userHome, applicationName + '/');
      }

      if (!workingDirectory.exists() && !workingDirectory.mkdirs()) {
         throw new RuntimeException("The working directory could not be created: " + workingDirectory);
      } else {
         return workingDirectory;
      }
   }

   private static Util.OS getPlatform() {
      String osName = System.getProperty("os.name").toLowerCase();
      if (osName.contains("win")) {
         return Util.OS.windows;
      } else if (osName.contains("mac")) {
         return Util.OS.macos;
      } else if (osName.contains("solaris")) {
         return Util.OS.solaris;
      } else if (osName.contains("sunos")) {
         return Util.OS.solaris;
      } else if (osName.contains("linux")) {
         return Util.OS.linux;
      } else {
         return osName.contains("unix") ? Util.OS.linux : Util.OS.unknown;
      }
   }

   public static String excutePost(String targetURL, String urlParameters) {
      HttpURLConnection connection = null;

      Object var4;
      try {
         URL url = new URL(targetURL);
         connection = (HttpURLConnection)url.openConnection();
         connection.setRequestMethod("POST");
         connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
         connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
         connection.setRequestProperty("Content-Language", "en-US");
         connection.setUseCaches(false);
         connection.setDoInput(true);
         connection.setDoOutput(true);
         connection.connect();
         byte[] bytes = new byte[294];
         DataInputStream dis = new DataInputStream(Util.class.getResourceAsStream("minecraft.key"));
         dis.readFully(bytes);
         dis.close();
         DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
         wr.writeBytes(urlParameters);
         wr.flush();
         wr.close();
         InputStream is = connection.getInputStream();
         BufferedReader rd = new BufferedReader(new InputStreamReader(is));
         StringBuffer response = new StringBuffer();

         String line;
         while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
         }

         rd.close();
         String str1 = response.toString();
         String var12 = str1;
         return var12;
      } catch (Exception var16) {
         var16.printStackTrace();
         var4 = null;
      } finally {
         if (connection != null) {
            connection.disconnect();
         }

      }

      return (String)var4;
   }

   public static boolean isEmpty(String str) {
      return str == null || str.length() == 0;
   }

   public static void openLink(URI uri) {
      try {
         Object o = Class.forName("java.awt.Desktop").getMethod("getDesktop").invoke((Object)null);
         o.getClass().getMethod("browse", URI.class).invoke(o, uri);
      } catch (Throwable var2) {
         System.out.println("Failed to open link " + uri.toString());
      }

   }

   public static void resetVersion() {
      DataOutputStream dos = null;

      try {
         File dir = new File(getWorkingDirectory() + File.separator + "bin" + File.separator);
         File versionFile = new File(dir, "version");
         dos = new DataOutputStream(new FileOutputStream(versionFile));
         dos.writeUTF("0");
         dos.close();
      } catch (FileNotFoundException var13) {
         Logger.getLogger(Util.class.getName()).log(Level.SEVERE, (String)null, var13);
      } catch (IOException var14) {
         Logger.getLogger(Util.class.getName()).log(Level.SEVERE, (String)null, var14);
      } finally {
         try {
            dos.close();
         } catch (IOException var12) {
            Logger.getLogger(Util.class.getName()).log(Level.SEVERE, (String)null, var12);
         }

      }

   }

   public static String getFakeLatestVersion() {
      try {
         File dir = new File(getWorkingDirectory() + File.separator + "bin" + File.separator);
         File file = new File(dir, "version");
         DataInputStream dis = new DataInputStream(new FileInputStream(file));
         String version = dis.readUTF();
         dis.close();
         return version.equals("0") ? "1285241960000" : version;
      } catch (IOException var4) {
         return "1285241960000";
      }
   }

   private static enum OS {
      linux,
      solaris,
      windows,
      macos,
      unknown;
   }
}
