package j3deditor.bin.util.preferences;


import j3deditor.bin.util.UserData;
import j3deditor.bin.util.Utilities;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;


/**
 * Language tab.
 *
 * @author Risto Seene
 */
public class LangTab extends JPanel{


	/** drop down menu that contains the list of available languages. */
	private JComboBox list;
	
	/** button for extracting default languages. */
	private JButton extract;


	/**
	 * Creates an instance of <code>LangTab</code> and
	 * initializes its components.
	 */
	public LangTab(){
		setName(Utilities.translator.translate(getClass(), "title"));
		setLayout(null);
		initContent();
	}
	
	
	/**
	 * Initializes the content of this panel.
	 */
	private void initContent(){
	          JLabel lang = new JLabel(Utilities.translator.translate(getClass(), "lang"), 4);
	          list = new JComboBox();
	          initializeList();
		
		lang.setBounds(20, 20, 100, (int)(list.getPreferredSize().getHeight()));
	          add(lang);
		
	          if(Utilities.userdata.getData("language") != null){
	                    list.setSelectedItem(getListItem(((String)Utilities.userdata.getData("language"))));
	          }
		
		list.setBounds(125, 20, 200, (int)(list.getPreferredSize().getHeight()));
		list.setToolTipText(Utilities.translator.translate(getClass(), "restart"));
		add(list);
		
		String buttonInfo = Utilities.translator.translate(this.getClass(), "extract");
		extract = new JButton(buttonInfo);
		extract.setBounds(45, 100, 280, (int)(extract.getPreferredSize().getHeight()));
		extract.addActionListener(new ActionListener(){

			          public void actionPerformed(ActionEvent e){
			                    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){

			                              public Void doInBackground(){
							Utilities.util.extractZipResources(Utilities.compressedData+"languages.zip", UserData.languages);
							Utilities.userdata.updateLanguageList();
							initializeList();
							extract.setEnabled(false);
			                                        return null;
			                              }
			                    };
			                    worker.execute();
			          }
			});
		add(extract);
	}


	/**
	 * Initializes the content of drop down menu.
	 */
	private void initializeList(){
		list.removeAllItems();
	          list.addItem("English [default]");
		String[][] listitems = Utilities.userdata.getLanguageList();
		for(int i = 0; i < listitems.length; i++){
			list.addItem(listitems[i][0] + " [" + listitems[i][1] + "]");
		}
	}
	
	
	/**
	 * Returns the specified list item as a String.
	 * 
	 * @param lang
	 * 	two letter language code
	 * @return the specified list item as a String
	 * 	or <code>null</code> if it can't be found in the list
	 */
	private String getListItem(String lang){
		for(int i=0; i<list.getItemCount(); i++){
			if(((String)list.getItemAt(i)).contains(" ["+lang+"]")){
				return ((String)list.getItemAt(i));
			}
		}
		return null;
	}


	/**
	 * Passes the values shown on this tab to the central
	 * <code>UserData</code> object.
	 */
	public void saveChanges(){
		UserData userdata = Utilities.userdata;
		String item = ((String)list.getSelectedItem());
		userdata.setData("language", item.substring(item.indexOf("[")+1, item.lastIndexOf("]")));
	}

}
