package net.minecraft;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class LauncherFrame extends Frame
{
  public static final int VERSION = 13;
  private static final long serialVersionUID = 1L;
  public Map<String, String> customParameters = new HashMap<String, String>();
  public Launcher launcher;
  public LoginForm loginForm;
  
  public LauncherFrame()
  {
    super(LauncherSettings.title);

    setBackground(Color.BLACK);
    loginForm = new LoginForm(this);
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(loginForm, "Center");

    p.setPreferredSize(new Dimension(854, 480));

    setLayout(new BorderLayout());
    add(p, "Center");

    pack();
    setLocationRelativeTo(null);
    try
    {
      setIconImage(ImageIO.read(LauncherFrame.class.getResource("favicon.png")));
    } catch (IOException e1) {
      e1.printStackTrace();
    }

    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent arg0) {
        new Thread() {
          public void run() {
            try {
              Thread.sleep(30000L);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            System.out.println("FORCING EXIT!");
            System.exit(0);
          }
        }
        .start();
        if (launcher != null) {
          launcher.stop();
          launcher.destroy();
        }
        System.exit(0);
      } } );
  }
  
  public void playCached(String userName) {
    try {
      if ((userName == null) || (userName.length() <= 0)) {
        userName = "Guest";
      }
      	GameUpdater.cancelUpdate = true;
      	Object[] options = {"Да, играю в одиночку!", "ХОЧУ НА СЕРВ!"};
      	int n =JOptionPane.showOptionDialog(loginForm, "В этом режиме нельзя войти на серв, вы реально хотите играть в одиночную?", "Предупреждение", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
	    if(n == JOptionPane.NO_OPTION){
	    	LoginForm.errorLabel.setText("");
	    	removeAll();
	    	add(LoginForm.buildMainLoginPanel(), "Center");
	    	validate();
        } else {
      	launcher = new Launcher();
	    launcher.customParameters.putAll(customParameters);
	    launcher.customParameters.put("userName", userName);
	    launcher.init();
	    removeAll();
	    add(launcher, "Center");
	    validate();
	    launcher.start();
	    loginForm = null;
	    setTitle(LauncherSettings.titleInGame); }
    } catch (Exception e) {
      e.printStackTrace();
      showError(e.toString());
    }
  }
  
  public void login(String userName, String password) {
    try {
    int ver = LauncherSettings.launcherVersion;
	String parameters = "user=" + URLEncoder.encode(userName, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8") + "&version=" + ver;
      String result = Util.excutePost(LauncherSettings.authUrl, parameters);
      if (result == null) {
        showError("Невозможно подключится к серверу!");
        loginForm.setNoNetwork();
        return;
      }
      if (!result.contains(":")) {
        if (result.trim().equals("Bad login")) {
          showError("Неправильный логин или пароль!");
        } else if (result.trim().equals("Old version")) {
          loginForm.setOutdated();
          showError("Нужно обновить лаунчер!");
        } else {
          showError(result);
        }
        loginForm.setNoNetwork();
        return;
      }
      String[] values = result.split(":");

      launcher = new Launcher();
      launcher.customParameters.putAll(customParameters);
      launcher.customParameters.put("userName", values[2].trim());
      launcher.customParameters.put("latestVersion", values[0].trim());
      launcher.customParameters.put("downloadTicket", values[1].trim());
      launcher.customParameters.put("sessionId", values[3].trim());
      if(OptionsPanel.autoenter = true){
      launcher.customParameters.put("server", LauncherSettings.serverAddr);
      launcher.customParameters.put("port", LauncherSettings.serverPort); }
      launcher.init();

      removeAll();
      add(launcher, "Center");
      validate();
      launcher.start();
      loginForm.loginOk();
      loginForm = null;
      setTitle(LauncherSettings.titleInGame);
    } catch (Exception e) {
      e.printStackTrace();
      showError(e.toString());
      loginForm.setNoNetwork();
    }
  }

  void showError(String error) {
    removeAll();
    add(loginForm);
    loginForm.setError(error);
    validate();
  }

  public boolean canPlayOffline(String userName) {
    Launcher launcher = new Launcher();
    launcher.customParameters.putAll(customParameters);
    launcher.init(userName, null, null, null);
    return launcher.canPlayOffline();
  }

  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception localException) {
    }
    LauncherFrame launcherFrame = new LauncherFrame();
    launcherFrame.setVisible(true);
    launcherFrame.customParameters.put("stand-alone", "true");

    if (args.length >= 3) {
      String ip = args[2];
      String port = "25565";
      if (ip.contains(":")) {
        String[] parts = ip.split(":");
        ip = parts[0];
        port = parts[1];
      }

      launcherFrame.customParameters.put("server", LauncherSettings.serverAddr);
      launcherFrame.customParameters.put("port", LauncherSettings.serverPort);
    }

    if (args.length >= 1) {
      launcherFrame.loginForm.userName.setText(args[0]);
      if (args.length >= 2) {
        launcherFrame.loginForm.password.setText(args[1]);
        launcherFrame.loginForm.doLogin();
      }
    }
  }
}