package pl.zalewski.mikolaj.symbdev.preferences;

import java.util.Iterator;
import java.util.LinkedList;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.IWorkbench;
import pl.zalewski.mikolaj.symbdev.*;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.window.Window;

/**
 * This class represents a preference page that
 * is contributed to the Preferences dialog.
 * <p>
 * This page is used to modify preferences only. They
 * are stored in the preference store that belongs to
 * the main plug-in class. That way, preferences can
 * be accessed directly via the preference store.
 */


public class Win32ToolsPreferencesPage
	extends PreferencePage
	implements IWorkbenchPreferencePage {
//	public static final String P_LIST = "listPreference";

	public Win32ToolsPreferencesPage() {
		super();
		setPreferenceStore(SymbdevPlugin.getDefault().getPreferenceStore());
		setDescription("To build programs for the emulator you will need a Win32 compiler. If it is not autodetected you can add it manually:");
		initializeDefaults();
	}
/**
 * Sets the default values of the preferences.
 */
	private void initializeDefaults() {
		IPreferenceStore store = getPreferenceStore();
	}
	
	/* puts the button on the panel and sets the layout */
	Button setupButton(Composite panel, String name, Listener action)
	{
		Button button= new Button(panel, SWT.PUSH);
		button.setText(name);
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.BEGINNING;
		button.setLayoutData(data);
		if (action!=null)
			button.addListener(SWT.Selection, action);
		return button;
	}
	
	public Control createContents(Composite ancestor) {
		Composite parent= new Composite(ancestor, SWT.NONE);

		GridLayout layout= new GridLayout();
		layout.numColumns= 2;
		layout.marginHeight= 0;
		layout.marginWidth= 0;
		parent.setLayout(layout);
		
		list=new List(parent, SWT.BORDER);
		GridData data= new GridData(GridData.FILL_BOTH);
		data.widthHint= convertWidthInCharsToPixels(60);
		data.heightHint= convertHeightInCharsToPixels(10);
		list.setLayoutData(data);
		
		Composite buttons= new Composite(parent, SWT.NULL);
		buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
		layout= new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		buttons.setLayout(layout);
		
		setupButton(buttons, "Add...", new Listener() {
			public void handleEvent(Event e) {
				add();
			}			
		});
		edit=setupButton(buttons, "Edit...", new Listener() {
			public void handleEvent(Event e) {
				edit();
			}
		});
		remove=setupButton(buttons, "Remove", new Listener() {
			public void handleEvent(Event e) {
				remove();
			}
		});
		
		list.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) { selectionEvent(); }
			public void widgetSelected(SelectionEvent e) { selectionEvent(); }
		});
		
		fillTable(-1);
		return parent;
	}
	
	protected void fillTable(int selectId)
	{
		LinkedList idlist=new LinkedList();
		int currSel=list.getSelectionIndex();
		list.removeAll();
		Iterator it=Win32ToolsList.getInstance().getEntries();
		while (it.hasNext())
		{
			Win32ToolEntry entry=(Win32ToolEntry)it.next();
			list.add(entry.name);
			idlist.add(new Integer(entry.id));
		}
		
		idtab=new int[idlist.size()];
		int pos=0;
		it=idlist.iterator();
		while (it.hasNext())
		{
			idtab[pos]=((Integer)it.next()).intValue();
			if (idtab[pos]==selectId)
				list.setSelection(pos);
			pos++;
		}
		
		if (selectId==-1)
		{
			if (currSel>list.getItemCount()) currSel=list.getItemCount();
			list.setSelection(currSel);
		}
		enableButtons();
	}
	
	protected void selectionEvent()
	{
		enableButtons();
	}
	
	protected void enableButtons()
	{
		boolean enab=list.getSelectionIndex()>=0;
		edit.setEnabled(enab);
		remove.setEnabled(enab && !Win32ToolsList.getInstance().getEntry(idtab[list.getSelectionIndex()]).autodetected);
	}
	
	/* called when the add button is pressed */
	protected void add()
	{
		Win32ToolEntry entry=new Win32ToolEntry();
		Win32ToolDlg dlg=new Win32ToolDlg(getShell(), entry, false);
		if (dlg.open() == Window.OK) {
			Win32ToolEntry result=dlg.getResult();
			if (dlg.resultChanged(result))
			{
				Win32ToolsList.getInstance().storeEntry(result);
				fillTable(result.id);
			}
		}
	}
	
	protected void edit()
	{
		if (list.getSelectionIndex()<0) return;
		Win32ToolEntry entry=Win32ToolsList.getInstance().getEntry(idtab[list.getSelectionIndex()]);
		Win32ToolDlg dlg=new Win32ToolDlg(getShell(), entry, false);
		if (dlg.open() == Window.OK) {
			Win32ToolEntry result=dlg.getResult();
			if (dlg.resultChanged(result))
			{
				Win32ToolsList.getInstance().storeEntry(result);
				fillTable(result.id);
			}
		}		
	}
	
	protected void remove()
	{
		String[] button=new String[2];
		button[0]="OK";
		button[1]="Cancel";
		
		if (MessageDialog.openQuestion(getShell(), SymbdevPlugin.MESSAGE_TITLE, "Are you sure that you want to remove that entry?"))
		{
			Win32ToolsList.getInstance().removeEntry(idtab[list.getSelectionIndex()]);
			fillTable(-1);
		}
	}
	
	List list;
	Button edit;
	Button remove;
	int idtab[];
	
	public void init(IWorkbench workbench) {
	}
}