/*
 *  Copyright 2006-2007 Columbia University.
 *
 *  This file is part of MEAPsoft.
 *
 *  MEAPsoft is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  MEAPsoft 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 MEAPsoft; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 *  02110-1301 USA
 *
 *  See the file "COPYING" for the text of the license.
 */


package com.meapsoft.visualizer;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import com.meapsoft.FeatChunk;
import com.meapsoft.FeatFile;
import com.meapsoft.gui.ColorMap;

public class SingleFeatureLineGraphPanel extends SingleFeaturePanel
{	
	//int numColors = 256;
	ColorMap colormap;// = ColorMap.getJet(numColors);
	
	public SingleFeatureLineGraphPanel(FeatFile featFile, String featureName) 
	{
		super(featFile, featureName);
	
		 numDrawableFeatures = -1;
	}

	public int initialize()
	{
		int error = super.initialize();
		if (error < 0)
			return error;
		
		colormap = ColorMap.getJet(featureSize);
		
		return 1;
	}
	
	public String getDisplayType() 
	{
		return "LineGraph";
	}
	
	public void drawData(Graphics g) 
	{
		double zoomMulti = (zoomLevel * 4.0)/4.0;
		int w = (int)(this.getWidth() * zoomMulti);
//		int w = this.getWidth();
		int h = this.getHeight();
				
		double yScaler = h/featureRange;			
		double xScaler = w/timeRange;
		
		//System.out.println("w: " + w + " h: " + h);
		//System.out.println("xScaler: " + xScaler + " yScaler: " + yScaler);
		//System.out.println("numChunks: " + numChunks);

		g.setColor(bgColor);
		g.fillRect(0, 0, w, h);
		
		g.setColor(fGColor);
		
		for (int j = 0; j < featureSize; j++)
		{
			int xPrev = 0;
			int yPrev = (int)(featureRange/2.0);
			
			if (featureSize > 1)
				g.setColor(colormap.table[j]);
			else
				g.setColor(fGColor);
			
			double localFirstEventTime = ((FeatChunk)events.elementAt(firstChunkToDraw)).startTime;
			
			int x = 0;
			
			for (int i = firstChunkToDraw; i < numChunks && x < getWidth() ; i++)
			{
				FeatChunk fC = (FeatChunk)events.elementAt(i);
				x = (int)((fC.startTime - localFirstEventTime) * xScaler);
				//adjust to zero
				double dataPoint = featureData[i][j] - lowestValue;
				int y = h - (int)(dataPoint * yScaler);
				//System.out.println("x: " + x + " y: " + y);
				//don't draw the first one to avoid false line
				if (i > 0)
					g.drawLine(xPrev, yPrev, x, y);
				xPrev = x; 
				yPrev = y;
			}
		}
	}
	
	public void mouseClicked(MouseEvent arg0) 
	{		
		//System.out.println(featureName = ": thanks for clicking in me!");
	}

	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	public static void main(String[] args)
	{	
		
		if (args.length < 2)
		{
			System.out.println("usage: SingleFeaturePanel myfilename myfeaturename\n");
			System.exit(-1);
		}

		final String fileName = args[0];
		final String featureName = args[1];
		final FeatFile fF = new FeatFile(fileName);
		//Schedule a job for the event-dispatching thread:
		//creating and showing this application's GUI.
		javax.swing.SwingUtilities.invokeLater(new Runnable() 
		{
			public void run() 
			{
				JFrame frame = new JFrame("SingleFeaturePanel");
				
				try
				{
					fF.readFile();
				}
				catch(Exception e)
				{
					e.printStackTrace();
					System.exit(-1);
				}
				SingleFeaturePanel sFP = new SingleFeatureLineGraphPanel(fF, featureName);
				if (sFP.initialize() == -1)
				{
					System.out.println("hmm, something wrong, bailing.");
					System.exit(-1);
				}
				sFP.setSize(600, 400);
				
				frame.setContentPane(sFP);
				frame.pack();
				frame.setVisible(true);
				frame.setBounds(100, 100, 600, 400);

				sFP.repaint();
			}
		});
	}
}
