import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Verwaltet Button
 * 
 * @author Felix Pfeffer 
 * @version 1.0
 */
public class Button extends Actor
{
    /**
      * neuer Button
      */
    private GreenfootImage but;
    /**
      * neuer gedrückter Button
      */
    private GreenfootImage but_pressed;
    /**
      * neuer geschlossener Button
      */
    private GreenfootImage but_locked;
    /**
      * Zähler
      */
    private long lastTime = System.currentTimeMillis();
    /**
      * falls true --> Button geschlossen
      */
    private boolean locked = false;
    /**
      * falls true --> Button wurde gedrückt
      */
    private boolean status = false;
    
    public Button(GreenfootImage Button , GreenfootImage ButtonGedrueckt , GreenfootImage ButtonBlockiert)
    {
        but         = new GreenfootImage( Button );
        but_pressed = new GreenfootImage( ButtonGedrueckt );
        but_locked  = new GreenfootImage( ButtonBlockiert );
    }
    
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if( !locked)
        {
            if( Greenfoot.mouseClicked(this) )
            {
                TeD_World.M_KLICK.play();
                this.setImage(but_pressed);
                lastTime = System.currentTimeMillis(); 
            
                status = true;
            
            }
            else if( waitEn(lastTime,100) )
            {
                this.setImage(but);
            
            }
            else
            {
                status = false;
            }
        }
        else
        {
            this.setImage(but_locked);
        }
    }
    /**
      * locked Button
      */
    public void lock()
    {
        locked = true;
    }
    /**
      * unlocked Button
      */
    public void unlock()
    {
        locked = false;
    }
    /**
      * gibt Button locked Status aus
      */
    public boolean getLockedStatus()
    {
        return locked;
    }
    /**
      * gibt Button locked Status aus
      */
    public boolean getStatus()
    {
        return status;
    }
    
    
    /**
      * Zähler
      */
    private boolean waitEn(long last , int intervall)
    {
        boolean b = false;
        
        if( System.currentTimeMillis() >= (last + intervall) )
        {
            b = true;
        }
        
        return b;
    }
}
