/*
 * Created on 2004-08-12
 *
 * Part of the Symbian development plug-in for Eclipse
 * Copyright (C) Mikołaj Zalewski, 2004
 */
package pl.zalewski.mikolaj.symbdev.preferences;

import java.util.Iterator;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import pl.zalewski.mikolaj.symbdev.SymbdevPlugin;
import pl.zalewski.mikolaj.symbdev.SymbianSDKEntry;
import pl.zalewski.mikolaj.symbdev.SymbianSDKsList;
import pl.zalewski.mikolaj.symbdev.Win32ToolEntry;
import pl.zalewski.mikolaj.symbdev.Win32ToolsList;
import pl.zalewski.mikolaj.symbdev.widgets.ChooseSymbianVersionDialog;
import pl.zalewski.mikolaj.symbdev.widgets.ChooseWin32ToolDialog;

/**
 * @author mikolaj
 *
 * The "Symbian SDKs" preferences page with the SDKs list
 */
public class SymbianSDKsPreferencesPage extends PreferencePage implements
		IWorkbenchPreferencePage {

	public SymbianSDKsPreferencesPage() {
		super();
		setDescription("Symbian SDKs found:");
		setPreferenceStore(SymbdevPlugin.getDefault().getPreferenceStore());
	}

	
	/* (non-Javadoc)
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	protected Control createContents(Composite ancestor) {
		Composite parent=new Composite(ancestor, SWT.NONE);
		GridLayout layout=new GridLayout();
		layout.numColumns=3;
		layout.marginHeight=0;
		layout.marginWidth=0;
		parent.setLayout(layout);

		GridData data;
		
		String error=SymbianSDKsList.getInstance().detectErrorString;
		if (error!=null)
		{
			Label l=new Label(parent, SWT.WRAP);
			l.setText("Error while detecting SDKs: "+error);
			l.setForeground(new Color(Display.getCurrent(), 255, 0, 0));
			data=new GridData();
			data.horizontalSpan=3;
			l.setLayoutData(data);
		}
		
		list=new List(parent, SWT.BORDER);
		data=new GridData(GridData.FILL_BOTH);
		data.widthHint= convertWidthInCharsToPixels(60);
		data.heightHint= convertHeightInCharsToPixels(10);
		data.horizontalSpan=3;
		list.setLayoutData(data);
		list.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) { selectionEvent(); }
			public void widgetSelected(SelectionEvent e) { selectionEvent(); }
		});
		
		
		Iterator i=SymbianSDKsList.getInstance().getEntries();
		String s;
		while (i.hasNext())
		{
			s=(String)i.next();
			list.add(s);
		}
		changedWin32Tools=new boolean[list.getItemCount()];
		newWin32Tools=new int[list.getItemCount()];		
		changedSymbianOSVersions=new boolean[list.getItemCount()];
		newSymbianOSVersions=new int[list.getItemCount()];		
		int ti;
		for (ti=0; ti<list.getItemCount(); ti++)
		{
			changedWin32Tools[ti]=false;
			changedSymbianOSVersions[ti]=false;
		}
		
		Label t=new Label(parent, SWT.NONE);
		t.setText("EPOCROOT: ");		
		epocRootText=new Label(parent, SWT.NONE);
		data=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		data.horizontalSpan=2;
		epocRootText.setLayoutData(data);

		t=new Label(parent, SWT.NONE);
		t.setText("Tools root: ");		
		toolsRootText=new Label(parent, SWT.NONE);		
		data=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		data.horizontalSpan=2;
		toolsRootText.setLayoutData(data);
		
		t=new Label(parent, SWT.NONE);
		t.setText("Symbian OS version: ");
		symbianVersionText=new Label(parent, SWT.NONE);
		data=new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		data.horizontalSpan=1;
		symbianVersionText.setLayoutData(data);		
		fChangeVer=new Button(parent, SWT.PUSH);
		fChangeVer.setText("Change...");
		fChangeVer.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				changeSymbianVersion();
			}
		});
		data=new GridData();
		data.horizontalAlignment=GridData.END;
		fChangeVer.setLayoutData(data);
		
		t=new Label(parent, SWT.NONE);
		t.setText("Default Win32 toolchain: ");		
		win32ToolText=new Label(parent, SWT.NONE);		
		data=new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan=1;
		data.grabExcessHorizontalSpace=true;
		win32ToolText.setLayoutData(data);
		fChangeTool=new Button(parent, SWT.PUSH);
		fChangeTool.setText("Change...");
		fChangeTool.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				changeWin32Tool();
			}
		});
		data=new GridData();
		data.horizontalAlignment=GridData.END;
		fChangeTool.setLayoutData(data);
				
		enableButtons();
		
		return parent;
	}
	
	protected int getVersion(int idx, SymbianSDKEntry entry)
	{
	    if (changedSymbianOSVersions[idx])
	        return newSymbianOSVersions[idx];
	    return entry.getSymbianVersion();
	}
	
	protected String getVersionName(int idx, SymbianSDKEntry entry)
	{
	    return SymbianSDKEntry.getSymbianVersionName(getVersion(idx, entry));
	}
	
	protected void selectionEvent()
	{
		int idx=list.getSelectionIndex();
		if (idx<0)
		{
			win32ToolText.setText("(none)");
			return;	
		}
		String name=list.getItem(idx);
		SymbianSDKEntry entry=SymbianSDKsList.getInstance().getEntry(name);
		epocRootText.setText(entry.epocRoot);
		toolsRootText.setText(entry.toolsRoot);
		if (changedSymbianOSVersions[idx] && newSymbianOSVersions[idx]==SymbianSDKEntry.SYMBIAN_UNKNOWN)
		    symbianVersionText.setText("(to be autodetected)");
			else symbianVersionText.setText("version "+getVersionName(idx, entry));
		
		int toolId=entry.win32ToolId;
		if (changedWin32Tools[idx])
			toolId=newWin32Tools[idx];
		Win32ToolEntry win32tool=Win32ToolsList.getInstance().getEntry(toolId);
		if (win32tool==null) win32ToolText.setText("(none)");
			else win32ToolText.setText(win32tool.name);

		String test=entry.getSDKHelp();
		enableButtons();
	}
	
	protected void enableButtons()
	{
		boolean enab=(list.getSelectionIndex()>=0);
		fChangeTool.setEnabled(enab);
		fChangeVer.setEnabled(enab);
	}
	
	protected void changeWin32Tool()
	{
		int index=list.getSelectionIndex();
		if (index<0) return;
		
	    int id;
	    if (changedWin32Tools[index])
	    	id=newWin32Tools[index];
	    	else id=SymbianSDKsList.getInstance().getEntry(list.getItem(index)).win32ToolId;
		
		ChooseWin32ToolDialog dlg=new ChooseWin32ToolDialog(getShell(), "Choose default tool:", true);
		dlg.setResult(id);
		if (dlg.open() == Window.OK)
		{
			changedWin32Tools[index]=true;
			newWin32Tools[index]=dlg.getResult();
			selectionEvent();  // update the string
		}		
	}
	
	protected void changeSymbianVersion()
	{
		int index=list.getSelectionIndex();
		if (index<0) return;

		int ver=getVersion(index, SymbianSDKsList.getInstance().getEntry(list.getItem(index)));
		ChooseSymbianVersionDialog dlg=new ChooseSymbianVersionDialog(getShell(), "Set Symbian OS version:");
		dlg.setResult(ver);
		if (dlg.open() == Window.OK)
		{
			changedSymbianOSVersions[index]=true;
			newSymbianOSVersions[index]=dlg.getResult();
			selectionEvent();  // update the string
		}
	}
	
	public boolean performOk()
	{
		int i;
		for (i=0; i<list.getItemCount(); i++)
		{
		    SymbianSDKEntry entry=null;
			if (changedWin32Tools[i])
			{
				entry=SymbianSDKsList.getInstance().getEntry(list.getItem(i));
				entry.setWin32ToolId(newWin32Tools[i]);
			}
			
			if (changedSymbianOSVersions[i])
			{
			    if (entry==null)
					entry=SymbianSDKsList.getInstance().getEntry(list.getItem(i));
			    entry.setSymbianVersion(newSymbianOSVersions[i]);
			}
		}
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
	 */
	public void init(IWorkbench workbench) {
	}
	
	int newWin32Tools[];
	boolean changedWin32Tools[];
	int newSymbianOSVersions[];
	boolean changedSymbianOSVersions[];
	Label epocRootText;
	Label toolsRootText;
	Label win32ToolText;
	Label symbianVersionText;
	List list;
	Button fChangeTool;
	Button fChangeVer;
}
