/*
 * 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.TimingTriggerer;
import csa.gui.TriggerCallback;
import csa.gui.Windowable;
import csa.gui.bitmapfont.BitmapFont;
import csa.gui.components.CSAView;
import csa.jmom.config.UIConfig;
import csa.tiletools.tiles.Tile;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

/**
 *
 * @author salchr
 */
public class GameMainMenuPanel extends javax.swing.JPanel implements Windowable {
    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 buttonFont;
    static private BitmapFont writtenByFont;
    static 
    {
        if (writtenByFont == null)
        {
            writtenByFont = UIConfig.getBaseFont().getDerivat(30);
            buttonFont = UIConfig.getBaseFont().getDerivat(50);
        }
    }
    
    /**
     * Creates new form GamePanel
     */
    public GameMainMenuPanel() {
        initComponents();
    }    
    public GameMainMenuPanel(CSAView jpv) {
        initComponents();
        mParent = jpv;
        setBackground(Color.BLACK);
        JLabel writtenBy = new JLabel();
        
        String text = "Written by Malban, 2013";
        try
        {
            BufferedImage image = writtenByFont.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();
        }
        
        
        
        
        
        addButton("Start");
        addButton("Test");
        addButton("Config");
        addButton("Quit");
        
        select(0);
        timer.setResolution(30);
        
        keyListener = new  csa.event.MasterEventListener()
        {
            public void eventOccured(csa.event.MasterEvent e)
            {
                if (e.eventHandled) return;
                if (e.keyboardState == e.KBD_STATE_PRESSED)
                {
                    if (e.keyCode == KeyEvent.VK_UP)
                    {
                        if (selected>=1) 
                            select(selected-1);
                        return;
                    }
                    else if (e.keyCode == KeyEvent.VK_DOWN)
                    {
                        if (selected<bList.size()-1)
                            select(selected+1);
                        return;
                    }
                }
                if (e.keyboardState == e.KBD_STATE_RELEASED)
                {
                    if (e.keyCode == KeyEvent.VK_ENTER)
                    {
                        buttonAction();
                        return;
                    }
                }
                // send key events to game!
            }
        };
        EventSupport.getEventSupport().addKeyEventListener(keyListener);
    }
    ArrayList<String> sList = new ArrayList<String>();
    ArrayList<JLabel> bList = new ArrayList<JLabel>();
    ArrayList<BufferedImage> iList = new ArrayList<BufferedImage>();
    int selected = -1;
    JLabel selectedButton = null;
    final TimingTriggerer timer = TimingTriggerer.getTimer();

    
    int r = 0;
    int g = 0;
    int b = 0;
    int a = 255;
    boolean forward = true;
    boolean noSelection = false;
    
    csa.event.MasterEventListener keyListener = null;
    private void deinit()
    {
        timer.removeTrigger(selectionCallback);
        EventSupport.getEventSupport().removeKeyEventListener(keyListener);
    }
    TriggerCallback selectionCallback = new TriggerCallback()
        {
            @Override
            public void doIt(int state, Object o)
            {
                if (selectedButton == null) return;
                if (forward)
                {
                    r = r + 10;
                    if (r>50)
                    {
                        r = 50;
                        forward = false;
                    }
                }
                else
                {
                    r = r - 10;
                    if (r<0)
                    {
                        r = 0;
                        forward = true;
                    }
                }
                
                Color c = new Color(r,b,g,a);
                selectedButton.setBackground(c);
                timer.addTrigger(selectionCallback, 60, 0, null);
            }
        };
    private void select(int no)
    {
        if (noSelection) return;
        if (selected == no) return;
        if (selected!=-1) UIConfig.doSoundSelectClick();
        selected = no;
        timer.removeTrigger(selectionCallback);
        if (selectedButton!=null)
        {
            selectedButton.setBackground(Color.BLACK);
            selectedButton.setOpaque(false);
        }
        
        if (no == -1)
        {
            timer.removeTrigger(selectionCallback);
            selectedButton = null;
        }
        else
        {
            selectedButton = bList.get(no);
            selectedButton.setBackground(Color.BLACK);
            selectedButton.setOpaque(true);
            r = 0;
            g = 0;
            b = 0;
            a = 255;
            timer.addTrigger(selectionCallback, 60, 0, null);
        }
    }
    
    private void reArrangeImageButtons()
    {
        int maxW = 0;
        int maxH = 0;

        int centerX = mParent.getUsableFrameWidth()/2;
        int centerY = mParent.getUsableFrameHeight()/2;
        
        
        for (int i=0; i<bList.size(); i++)
        {
            JLabel b = bList.get(i);
            remove(b);
        }
        bList.clear();
        
        for (int i=0; i<iList.size(); i++)
        {
            int w = iList.get(i).getWidth();
            int h = iList.get(i).getHeight();
            if (maxW<w) maxW=w;
            if (maxH<h) maxH=h;
        }
        
        int x = centerX-(maxW/2); // without button borders...
        int y = centerY-(iList.size()*(maxH/2)); // without button borders...
        for (int i=0; i<iList.size(); i++)
        {
            JLabel b = new JLabel();
            ImageIcon icon = new ImageIcon(iList.get(i));
            b.setHorizontalAlignment(SwingConstants.CENTER);
            b.setIcon(icon);
            b.setBounds(x, y, maxW, maxH);
            b.setBorder(null);
            b.setOpaque(false);
            b.setName(""+i);
            y+=maxH;
            add(b);
            bList.add(b);
            b.addMouseMotionListener(new java.awt.event.MouseAdapter() {
                    @Override
                    public void mouseMoved(java.awt.event.MouseEvent evt) 
                    {
                        select( csa.util.UtilityString.Int0(    ((JLabel)evt.getSource()).getName()    ));
                    }
                });
            b.addMouseListener(new java.awt.event.MouseAdapter() {
                    @Override
                    public void mousePressed(java.awt.event.MouseEvent evt) 
                    {
                        buttonAction();
                    }
                });
            
            
        }
        invalidate();
        validate();
        repaint();
    }
    private void addButton(String name)
    {
        BufferedImage image = buttonFont.drawText(name, 600);
        iList.add(image);
        sList.add(name);
        reArrangeImageButtons();
    }

    /**
     * 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

    TriggerCallback actionCallback = new TriggerCallback()
        {
            @Override
            public void doIt(int state, Object o)
            {
                if (selectedButton == null) return;
                if (forward)
                {
                    r = r + 10;
                    if (r>255)
                    {
                        r = 255;
                        forward = false;
                    }
                    timer.addTrigger(actionCallback, 10, 0, null);
                    Color c = new Color(r,b,g,a);
                    selectedButton.setBackground(c);
                }
                else
                {
                    doSelectedAction();
                }
                
            }
        };
    private void buttonAction()
    {
        noSelection = true;
        EventSupport.getEventSupport().removeKeyEventListener(keyListener);
        timer.removeTrigger(selectionCallback);
        forward = true;
        timer.addTrigger(actionCallback, 10, 0, null);
        UIConfig.doSoundActionClick();
    }
    private void doSelectedAction()
    {
        timer.removeTrigger(actionCallback);
        String action = sList.get(selected);
        mParent.doGameAction(action);
    }

}
