/*
    Moenagade
    °°°°°°°°°

    Moenagade is a graphical programming tool for beginners. For this
    reason it is rather limited in its functionality. It aims to help
    students to gain knowledge about the programming structures. Due
    to the explicit conversion to real and executable Java code, it 
    should help to make an easy from block programming to coding.

    Copyright (C) 2009  Bob Fisch

    This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package moenagade.base;

import java.awt.Insets;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;


/**
 *
 * @author robert.fisch
 */
public abstract class MainFrame extends javax.swing.JFrame implements KeyEventDispatcher {

    private ArrayList<Entity> entities = new ArrayList<>();
    private World world = null;

    private Insets insets;

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        // enable hardware acceleration
        System.setProperty("sun.java2d.opengl", "true");
        System.setProperty("sun.java2d.d3d", "true");
        System.setProperty("sun.java2d.accthreshold", "0");

        initComponents();

        setResizable(false);
        
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(this);

        setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);

        pack();
        insets = getInsets();

        onCreate();
    }
    
    protected void onCreate()
    {
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent e) {
        if (e.getID() == KeyEvent.KEY_PRESSED) 
        {
            if(world!=null) world.keyPressed(e);
            for (int i = 0; i < entities.size(); i++) {
                entities.get(i).keyPressed(e);
            }
        } 
        else if (e.getID() == KeyEvent.KEY_RELEASED) 
        {
            if(world!=null) world.keyReleased(e);
            for (int i = 0; i < entities.size(); i++) {
                entities.get(i).keyReleased(e);
            }
        } 
        else if (e.getID() == KeyEvent.KEY_TYPED) 
        {
            if(world!=null) world.keyTyped(e);
            for (int i = 0; i < entities.size(); i++) {
                entities.get(i).keyTyped(e);
            }
        }
        return false;
    }

    public void setWindowSize(int width, int height)
    {
        setSize(width, height);
        revalidate();
    }
    
    public void setWorld(World world)
    {
        // stop sounds that are still active
        if(this.world!=null)
        {
            this.world.stopClips();
            this.world.stopTimers();
        }

        this.world=world;

        if(world!=null)
        {
            setWindowSize(insets.left + insets.right+world.getWidth(), 
                          insets.top + insets.bottom+ world.getHeight());
            world.setMain(this);
        }
        getContentPane().removeAll();
        
        if(world!=null)
        {
            getContentPane().add(world);
            this.entities=world.getEntities();
            world.repaint();
        }
    }

    public World getWorld() {
        return world;
    }
 
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {  
        setWorld(null);
        System.gc();
    } 

    public static void stop(Object mainframe) {  
        MainFrame frame = (MainFrame) mainframe;
        frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
        frame.dispose();
        System.gc();
    } 


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
