package net.minecraft.launcher;

import com.mojang.launcher.OperatingSystem;
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class NetLauncherMain {
   private static final Logger LOGGER = LogManager.getLogger();

   public static void main(String[] args) {
      LOGGER.debug("main() called!");
      startLauncher(args);
   }

   public static void startLauncher(String[] args) {
      OptionParser parser = new OptionParser();
      parser.allowsUnrecognizedOptions();
      parser.accepts("winTen");
      OptionSpec<String> proxyHostOption = parser.accepts("proxyHost").withRequiredArg();
      OptionSpec<Integer> proxyPortOption = parser.accepts("proxyPort").withRequiredArg().defaultsTo("8080").ofType(Integer.class);
      OptionSpec<File> workDirOption = parser.accepts("workDir").withRequiredArg().ofType(File.class).defaultsTo(getWorkingDirectory());
      OptionSpec<String> nonOption = parser.nonOptions();
      OptionSet optionSet = parser.parse(args);
      List<String> leftoverArgs = optionSet.valuesOf((OptionSpec)nonOption);
      String hostName = (String)optionSet.valueOf((OptionSpec)proxyHostOption);
      Proxy proxy = Proxy.NO_PROXY;
      if (hostName != null) {
         try {
            proxy = new Proxy(Type.SOCKS, new InetSocketAddress(hostName, (Integer)optionSet.valueOf((OptionSpec)proxyPortOption)));
         } catch (Exception var15) {
         }
      }

      File workingDirectory = (File)optionSet.valueOf((OptionSpec)workDirOption);
      workingDirectory.mkdirs();
      LOGGER.debug("About to create JFrame.");
      JFrame frame = new JFrame();
      frame.setTitle(LauncherConstants.getVersionName() + LauncherConstants.PROPERTIES.getEnvironment().getTitle());
      frame.setPreferredSize(new Dimension(900, 580));

      try {
         InputStream in = Launcher.class.getResourceAsStream("/favicon.png");
         if (in != null) {
            frame.setIconImage(ImageIO.read(in));
         }
      } catch (IOException var14) {
      }

      frame.pack();
      frame.setLocationRelativeTo((Component)null);
      frame.setVisible(true);
      if (optionSet.has("winTen")) {
         System.setProperty("os.name", "Windows 10");
         System.setProperty("os.version", "10.0");
      }

      LOGGER.debug("Starting up launcher.");
      Launcher launcher = new Launcher(frame, workingDirectory, proxy, (PasswordAuthentication)null, (String[])((String[])leftoverArgs.toArray(new String[leftoverArgs.size()])), 100);
      if (optionSet.has("winTen")) {
         launcher.setWinTenHack();
      }

      frame.setLocationRelativeTo((Component)null);
   }

   public static File getWorkingDirectory() {
      String userHome = System.getProperty("user.home", ".");
      File workingDirectory;
      switch(OperatingSystem.getCurrentPlatform().ordinal()) {
      case 1:
         workingDirectory = new File(userHome, ".minecraft/");
         break;
      case 2:
         String applicationData = System.getenv("APPDATA");
         String folder = applicationData != null ? applicationData : userHome;
         workingDirectory = new File(folder, ".minecraft/");
         break;
      case 3:
         workingDirectory = new File(userHome, "Library/Application Support/minecraft");
         break;
      default:
         workingDirectory = new File(userHome, "minecraft/");
      }

      return workingDirectory;
   }
}
