package de.visone.nlpext.gui;

import java.io.File;

import org.apache.log4j.Logger;

import de.visone.base.Mediator;
import de.visone.gui.tabs.option.BooleanOptionItem;
import de.visone.gui.tabs.option.TextOptionItem;
import de.visone.nlpext.network.CRANetwork;
import de.visone.nlpext.network.CreationProcess;
import de.visone.nlpext.network.TextNetwork;

/**
 * Panel with option for the CRA algorithm.
 * 
 * @author nick
 * @author matthias
 */
public class CRACard extends ParserNetworkCard {
	private static final String LEVEL_LABEL = "take centers from bottom level";
	private static final String CENTER_TAGS_LABEL = "center tags";

	/** default value for "center tag" field. classically this is NP for CRA. */
	private static final String DEFAULT_CENTER_TAG = "NP";

	private BooleanOptionItem level;
	private TextOptionItem tagsBox;

	private static final Logger logger = Logger.getLogger(CRACard.class);

	/**
	 * @param mediator
	 *            the mediator
	 */
	public CRACard(final Mediator mediator) {
		super(mediator, "centering resonance analysis");
	}

	@Override
	public String getApplyText() {
		return "create CRA network";
	}

	// ------------ panel creation methods ------------

	@Override
	protected void fillParameterPanel() {
		addBasicOptions();
		addParserOptions();

		addHeading("center identifying tags (comma separated)");
		tagsBox = new TextOptionItem();
		tagsBox.setValue(DEFAULT_CENTER_TAG);
		addOptionItem(tagsBox, CENTER_TAGS_LABEL);
		level = new BooleanOptionItem();
		level.setValue(true);
		addOptionItem(level, LEVEL_LABEL);
	}

	// ------------ get methods ------------

	/**
	 * returns an initialized <code>CRANetwork</code> or <code>null</code> if
	 * parser could not be loaded.
	 */
	@Override
	public TextNetwork getTextNetwork() {
		// try to load the (new) parser
		try {
			loadParser();

			if (parser != null) {
				final String[] labels = getRootTags();

				final String[] stopWords = extractLinesFromFile(
						stopwords.getValue(), STOP_WORDS_FILTER,
						CANNOT_LOAD_STOPWORDS, useDefaultStopWords());

				final boolean levelMode = getLevelMode();

				final int minWordLength = getMinWordLength();

				return new CRANetwork(parser, labels, stopWords, levelMode,
						minWordLength, getLayoutMode());
			}
			logger.debug("parser is null");
			return null;
		} catch (final Exception e) {
			logger.debug("parser threw exception:", e);
			return null;
		}
	}

	/** tries to read tags specifying the centers in CRA. */
	private String[] getRootTags() {
		try {
			final String[] tags = tagsBox.getValue().split(",");
			for (int i = 0; i < tags.length; i++)
				tags[i] = tags[i].trim().toUpperCase();
			// System.out.println(tags[i]);
			// TODO catch the case where all tags are non-sense !
			return tags;
		} catch (final Exception e) {
			error("could not read center tags. will just use noun phrases.");
			return new String[] { DEFAULT_CENTER_TAG };
		}
	}

	/** return whether bottom level center search is selected. */
	private boolean getLevelMode() {
		return level.getValue();
	}

	@Override
	public void runAlgorithm() {
		try {
			final File textFile = getTextFile();
			if (textFile == null)
				return;

			final TextNetwork textNetw = getTextNetwork();
			logger.debug("text network received");
			if (textNetw == null) {
				logger.error("text network is null");
				return;
			}

			final boolean doQuickLayout = getLayoutMode();

			// new NetworkCreation(textFile, textNetw, doQuickLayout, mediator);
			logger.debug("start create process");
			new CreationProcess(textFile, textNetw, doQuickLayout, mediator);
		} catch (final Exception e) {
			logger.error(e);
		}
	}
}
