package net.minecraft;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.net.URL;
import java.net.URLEncoder;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class OptionsPanel extends JDialog
{
  private static final long serialVersionUID = 1L;
  public JTextField memoryfield = new JTextField(4);
  public static int memory;
  public static String memorys;
  public static boolean autoenter = true;
  public static JPanel panel = new JPanel(new BorderLayout());
  public static JPanel optionsPanel = new JPanel(new BorderLayout());
  public static JLabel label = new JLabel("Настройки", 0);
  public static JButton backButton = new JButton("Назад");
  public static JPanel buttonsPanel = new JPanel(new BorderLayout());
  public static JButton skinButton = new JButton("Загрузка скинов");
  
  public static JPanel skinsPanel = new JPanel(new BorderLayout());
  public static JPanel skinLabelPanel = new JPanel(new GridLayout(0, 1));
  public static JPanel skinFieldPanel = new JPanel(new GridLayout(0, 1));
  public static JLabel fileTitle = new JLabel("Файл скина:");
  public static JLabel statusTitle = new JLabel("Состояние:");
  public static JLabel status = new JLabel();
  final JFileChooser file = new JFileChooser();
  public static JButton selectFile = new JButton("Выбрать файл...");
  
  public void setSkin() {
	  panel.remove(optionsPanel);
	  buttonsPanel.remove(skinButton);
	  buttonsPanel.add(backButton, "West");
	  panel.add(skinsPanel);
	  label.setText("Загрузка скинов (Будет загружен после входа)");//TODO
	  pack();
	  
	  selectFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) {
		  int returnVal = file.showOpenDialog(OptionsPanel.this);
		  
          if (returnVal == JFileChooser.APPROVE_OPTION) {
    		  File FSource = file.getSelectedFile();
              System.out.println("Выбран скин");
      }}});}
  
public OptionsPanel(final Frame parent)
  {
    super(parent);
    
    skinButton.setEnabled(false);
    setModal(true);
    label.setBorder(new EmptyBorder(0, 0, 16, 0));
    label.setFont(new Font("Default", 1, 16));
    panel.add(label, "North");
    JPanel labelPanel = new JPanel(new GridLayout(0, 1));
    JPanel fieldPanel = new JPanel(new GridLayout(0, 1));
    optionsPanel.add(labelPanel, "West");
    optionsPanel.add(fieldPanel, "Center");
    
    skinsPanel.add(skinLabelPanel, "West");
    skinsPanel.add(skinFieldPanel, "Center");
	skinFieldPanel.add(selectFile);
	skinFieldPanel.add(status);
	skinLabelPanel.add(fileTitle);
	skinLabelPanel.add(statusTitle);
    panel.remove(skinsPanel);

    final JButton forceButton = new JButton("Обновить клиент!");
    forceButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        GameUpdater.forceUpdate = true;
        forceButton.setText("Клиент будет обновлен!");
        forceButton.setEnabled(false);
      }
    });
    
    forceButton.setFocusable(false);
    labelPanel.add(new JLabel("Принудительно обновить клиент: ", 4));
    fieldPanel.add(forceButton);
    
    skinButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
        	setSkin();
        }
      });
    
    backButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
        	setOptionPanel();
        }
      });
    
    labelPanel.add(new JLabel("Расположение клиента на компьютере: ", 4));
    TransparentLabel dirLink = new TransparentLabel(Util.getWorkingDirectory().toString()) {
      private static final long serialVersionUID = 0L;

      public void paint(Graphics g) { super.paint(g);

        int x = 0;
        int y = 0;

        FontMetrics fm = g.getFontMetrics();
        int width = fm.stringWidth(getText());
        int height = fm.getHeight();

        if (getAlignmentX() == 2.0F) x = 0;
        else if (getAlignmentX() == 0.0F) x = getBounds().width / 2 - width / 2;
        else if (getAlignmentX() == 4.0F) x = getBounds().width - width;
        y = getBounds().height / 2 + height / 2 - 1;

        g.drawLine(x + 2, y, x + width - 2, y); }

      public void update(Graphics g)
      {
        paint(g);
      }
    };
    
    labelPanel.add(new JLabel("Сколько памяти выделять игре(мб): ", 4));
	
    dirLink.setCursor(Cursor.getPredefinedCursor(12));
    dirLink.addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent arg0) {
        try {
          Util.openLink(new URL("file://" + Util.getWorkingDirectory().getAbsolutePath()).toURI());
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
    dirLink.setForeground(new Color(2105599));

    fieldPanel.add(dirLink);

    memory = Util.getMemorySelection(); 
    if (memory == 1 ) memory = 1024;
    String memos = Integer.toString(memory);
    memoryfield.setText(memos);
    fieldPanel.add(memoryfield,"mb");

	panel.add(optionsPanel, "Center");

    buttonsPanel.add(new JPanel(), "Center");
    JButton doneButton = new JButton("OK");//TODO Кнопка ОК
    doneButton.addActionListener(new ActionListener() {
    	@SuppressWarnings("unused")
		public void actionPerformed(ActionEvent ae) {
        	memorys = memoryfield.getText();
        	int memory = Integer.parseInt(memorys);
        	if (memory != Util.getMemorySelection()) {
    			Util.setMemorySelection(memory);
    	        String[] args = null;
    			try { MinecraftLauncher.main(args); } catch (Exception e) { e.printStackTrace(); }}
        	if ("" == "Нет") { Util.setAutoEnter(0); } else { Util.setAutoEnter(1); } //TODO
            setVisible(false); }});
    
    buttonsPanel.add(doneButton, "East");
    buttonsPanel.setBorder(new EmptyBorder(16, 0, 0, 0));
    buttonsPanel.add(skinButton, "West"); //TODO Добавление кнопки со скинами
    skinButton.setFocusable(false);
    doneButton.setFocusable(false);
    backButton.setFocusable(false);
    
    labelPanel.add(new JLabel("                                           Расширеные", 4));
    fieldPanel.add(new JLabel("настройки                                                          ", 4));
    
    String[] Update = { "Нет", "Да" };
    labelPanel.add(new JLabel("Отменить автообновление: ", 4));
    
    String[] values = { "Нет" , "Да" };
    	ActionListener ChooseCancelUpdate = new ActionListener() {
    	   public void actionPerformed(ActionEvent e) {
    		JComboBox box = (JComboBox)e.getSource();
    	          String cancel = (String)box.getSelectedItem();
    	          if (cancel == "Да") { GameUpdater.cancelUpdate = true; } else { GameUpdater.cancelUpdate = false; } }};

    	     JComboBox cancelbox = new JComboBox(values);
    	     cancelbox.addActionListener(ChooseCancelUpdate);
    	     fieldPanel.add(cancelbox);
    	     labelPanel.add(new JLabel("Автовход на сервер: ", 4));
    	     String[] values2 = { "Да", "Нет" };
    	     	ActionListener ChooseAutoenter = new ActionListener() {
    	     	   public void actionPerformed(ActionEvent e) {
    	     		JComboBox box = (JComboBox)e.getSource();
    	     	     String autoenters = (String)box.getSelectedItem(); }}; //TODO
    	     	     JComboBox autoenterbox = new JComboBox(values2);
    	     	     cancelbox.addActionListener(ChooseCancelUpdate);
    	     	     System.out.println(Integer.toString(Util.getAutoEnter()));
    	     	     cancelbox.setSelectedIndex(0);
    	     	     fieldPanel.add(autoenterbox);
    
    panel.add(buttonsPanel, "South");

    add(panel);
    panel.setBorder(new EmptyBorder(16, 24, 24, 24));
    pack();
    setLocationRelativeTo(parent);
  }

public void setOptionPanel() {
	panel.add(optionsPanel);
	panel.remove(skinsPanel);
    buttonsPanel.add(skinButton, "West");
    buttonsPanel.remove(backButton);
    label.setText("Настройки");
    pack();
}}