/*
 * 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.Color;
import java.awt.Dimension;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import javax.swing.JLabel;
import javax.swing.JTextField;

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

/**
 * The date range field is used for couples of parameter with both a `Date` type (actually only
 * `time_min` and `time_max` parameters is concerned for now). These are JTextFields which check if
 * the content is a valid date, according to DATE_FORMAT. If the parameter is valid or if it is
 * empty, then the dates value are sent to the controller, in Julian Day format.
 *
 * @author N. Jourdane
 */
public class DateRangeField extends ParamField implements TextFieldListener {

	/** The date format used in the DateRange field */
	private static final String DATE_FORMAT = "dd/MM/yyyy";

	/** The regex used to validate the Date fields */
	private static final String DATE_REGEX = "(^(((0[1-9]|1[0-9]|2[0-8])[\\/](0[1-9]|1[012]))|((29|30|31)[\\/](0[13578]|1[02]))|((29|30)[\\/](0[4,6,9]|11)))[\\/](19|[2-9][0-9])\\d\\d$)|(^29[\\/]02[\\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)";

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

	/** The JTextField used to put the parameter maximum value of the range. */
	private JTextField fieldMax;

	/** The JTextField used to put the parameter minimum value of the range. */
	private JTextField fieldMin;


	/**
	 * Method constructor
	 *
	 * @param requestPanelListener The listener of the request panel.
	 * @param paramName The name of the parameter.
	 */
	public DateRangeField(RequestPanelListener requestPanelListener, String paramName) {
		super(requestPanelListener, paramName);
		this.add(new JLabel("min "));
		fieldMin = new JTextField();
		fieldMin.setName(MIN_SUFFIX);
		fieldMin.setPreferredSize(
				new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
		addChangeListener(this, fieldMin);
		this.add(fieldMin);

		this.add(new JLabel("max "));
		fieldMax = new JTextField();
		fieldMax.setName(MAX_SUFFIX);
		fieldMax.setPreferredSize(
				new Dimension(MIN_FIELD_WIDTH, FIELD_HEIGHT));
		addChangeListener(this, fieldMin);
		this.add(fieldMax);
	}

	/**
	 * This method is called each time the field is modified.
	 */
	@Override
	public void update(JTextField field) {
		DateFormat df = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);
		if (field.getText().isEmpty()) {
			field.setBackground(Color.WHITE);
			requestPanelListener.onParameterRemoved(paramName + field.getName());
		} else if (field.getText().matches(DATE_REGEX)) {
			try {
				long date = df.parse(field.getText()).getTime();
				date = Math.round(date / 86400000.0 + 2440587.5); // to JD
				requestPanelListener.onParameterUpdated(paramName + field.getName(),
						date);
				field.setBackground(Color.WHITE);
			} catch (ParseException e) {
				field.setBackground(Color.PINK);
			}
		} else {
			field.setBackground(Color.PINK);
		}
	}
}