import java.awt.BorderLayout;
import javax.swing.JFrame;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 * Base clase to launch 1990
 * 
 * @author Fred Sonnenwald
 * @version November 19th 2016
 */
public class nn
{
    public static double TIMESTEP = 1000/60;

    public static void main(String[] args) throws InterruptedException
    {
        System.out.println("Starting 1990...");
        new nn().go();
    }

    private void go() throws InterruptedException
    {
        World w = new World();
        
        JFrame f = new JFrame();
        f.setTitle("Nineteen-ninety");
        f.setLocationByPlatform(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // f.setSize(640, 480);
        f.setSize(1280, 960);
        f.setResizable(false);
        f.setLayout(new BorderLayout());

        displaySplash(f);

        WorldInputHandler wih = new WorldInputHandler(w);
        f.addKeyListener(wih);
        
        DrawPanel mypanel = new DrawPanel(w);
        f.add(mypanel, BorderLayout.CENTER);

        
        // get going
        f.setVisible(true);
        double towait;
        while (true)
        {
            towait = System.nanoTime();
            w.step(TIMESTEP);
            mypanel.repaint();
            towait = (System.nanoTime()-towait)/1e6;
            // System.out.println(towait);
            Thread.sleep((long)(TIMESTEP-towait));
        }
    }
    
    private void displaySplash(JFrame f) throws InterruptedException
    {

        ImageIcon image = new ImageIcon("splash.png");
        JLabel imageLabel = new JLabel(image); 
        f.add(imageLabel, BorderLayout.CENTER);
        imageLabel.setBounds(320, 240, 640, 480);
        imageLabel.setVisible(true);
        f.setVisible(true);
        
        Thread.sleep(2000);
        imageLabel.setVisible(false);
    }
}
