package net.mkonrad.synes.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import net.mkonrad.synes.data.Config;
import net.mkonrad.synes.util.DbgTools;
import net.mkonrad.synes.util.ErrTools;

public class Main extends JFrame implements WindowListener
{
  SceneCoreOSC _core;
  
	public Main() 
	{
		super("Synesthesia");
		
		if (!hasJ3D())
		{
      ErrTools.die("Java3D Libraries not found!");
		}
		
	  //DbgTools.setDbgMode(System.getProperty("debug") != null &&
	  //System.getProperty("debug").equals("true"));
	  DbgTools.setDbgMode(true);
	  
	  boolean oscOn = (System.getProperty("osc") == null
	      || System.getProperty("osc").equals("true"));
	  	
	  Container c = getContentPane();
	  c.setLayout(new BorderLayout());
	  
	  _core = new SceneCoreOSC(this, new Config(), oscOn);
	  c.add(_core, BorderLayout.CENTER);
	  
	  addWindowListener(this);
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		pack();
		setResizable(false);
		
		centerWindow();
		
		setVisible(true);
	}

	public static void main(String[] args)
	{
		new Main();
	}
	
  private boolean hasJ3D()
  {
    try
    {   // test for an essential Java 3D class
      Class.forName("com.sun.j3d.utils.universe.SimpleUniverse");
      return true;
    }
    catch(ClassNotFoundException e)
    {
      return false;
    }
  }
  
  private void centerWindow()
  {
  	Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension winDim = this.getSize();

    this.setLocation( (screenDim.width-winDim.width)/2,
                      (screenDim.height-winDim.height)/2);
  }

  public void windowActivated(WindowEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void windowClosed(WindowEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void windowClosing(WindowEvent arg0)
  {
    _core.shutdown();
  }

  public void windowDeactivated(WindowEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void windowDeiconified(WindowEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void windowIconified(WindowEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }

  public void windowOpened(WindowEvent arg0)
  {
    // TODO Auto-generated method stub
    
  }
}