import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class Wind2SmallDisplay {
	protected AnemometerPanel anemometer[];
	protected UserADCPanel user[];
	protected WindDirectionPanel windDirection;
	protected JFrame f;
	protected String stationID, imageFileName;
	protected String anemometerType[];
	protected boolean hideAnemometer[],hideUser,hideWindDirection;
	protected JLabel statusLabel;
	protected javax.swing.Timer timer;
	protected Date recordDate;
	protected String recDate;
	protected UserChannel uc[];
	protected int maxAge; // seconds of history to plot
	protected int nPlots; // number of user plots

	public void updateStatus() {
		/* calculate the difference between rec.rxDate and the current date and update status bar */
		Date d=new Date();
		long delta;

		delta=(d.getTime()-recordDate.getTime())/1000;
		
		statusLabel.setText("Last record received at " + recDate + " ( " + delta + " seconds ago)");

	}
	
	public void addHistoricalRecord(Wind2Record rec) {
		for ( int i=0 ; i<3 ; i++ ) {
			if ( false == hideAnemometer[i] ) {
				anemometer[i].setWindHistory(rec.dlogDate,rec.windSpeed[i],rec.windGust[i]);
			}
		}
	
		/* we should really send only those we need to */
		for ( int j=0 ; j<nPlots ; j++ ) {
			for ( int i=0 ; i<8 ; i++ ) {
				user[j].setChannelHistory(rec.dlogDate,i,rec.adcVoltage[i]);
			}
		}
		windDirection.setWindDirectionHistory(rec.dlogDate,rec.windDirection);
	}

	public void updateDisplay(Wind2Record rec) {
		/* timer for updating the status bar */
		if ( ! timer.isRunning() ) {
			timer.start();
		}

		recordDate=rec.rxDate;
		recDate=rec.date;
		updateStatus();
			
		
		for ( int i=0 ; i<3 ; i++ ) {
			if ( false == hideAnemometer[i] ) {
				anemometer[i].setWind(rec.windSpeed[i],rec.windGust[i],rec.windCount[i]);
			}
		}
		

		for ( int j=0 ; j<nPlots ; j++ ) {
			for ( int i=0 ; i<8 ; i++ ) {
				user[j].setChannel(i,rec.adcVoltage[i]);
			}
		}

		windDirection.setWindDirection(rec.windDirection);
		
		f.repaint();
	}

	protected void readIni(IniFile ini) {
		double m,b;
		String name, label, format;
		boolean disabled=true;
		int plotNumber;
		stationID=ini.getValue("GENERAL","stationID");
		imageFileName=ini.getValue("GUI","imageFileName");
		maxAge=Integer.parseInt(ini.getValue("GUI","historySeconds"));

		//System.err.println("uc is " + uc);
		
		/* read adc channels */
		for ( int i=0 ; i<8 ; i++ ) {
			String subject="ADC" + i;

			try { 
				m=Double.parseDouble(ini.getValue(subject,"m"));
				b=Double.parseDouble(ini.getValue(subject,"b"));
				name=ini.getValue(subject,"name");
				label=ini.getValue(subject,"label");
				format=ini.getValue(subject,"format");
				disabled=ini.isTrue(subject,"disabled");
				plotNumber=Integer.parseInt(ini.getValue(subject,"plot"));
				
			} catch ( Exception e ) {
				System.err.println("# Error encountered when reading USER ADC channel from INI File " + e);
				m=0.0;
				b=0.0;
				name="Undefined";
				label="volts";
				format="0.00";
				disabled=true;
				plotNumber=0;
			}

			uc[i]=new UserChannel(i,m,b,label,name,format,false,disabled,plotNumber);
			
		}

		/* panels to show */
		hideAnemometer[0]=ini.isTrue("AN0","disabled");
		hideAnemometer[1]=ini.isTrue("AN1","disabled");
		hideAnemometer[2]=ini.isTrue("AN2","disabled");
		//hideAnalog=ini.isTrue("GUI","hideAnalog");
		hideUser=ini.isTrue("GUI","hideUser");
		hideWindDirection=ini.isTrue("WINDVANE","disabled");

		
		/* get anemometer labels */
		for ( int i=0 ; i<3 ; i++ ) {
			String subject="AN" + i;
			try { 
				anemometerType[i]=ini.getValueSafe(subject,"type","");
			} catch ( Exception e ) {
				System.err.println("# Error encountered when reading " + subject + " configuration from INI File " + e);
			}
		}
	}
	
	public void setVisible(boolean state) {
		f.setVisible(state);
	}

	public Wind2SmallDisplay(IniFile ini) {
		WindowUtilities.setNativeLookAndFeel();
	
		hideAnemometer=new boolean[3];
		anemometerType=new String[3];
	
		uc=new UserChannel[8];
		
		readIni(ini);

		anemometer=new AnemometerPanel[3];

		String sUnits="MPH";


		//int nPlots=0;
		int plots[]=new int[uc.length];
		for ( int i=0 ; i<uc.length ; i++ ) {
			plots[uc[i].getPlotNumber()]++;
		}
		for ( int i=0 ; i<plots.length ; i++ ) {
			if ( plots[i] > 0 )
				nPlots++;
			System.err.println("plot[" + i + "]=" + plots[i]);
			                                 
		}
		nPlots-=1;
		
		user=new UserADCPanel[nPlots];
		
		for ( int i=0 ; i<nPlots ; i++ ) {
			user[i]=new UserADCPanel("Analog Sensors (plot " + (i+1) + "):", uc, maxAge,i+1);
		}
		windDirection=new WindDirectionPanel("Wind Vane:",maxAge);

		f = new JFrame("Current Weather at " + stationID );
		f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		Image icon = Toolkit.getDefaultToolkit().getImage("small_sunny.gif");
		f.setIconImage(icon);

		
		/* determine our current screen size then take a portion of it */
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        int width=(int) dim.getWidth();
        int height=(int) dim.getHeight();
        System.err.println("Screen size " + width + "x" + height);
        
        height-=50;
		
        
		f.setSize(width, height);

		/* Overall BorderLayout */
		Container cont = f.getContentPane();
		cont.setBackground(Color.white);
		cont.setLayout(new BorderLayout());
		
		/* Our body section */
		Container content=new Container();
		
		content.setBackground(Color.white);

		content.setLayout(new GridLayout(0,2)); /* two columns wide ... as long as we need */
		
		
		for ( int i=0 ; i<3 ; i++ ) {
			String anemometerTitle=new String("Anemometer " + i + " - " + anemometerType[i] + " - ");
			anemometer[i]=new AnemometerPanel(anemometerTitle,sUnits,maxAge,width/2-20,height/2-65);
			if ( false == hideAnemometer[i] ) {
				content.add(anemometer[i]);
				//content.add(new JLabel("This space intentionally blank"));	
			}
		}

		if ( false == hideWindDirection ) 
			content.add(windDirection);
		if ( false == hideUser ) { 
			for ( int i=0 ; i<nPlots ; i++ ) {
				content.add(user[i]);
			}
		}
	
		/* title text and photo */
		Container titleContainer=new Container();
		titleContainer.setLayout(new FlowLayout());
		
		if ( null != imageFileName && 0 != imageFileName.compareTo("") ) {
			/* big photo of datalogger */
			ImageIcon photo = new ImageIcon(imageFileName,"User Image");
			JLabel photoLabel = new JLabel(photo,JLabel.CENTER);
			titleContainer.add(photoLabel);
		}

		JLabel titleLabel = new JLabel("Current Weather at " + stationID);
		titleLabel.setFont(new Font("Serif", Font.BOLD, 36));
		titleLabel.setForeground(Color.blue);
		titleContainer.add(titleLabel);
		
		/* add the title */
		cont.add(titleContainer, BorderLayout.PAGE_START);
		
		/* Add the body */
		cont.add(content, BorderLayout.CENTER);

		/* Add our status bar */
		statusLabel = new JLabel("No data received.",JLabel.CENTER);
		statusLabel.setOpaque(true);
		statusLabel.setBackground(Color.lightGray);
		cont.add(statusLabel, BorderLayout.PAGE_END);

		f.setLocationRelativeTo(null);

		f.addWindowListener(new ExitListener());
//		f.setVisible(true);


//		System.err.println("anemometer panel size:" + anemometer[0].getSize());
		/* add a timer to keep our status bar updated */
		timer = new javax.swing.Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStatus();
			}
		});

	}
}
