/*
 * This file is a part of EpnTAPClient.
 * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
 * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
 * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
 *
 * This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version. This program 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 General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package eu.omp.irap.vespa.epntapclient.gui.requestpanel.paramfield;

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

import javax.swing.JComboBox;

import eu.omp.irap.vespa.epntapclient.gui.requestpanel.RequestPanelListener;

/**
 * The target class field is used only for the `target_class` parameter. It is a ComboBox filled
 * with a list of static target classes (see
 * https://voparis-confluence.obspm.fr/display/VES/4+-+EPN-TAP+queries#id-4-EPN-TAPqueries-
 * __RefHeading__39_312326667_Toc3037660464.2.6TargetClass). The parameter is sent to the controller
 * each time it is updated.
 *
 * @author N. Jourdane
 */
public class TargetClassField extends ParamField {

	/**
	 * An enumeration of all available target classes.
	 *
	 * @author N. Jourdane
	 */
	@SuppressWarnings("javadoc")
	enum TargetClass {
		// @noformat
		ALL("All"),        COMET("Comet"),   EXOPLANET("Exoplanet"),    INTERPLANETARY_MEDIUM("Interplanetary medium"),
		RING("Ring"),  SAMPLE("Sample"),       SKY("Sky"),  SPACECRAFT("Spacecraft"),
		SPACEJUNK("Spacejunk"),      STAR("Star");
		// @format

		/** The name of the target class. */
		String name;


		/**
		 * Method constructor for the enumeration.
		 *
		 * @param name The name of the target class.
		 */
		TargetClass(String name) {
			this.name = name;
		}

		/**
		 * @return The name formated for the query.
		 */
		String query() {
			return name.replace(" ", "_").toLowerCase();
		}
	}


	/** The serial version UID. */
	private static final long serialVersionUID = 1L;

	/** The comboBox used to select the target class. */
	private JComboBox<TargetClass> comboBox;


	/**
	 * Method constructor
	 *
	 * @param requestPanelListener The listener of the request panel.
	 * @param paramName The name of the parameter.
	 */
	public TargetClassField(RequestPanelListener requestPanelListener, String paramName) {
		super(requestPanelListener, paramName);
		comboBox = new JComboBox<>(TargetClass.values());
		comboBox.setPreferredSize(new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
		comboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				update();
			}
		});
		this.add(comboBox);
	}

	/**
	 * This method is called each time the field is modified.
	 */
	public void update() {
		TargetClass value = (TargetClass) comboBox.getSelectedItem();
		if (TargetClass.ALL.equals(value)) {
			requestPanelListener.onParameterRemoved(paramName);
		} else {
			requestPanelListener.onParameterUpdated(paramName, value.query());
		}
	}

}