/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csa.jmom.game;

import csa.event.EventSupport;
import csa.gui.Windowable;
import csa.gui.bitmapfont.BitmapFont;
import csa.gui.components.CSAView;
import csa.jmom.config.UIConfig;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

/**
 *
 * @author salchr
 */
public class GamePanel extends javax.swing.JPanel implements Windowable {

    csa.event.MasterEventListener keyListener = null;

    private CSAView mParent = null;
    private javax.swing.JMenuItem mParentMenuItem = null;
    private int mClassSetting=0;
    @Override
    public void closing()
    {
        deinit();
    }
    @Override
    public void setParentWindow(CSAView jpv)
    {
        mParent = jpv;
    }
    @Override
    public void setMenuItem(javax.swing.JMenuItem item)
    {
        mParentMenuItem = item;
        mParentMenuItem.setText("Game main menu");
    }
    @Override
    public javax.swing.JMenuItem getMenuItem()
    {
        return mParentMenuItem;
    }
    @Override
    public javax.swing.JPanel getPanel()
    {
        return this;
    }    
    
    static private BitmapFont gameFont;
    static 
    {
        if (gameFont == null)
        {
            gameFont = UIConfig.getBaseFont().getDerivat(16);
        }
    }    /**
     * Creates new form GamePanel
     */
    public GamePanel() {
        initComponents();
    }
    public GamePanel(CSAView jpv) 
    {
        initComponents();
        mParent = jpv;
        setBackground(Color.BLACK);
        JLabel writtenBy = new JLabel();
        
        String text = "Written by Malban, 2013";
        try
        {
            BufferedImage image = gameFont.drawText(text, 800);
            writtenBy.setIcon(new ImageIcon(image));

            int x = (mParent.getUsableFrameWidth()/2)-(image.getWidth()/2);
            int y = mParent.getUsableFrameHeight()-40;
            int w = image.getWidth();
            int h = image.getHeight();
  
            
            writtenBy.setBounds(x, y, w, h);
            this.add(writtenBy);
            invalidate();
            validate();
            repaint();
        }
        catch (Throwable e)
        {
            e.printStackTrace();
        }
        
        
        
        keyListener = new  csa.event.MasterEventListener()
        {
            public void eventOccured(csa.event.MasterEvent e)
            {
                if (e.eventHandled) return;

                handleGameEvent(e);
                
                // send key events to game!
            }
        };
        EventSupport.getEventSupport().addKeyEventListener(keyListener);
    }

    /**
     * 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() {

        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    private void handleGameEvent(csa.event.MasterEvent e)
    {
        
    }

    private void deinit()
    {
        EventSupport.getEventSupport().removeKeyEventListener(keyListener);
    }
}
