/*===========================================================================
  Copyright (C) 2008-2009 by the Okapi Framework contributors
-----------------------------------------------------------------------------
  This library is free software; you can redistribute it and/or modify it 
  under the terms of the GNU Lesser General Public License as published by 
  the Free Software Foundation; either version 2.1 of the License, or (at 
  your option) any later version.

  This library is distributed in the hope that it will be useful, but 
  WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this library; if not, write to the Free Software Foundation, 
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

  See also the full LGPL text here: http://www.gnu.org/copyleft/lesser.html
===========================================================================*/

package net.sf.okapi.steps.tokenization.ui.locale;

import java.util.ArrayList;

import net.sf.okapi.common.ui.abstracteditor.AbstractBaseDialog;
import net.sf.okapi.common.ui.abstracteditor.IDialogPage;
import net.sf.okapi.common.ui.abstracteditor.TableAdapter;
import net.sf.okapi.lib.extra.INotifiable;
import net.sf.okapi.steps.tokenization.locale.LanguageList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget;

public class LanguageSelectorPage extends Composite implements IDialogPage {
	protected Label listDescr;
	private Table table;
	private TableColumn col1;
	private TableColumn col2;
	private TableAdapter adapter;
	private TableColumn col3;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public LanguageSelectorPage(final Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		listDescr = new Label(this, SWT.NONE);
		listDescr.setData("name", "listDescr");
		listDescr.setText("The program displays installed languages and their codes.");
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		// Recreate table, SWT Designer cannot handle *else* here
		if (hasCheckBoxes()) {
			
			table.dispose(); //!!! Otherwise layout gets broken
			table = new Table(this, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
		}
				
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				
				Object dialog = getData("dialog");
				
				if (dialog instanceof AbstractBaseDialog) {
				
					// Double-click closes the dialog only if it has a parent, and is not a top-level window
					Shell shell = ((AbstractBaseDialog) dialog).getShell();
					Object parent = shell.getData("parent");
					if (parent != null)
						if (dialog instanceof INotifiable)
							((INotifiable) dialog).exec(this, AbstractBaseDialog.NOTIFICATION_OK, null);
				}				
			}
		});
		GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gridData.heightHint = 400;
		table.setLayoutData(gridData);
		table.setData("name", "table");
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		col1 = new TableColumn(table, SWT.NONE);
		col1.setData("name", "col1");
		col1.setWidth(348);
		col1.setText("Language");
		
		col2 = new TableColumn(table, SWT.NONE);
		col2.setData("name", "col2");
		col2.setWidth(150);
		col2.setText("Code Okapi");
		
		adapter = new TableAdapter(table);
		
		col3 = new TableColumn(table, SWT.NONE);
		col3.setData("name", "col3");
		col3.setWidth(150);
		col3.setText("Code CLDR");
		adapter.setRelColumnWidths(new double [] {6, 1.25, 1.25});
	}

	protected boolean hasCheckBoxes() {

		return false;
	}

	public boolean canClose(boolean isOK) {
		// TODO Auto-generated method stub
		return true;
	}

	public void interop(Widget speaker) {
		// TODO Auto-generated method stub
		
	}

	@SuppressWarnings("unchecked")
	public boolean load(Object data) {
				
//		list.setItems(LanguageList.getLanguages());
//		list.pack();
		
		String [] languages = LanguageList.getLanguages();
		String [] codes = LanguageList.getLanguageCodes_Okapi();
		String [] codes2 = LanguageList.getLanguageCodes_ICU();
		
		for (int i = 0; i < Math.min(languages.length, codes.length); i++) {
			
			adapter.addRow(new String[] {languages[i], codes[i], codes2[i]}, false);
		}
		
		adapter.sort(1, false);
		
		if (data instanceof ArrayList<?>) {
			
			ArrayList<String> list = (ArrayList<String>) data;
			
			for (String string : list) {
				
				TableItem item = adapter.findValue(string, 2);
				if (item == null) continue;
				
				item.setChecked(true);
			}
		}
		
		return true;
	}

	@SuppressWarnings("unchecked")
	public boolean save(Object data) {
		
		if (data instanceof ArrayList<?>) {
		
			ArrayList<String> list = (ArrayList<String>) data;
			
			list.clear();
			for (TableItem item : table.getItems())
				if (item.getChecked())
					list.add(item.getText(1));
		}
				
		return true;
	}

}
