package gui;
import java.util.HashMap;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;

import controller.Action;

public class ActionPanel extends JPanel 
{

	private RobotBottomSensorPanel bottom_sensor_;
	private RobotDirectionPanel direction_panel_;
	private RobotStatePanel state_panel_;
	private boolean highlight_;
	private HashMap brick_images_;
	private TUGLabyrinth labyrinth_;
	
	public ActionPanel(HashMap brick_images, TUGLabyrinth labyrinth) 
    {	    
		super();
		labyrinth_=labyrinth;
        brick_images_ = brick_images;
        highlight_=false;
        bottom_sensor_ = new RobotBottomSensorPanel(brick_images_,labyrinth_);
        direction_panel_ = new RobotDirectionPanel(brick_images_);
        state_panel_ = new RobotStatePanel(brick_images_,labyrinth_, false);
        initComponents();   
	}
	

	public ActionPanel(HashMap brick_images, TUGLabyrinth labyrinth, Action action) 
    	{	    
		super();
		labyrinth_=labyrinth;
        brick_images_ = brick_images;
        highlight_=false;
               
        int direction = 0;
        switch (action.getMovingDirection()) 
        {
        case Action.MOVE_AHEAD:
        	direction = RobotDirectionPanel.UP;
            break;
        case Action.MOVE_RIGHT:
            direction = RobotDirectionPanel.RIGHT;
            break;
        case Action.MOVE_LEFT:
            direction = RobotDirectionPanel.LEFT;
            break;
        case Action.MOVE_BACK:
            direction = RobotDirectionPanel.DOWN;
        }

        bottom_sensor_ = new RobotBottomSensorPanel(brick_images_, action.getCellMark()+1, labyrinth_);
        
        direction_panel_ = new RobotDirectionPanel(brick_images_, direction);
        
        state_panel_ = new RobotStatePanel(brick_images_,action.GetState()+1, labyrinth_, false);
        
        initComponents();   
	}
	
	private void initComponents() 
	{
        JLabel top_label = new JLabel();
        JLabel info_label = new JLabel();
        this.setLayout(null);
        this.add(top_label);
        this.add(info_label);
        this.add(bottom_sensor_);
        this.add(direction_panel_);
        this.add(state_panel_);
        top_label.setText("Robotersteuerung:");
        top_label.setForeground(Color.white);
        top_label.setBounds(5, 2, 140, 15);
        info_label.setText("Gehe:");
        info_label.setBounds(10, 20, 140, 20);
        info_label.setForeground(Color.white);
        direction_panel_.setBounds(50, 20, 20, 20);
        bottom_sensor_.setBounds(75, 20, 20, 20);
        state_panel_.setBounds(100, 25, 30, 15);
        setBackGround();
        this.setBorder(new javax.swing.border.SoftBevelBorder(
                javax.swing.border.BevelBorder.RAISED));
        repaint();
        System.out.println("\n ActionPanel  --> initComponents");  
    }
    
    public Action getAction() 
    {
        int direction = 0;
        switch (direction_panel_.getInternalState()) 
        {
        case RobotDirectionPanel.UP:
            direction = Action.MOVE_AHEAD;
            break;
        case RobotDirectionPanel.RIGHT:
            direction = Action.MOVE_RIGHT;
            break;
        case RobotDirectionPanel.LEFT:
            direction = Action.MOVE_LEFT;
            break;
        case RobotDirectionPanel.DOWN:
            direction = Action.MOVE_BACK;
        }

        Action ret_action = new Action(state_panel_.getInternalState()-1,
                bottom_sensor_.getInternalState()-1, direction);
        return ret_action;
    }
    
    private void setBackGround()
    {
	    if(highlight_==false)
        	this.setBackground(Color.DARK_GRAY);
        else
        	this.setBackground(Color.RED.darker().darker());
	}
    
    public void setHighlighting (boolean highlight)
    {
		highlight_=highlight;
		setBackGround();
		repaint();   
	}
	
	public boolean isHighlighting ()
	{
		return highlight_;
	}
}