package net.mc.main;

import com.mojang.launcher.OperatingSystem;
import java.awt.Component;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.Proxy.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import joptsimple.NonOptionArgumentSpec;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import net.minecraft.launcher.Language;
import net.minecraft.launcher.Launcher;
import net.minecraft.launcher.StorageManager;

public class Main {
   public static Language lh;
   public static String currentv = "3.0";
   private static String newestv = "";
   private static String wherenewv = "";
   private static String updateurl = "";
   private static Boolean isLatest = true;
   public static String cbuttontext = "Launcher Website";
   public static String cbuttonlink = "https://launcher.skaia.us";
   public static String sButton = "Support Ticket";
   public static String sLink = "https://skaiacraft.net/support";
   public static String npg = "http://486.merith.xyz/obsidian/";
   public static List<String> dnames = new ArrayList();
   public static String[] urls = new String[]{"486.merith.xyz/obsidian"};
//    public static String bUrl = "http://486.merith.xyz/obsidian/";
//    public static String bUrl2 = "http://486.merith.xyz/obsidian/";
//    public static String backupBaseURL = "http://486.merith.xyz/obsidian/";
//    public static String backupBaseURL2 = "486.merith.xyz/obsidian";
   private static JFrame frame;
   private static File workingDirectory;
   private static Proxy proxy;
   private static PasswordAuthentication passwordAuthentication;
   private static String[] argss;

   public static void restartApplication() throws IOException, URISyntaxException {
      System.out.println("Restarting launcher'");
      new Launcher(frame, workingDirectory, proxy, passwordAuthentication, argss, 100);
   }

//    public static void checkUpdates() {
//       try {
//          URL url;
//          if (isReachable(bUrl2)) {
//             System.out.println("Using default URL.");
//             url = new URL(bUrl + "version.php");
//          } else {
//             System.out.println("Could not reach " + bUrl + ". Trying to reach old URL.");
//             if (!isReachable(backupBaseURL2)) {
//                System.out.println("Could not reach backup URL. Exiting update check sequence.");
//                isLatest = true;
//                return;
//             }

//             System.out.println("Using backup URL.");
//             url = new URL(backupBaseURL + "version.php");
//          }

//          URLConnection yc = url.openConnection();
//          yc.addRequestProperty("User-Agent", "Mozilla/4.76");
//          BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));

//          String inputLine;
//          for(int i = 0; (inputLine = in.readLine()) != null; ++i) {
//             if (i == 0) {
//                newestv = inputLine;
//             } else if (i == 1) {
//                wherenewv = inputLine;
//             } else if (i == 2) {
//                updateurl = inputLine;
//             } else if (i == 3) {
//                cbuttontext = inputLine;
//             } else if (i == 4) {
//                cbuttonlink = inputLine;
//             } else if (i == 5) {
//                sButton = inputLine;
//             } else if (i == 6) {
//                sLink = inputLine;
//             }
//          }

//          in.close();
//          if (!newestv.equalsIgnoreCase(currentv)) {
//             System.out.println(newestv);
//             System.out.println(currentv);
//             isLatest = false;
//          } else {
//             isLatest = true;
//          }
//       } catch (IOException var5) {
//          isLatest = true;
//       }

//    }

   public static void startMC(String[] args) throws IOException {
      String[] var4 = args;
      int var3 = args.length;

      for(int var2 = 0; var2 < var3; ++var2) {
         String s = var4[var2];
         System.out.println(s);
      }

      System.setProperty("java.net.preferIPv4Stack", "true");
      OptionParser optionParser = new OptionParser();
      optionParser.allowsUnrecognizedOptions();
      optionParser.accepts("help", "Show help").forHelp();
      optionParser.accepts("force", "Force updating");
      OptionSpec proxyHostOption = optionParser.accepts("proxyHost", "Optional").withRequiredArg();
      OptionSpec proxyPortOption = optionParser.accepts("proxyPort", "Optional").withRequiredArg().defaultsTo("8080").ofType(Integer.class);
      OptionSpec proxyUserOption = optionParser.accepts("proxyUser", "Optional").withRequiredArg();
      OptionSpec proxyPassOption = optionParser.accepts("proxyPass", "Optional").withRequiredArg();
      OptionSpec workingDirectoryOption = optionParser.accepts("workdir", "Optional").withRequiredArg().ofType(File.class).defaultsTo(Util.getWorkingDirectory());
      NonOptionArgumentSpec var7 = optionParser.nonOptions();

      OptionSet optionSet;
      try {
         optionSet = optionParser.parse(args);
      } catch (OptionException var14) {
         optionParser.printHelpOn((OutputStream)System.out);
         System.out.println("(to pass in arguments to minecraft directly use: '--' followed by your arguments");
         return;
      }

      if (optionSet.has("help")) {
         optionParser.printHelpOn((OutputStream)System.out);
      } else {
         String hostName = (String)optionSet.valueOf((OptionSpec)proxyHostOption);
         proxy = Proxy.NO_PROXY;
         if (hostName != null) {
            try {
               proxy = new Proxy(Type.SOCKS, new InetSocketAddress(hostName, (Integer)optionSet.valueOf((OptionSpec)proxyPortOption)));
            } catch (Exception var13) {
            }
         }

         String proxyUser = (String)optionSet.valueOf((OptionSpec)proxyUserOption);
         String proxyPass = (String)optionSet.valueOf((OptionSpec)proxyPassOption);
         passwordAuthentication = null;
         if (!proxy.equals(Proxy.NO_PROXY) && stringHasValue(proxyUser) && stringHasValue(proxyPass)) {
            passwordAuthentication = new PasswordAuthentication(proxyUser, proxyPass.toCharArray());
            final PasswordAuthentication auth = passwordAuthentication;
            Authenticator.setDefault(new Authenticator() {
               protected PasswordAuthentication getPasswordAuthentication() {
                  return auth;
               }
            });
         }

         workingDirectory = (File)optionSet.valueOf((OptionSpec)workingDirectoryOption);
         if (workingDirectory.exists() && !workingDirectory.isDirectory()) {
            throw new RuntimeException("Invalid working directory: " + workingDirectory);
         } else if (!workingDirectory.exists() && !workingDirectory.mkdirs()) {
            throw new RuntimeException("Unable to create directory: " + workingDirectory);
         } else {
            frame = new JFrame();
            argss = args;
            new Launcher(frame, workingDirectory, proxy, passwordAuthentication, args, 100);
         }
      }
   }

   public static Boolean isReachable(String url) {
      try {
         HttpURLConnection.setFollowRedirects(false);
         HttpURLConnection con = (HttpURLConnection)(new URL("http://" + url + "/version.php")).openConnection();
         con.setRequestMethod("HEAD");
         return con.getResponseCode() == 200 ? true : false;
      } catch (Exception var2) {
         var2.printStackTrace();
         return false;
      }
   }

   public static void main(String[] args) throws IOException {
      StorageManager sm = new StorageManager();
      sm.load();
      new DarkTheme(sm.getDark());
      String lang = sm.getLanguage();
      lh = new Language();

      try {
         lh.loadFileNames();
      } catch (URISyntaxException var10) {
         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, (String)null, var10);
      }

      lh.load(lang);
      //checkUpdates();
      if (!isLatest) {
         Object[] options1 = new Object[]{Language.get("button.ok"), Language.get("button.update"), Language.get("button.cancel")};
         JPanel panel = new JPanel();
         panel.add(new JLabel("<html><center>" + wherenewv + "<br>You have version: v" + currentv + "<br>The latest version is: v" + newestv + "</center></html>"));
         int result = JOptionPane.showOptionDialog((Component)null, panel, Language.get("update.title.updateavail"), 1, -1, (Icon)null, options1, (Object)null);
         if (result == 0) {
            startMC(args);
         } else if (result == 1) {
            try {
               OperatingSystem.openLink(new URI(updateurl));
            } catch (Exception var9) {
               try {
                  Desktop.getDesktop().browse(new URI(updateurl));
               } catch (Exception var8) {
               }
            }
         }
      } else {
         startMC(args);
      }

   }

   public static boolean stringHasValue(String string) {
      return string != null && !string.isEmpty();
   }
}
