/*
 * 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.resultpanel;

import java.util.logging.Logger;

import eu.omp.irap.vespa.epntapclient.gui.mainpanel.MainPanelListener;
import eu.omp.irap.vespa.votable.gui.VOTablePanelListener;
import eu.omp.irap.vespa.votable.gui.VOTablePanelView;
import eu.omp.irap.vespa.votable.utils.StringJoiner;
import eu.omp.irap.vespa.votable.votable.VOTableCtrl;

/**
 * @author N. Jourdane
 */
public class ResultTabCtrl extends VOTableCtrl implements VOTablePanelListener {

	/** The logger for the class ResultPanelCtrl. */
	private static final Logger LOGGER = Logger.getLogger(ResultTabCtrl.class.getName());

	/** The listener of the main panel. */
	private MainPanelListener listener;

	/** The result panel view, used to add the new tab into it. */
	private ResultsPanelView resultsPanelView;

	/** The title of the tab (ie. The name of the service). */
	private String title;

	/** The result panel view. */
	private VOTablePanelView view;


	/**
	 * Constructor of ResultPanelCtrl.
	 *
	 * @param listener The listener of the main panel.
	 * @param resultsPanelView The result panel view, used to add the new tab into it.
	 * @param title The title of the tab (ie. The name of the service).
	 */
	public ResultTabCtrl(MainPanelListener listener, ResultsPanelView resultsPanelView,
			String title) {
		this.listener = listener;
		this.resultsPanelView = resultsPanelView;
		view = new VOTablePanelView(this);
		this.title = title;
	}

	@Override
	public void displayError(String message, Exception error) {
		listener.displayError(message, error);
	}

	@Override
	public void displayInfo(String shortMessage, String detailledMessage) {
		listener.displayInfo(shortMessage, detailledMessage);
	}

	@Override
	public void displayTable() {
		view.fillTable(voTableData);
		resultsPanelView.addTab(title, view);
	}

	/**
	 * @return The result panel view.
	 */
	public VOTablePanelView getView() {
		return view;
	}

	@Override
	public void onRowsSelected() {
		LOGGER.info("Selected row(s): " + StringJoiner.join(view.getSelectedRows()));
	}

	@Override
	public void setWaitMode(boolean enableWaitMode) {
		listener.setWaitMode(enableWaitMode);
	}

}
