/*
 * Decompiled with CFR 0.152.
 */
package net.mc.main;

import com.mojang.launcher.OperatingSystem;
import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
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.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import joptsimple.ArgumentAcceptingOptionSpec;
import joptsimple.NonOptionArgumentSpec;
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import net.mc.main.DarkTheme;
import net.mc.main.Util;
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;
    private static String newestv;
    private static String wherenewv;
    private static String updateurl;
    private static Boolean isLatest;
    public static String cbuttontext;
    public static String cbuttonlink;
    public static String sButton;
    public static String sLink;
    public static String npg;
    public static List<String> dnames;
    public static String[] urls;
    public static String bUrl;
    public static String bUrl2;
    public static String backupBaseURL;
    public static String backupBaseURL2;
    private static JFrame frame;
    private static File workingDirectory;
    private static Proxy proxy;
    private static PasswordAuthentication passwordAuthentication;
    private static String[] argss;

    static {
        currentv = "3.0";
        newestv = "";
        wherenewv = "";
        updateurl = "";
        isLatest = true;
        cbuttontext = "Launcher Website";
        cbuttonlink = "https://launcher.skaia.us";
        sButton = "Support Ticket";
        sLink = "https://skaiacraft.net/support";
        npg = "http://merovingian861628905.skaia.us/news3000.htm";
        dnames = new ArrayList<String>();
        urls = new String[]{"merovingian861628905.skaia.us", "merovingian861628905.skaiacraft.net", "merovingian861628905.413.io"};
        bUrl = "http://merovingian861628905.skaia.us/";
        bUrl2 = "merovingian861628905.skaia.us";
        backupBaseURL = "http://merovingian861628905.413.io/";
        backupBaseURL2 = "merovingian861628905.413.io";
    }

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

    public static void checkUpdates() {
        try {
            String inputLine;
            URL url;
            if (Main.isReachable(bUrl2).booleanValue()) {
                System.out.println("Using default URL.");
                url = new URL(String.valueOf(bUrl) + "version.php");
            } else {
                System.out.println("Could not reach " + bUrl + ". Trying to reach old URL.");
                if (!Main.isReachable(backupBaseURL2).booleanValue()) {
                    System.out.println("Could not reach backup URL. Exiting update check sequence.");
                    isLatest = true;
                    return;
                }
                System.out.println("Using backup URL.");
                url = new URL(String.valueOf(backupBaseURL) + "version.php");
            }
            URLConnection yc = url.openConnection();
            yc.addRequestProperty("User-Agent", "Mozilla/4.76");
            BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
            int i = 0;
            while ((inputLine = in.readLine()) != null) {
                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;
                }
                ++i;
            }
            in.close();
            if (!newestv.equalsIgnoreCase(currentv)) {
                System.out.println(newestv);
                System.out.println(currentv);
                isLatest = false;
            } else {
                isLatest = true;
            }
        }
        catch (IOException ex) {
            isLatest = true;
        }
    }

    public static void startMC(String[] args) throws IOException {
        OptionSet optionSet;
        String[] stringArray = args;
        int n = args.length;
        int n2 = 0;
        while (n2 < n) {
            String s = stringArray[n2];
            System.out.println(s);
            ++n2;
        }
        System.setProperty("java.net.preferIPv4Stack", "true");
        OptionParser optionParser = new OptionParser();
        optionParser.allowsUnrecognizedOptions();
        optionParser.accepts("help", "Show help").forHelp();
        optionParser.accepts("force", "Force updating");
        ArgumentAcceptingOptionSpec<String> proxyHostOption = optionParser.accepts("proxyHost", "Optional").withRequiredArg();
        ArgumentAcceptingOptionSpec<Integer> proxyPortOption = optionParser.accepts("proxyPort", "Optional").withRequiredArg().defaultsTo("8080", (String[])new String[0]).ofType(Integer.class);
        ArgumentAcceptingOptionSpec<String> proxyUserOption = optionParser.accepts("proxyUser", "Optional").withRequiredArg();
        ArgumentAcceptingOptionSpec<String> proxyPassOption = optionParser.accepts("proxyPass", "Optional").withRequiredArg();
        ArgumentAcceptingOptionSpec<File> workingDirectoryOption = optionParser.accepts("workdir", "Optional").withRequiredArg().ofType(File.class).defaultsTo(Util.getWorkingDirectory(), (File[])new File[0]);
        NonOptionArgumentSpec<String> nonOptions = optionParser.nonOptions();
        try {
            optionSet = optionParser.parse(args);
        }
        catch (OptionException e) {
            optionParser.printHelpOn(System.out);
            System.out.println("(to pass in arguments to minecraft directly use: '--' followed by your arguments");
            return;
        }
        if (optionSet.has("help")) {
            optionParser.printHelpOn(System.out);
            return;
        }
        String hostName = optionSet.valueOf(proxyHostOption);
        proxy = Proxy.NO_PROXY;
        if (hostName != null) {
            try {
                proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(hostName, (int)optionSet.valueOf(proxyPortOption)));
            }
            catch (Exception exception) {
                // empty catch block
            }
        }
        String proxyUser = optionSet.valueOf(proxyUserOption);
        String proxyPass = optionSet.valueOf(proxyPassOption);
        passwordAuthentication = null;
        if (!proxy.equals(Proxy.NO_PROXY) && Main.stringHasValue(proxyUser) && Main.stringHasValue(proxyPass)) {
            final PasswordAuthentication auth = passwordAuthentication = new PasswordAuthentication(proxyUser, proxyPass.toCharArray());
            Authenticator.setDefault(new Authenticator(){

                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return auth;
                }
            });
        }
        if ((workingDirectory = optionSet.valueOf(workingDirectoryOption)).exists() && !workingDirectory.isDirectory()) {
            throw new RuntimeException("Invalid working directory: " + workingDirectory);
        }
        if (!workingDirectory.exists() && !workingDirectory.mkdirs()) {
            throw new RuntimeException("Unable to create directory: " + workingDirectory);
        }
        frame = new JFrame();
        argss = args;
        Launcher l = 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");
            if (con.getResponseCode() == 200) {
                return true;
            }
            return false;
        }
        catch (Exception e) {
            e.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 ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        lh.load(lang);
        Main.checkUpdates();
        if (!isLatest.booleanValue()) {
            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(null, panel, Language.get("update.title.updateavail"), 1, -1, null, options1, null);
            if (result == 0) {
                Main.startMC(args);
            } else if (result == 1) {
                try {
                    OperatingSystem.openLink(new URI(updateurl));
                }
                catch (Exception e) {
                    try {
                        Desktop.getDesktop().browse(new URI(updateurl));
                    }
                    catch (Exception exception) {}
                }
            }
        } else {
            Main.startMC(args);
        }
    }

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