/*
 * Created on 15.09.2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package TUGLaby;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.LayoutManager;
import java.util.HashMap;

import javax.swing.JPanel;

/**
 * @author user
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class ConditionChoosePanel extends JPanel {

    /**
     *  
     */

    private HashMap brick_images_;

    /**
     *  
     */
    private Image current_image_;

    private int internal_state_;

    private boolean on_cond_panel_;

    public final static int NONE = 0;

    public final static int GREEN = 1;

    public final static int BLUE = 2;

    // must be the count of states (HACK)
    public final static int STATE_COUNT = 3;

    public ConditionChoosePanel(HashMap brick_images) {
        super();
        // TODO Auto-generated constructor stub
        brick_images_ = brick_images;
        this.setBackground(Color.green);
        internal_state_ = NONE;
        current_image_ = (Image) brick_images_.get("COND_NONE");
        this.setToolTipText("Anklicken um den Zustand zu ändern.");
        on_cond_panel_ = false;
        initMouseListener();
    }

    public ConditionChoosePanel(HashMap brick_images, boolean cond_panel) {
        this(brick_images);
        on_cond_panel_ = cond_panel;
        if (on_cond_panel_)
            current_image_ = (Image) brick_images_.get("COND_NONE_1");
    }

    public ConditionChoosePanel(HashMap brick_images, int init_state) {
        this(brick_images);
        setInternalState(init_state);

    }

    public ConditionChoosePanel(HashMap brick_images, int init_state,
            boolean cond_panel) {
        this(brick_images, init_state);
        on_cond_panel_ = cond_panel;
        if (on_cond_panel_)
            current_image_ = (Image) brick_images_.get("COND_NONE_1");

    }

    private void setInternalState(int new_state) {

        if ((new_state >= NONE) && (new_state <= STATE_COUNT))
            internal_state_ = new_state;
        paintState();
    }

    public int getInternalState() {
        return internal_state_;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawImage(current_image_, 0, 0, this);

    }

    private void paintState() {
        String postnote;
        if (on_cond_panel_)
            postnote = "_1";
        else
            postnote = "";
        System.out.println(postnote + " : " + on_cond_panel_);
        switch (internal_state_) {
        case NONE:
            current_image_ = (Image) brick_images_.get("COND_NONE" + postnote);
            break;
        case BLUE:
            current_image_ = (Image) brick_images_.get("COND_BLUE" + postnote);
            break;
        case GREEN:
            current_image_ = (Image) brick_images_.get("COND_GREEN" + postnote);
            break;

        }
        ConditionChoosePanel.this.repaint();
    }

    private void initMouseListener() {
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                internal_state_ = ((internal_state_ + 1) % STATE_COUNT);
                //System.out.println("Internal State: "+internal_state_ );
                paintState();
            }
        });
    }

}
