/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM 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
 * any later version.
 * 
 * PeerfactSim.KOM 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 PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.topology.views.fiveg.utils;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import de.tud.kom.p2psim.impl.topology.views.FiveGTopologyView;
import de.tud.kom.p2psim.impl.topology.views.fiveg.ModelBasedSegmentDatabase;
import de.tud.kom.p2psim.impl.topology.views.fiveg.models.AbstractModel;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Oracle;

/**
 * A simple graph for debugging the models of the
 * {@link ModelBasedSegmentDatabase} used by the {@link FiveGTopologyView}.
 * 
 * @author Marc Schiller
 * @version 1.0, 15 Dec 2016
 */
public class Graph extends JFrame implements EventHandler {

	AbstractModel model = null;

	public Graph() {
		super("FiveGTopology - Cell Model Debug Graph");
		Event.scheduleImmediately(this, null, 0);
	}

	public void setModel(AbstractModel model) {
		this.model = model;
	}

	@Override
	public void eventOccurred(Object content, int type) {
		if (model.getDebug()) {
			XYSeries data = new XYSeries("ModelData");
			for (int i = 0; i < Oracle.getAllHosts().size(); i += 1) {
				data.add(i, model.getDouble(i));
			}
			JFreeChart xyChart = ChartFactory.createXYLineChart(
					model.getParameterType().toString() + " -- " + model.toString(), "Users", "Value",
					new XYSeriesCollection(data), PlotOrientation.VERTICAL,
					false, true, false);

			// X-Axis Ticks every 5%
			((NumberAxis) xyChart.getXYPlot().getDomainAxis())
					.setTickUnit(new NumberTickUnit(Oracle.getAllHosts().size() / 20));
			
			ChartPanel chartPanel = new ChartPanel(xyChart);
			setContentPane(chartPanel);
			pack();
			setVisible(true);
		}
	}
}
