package gui;

import java.util.HashMap;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import controller.Condition;


public class ConditionPanel extends JPanel 
{
	private RobotEnviromentSensorPanel left_sensor_;
    private RobotEnviromentSensorPanel right_sensor_;
    private RobotEnviromentSensorPanel ahead_sensor_;
    private RobotBottomSensorPanel bottom_sensor_;   
    private RobotStatePanel state_panel_;
    
    private boolean highlight_;
	private HashMap brick_images_;
	private TUGLabyrinth labyrinth_;
	
	public ConditionPanel(HashMap brick_images, TUGLabyrinth labyrinth) 
    {	    
		super();
		labyrinth_=labyrinth;
        brick_images_ = brick_images;
        highlight_=false;
        
        left_sensor_ = new RobotEnviromentSensorPanel(brick_images_);
        right_sensor_ = new RobotEnviromentSensorPanel(brick_images_);
        ahead_sensor_ = new RobotEnviromentSensorPanel(brick_images_);
        bottom_sensor_ = new RobotBottomSensorPanel(brick_images_,labyrinth_);
        state_panel_ = new RobotStatePanel(brick_images_,labyrinth_, false);
        
        initComponents();   
	}

	public ConditionPanel(HashMap brick_images, TUGLabyrinth labyrinth, Condition condition) 
    {	    
		super();
		labyrinth_=labyrinth;
        brick_images_ = brick_images;
        highlight_=false;

        int tmp;
        if (condition.isWallLeft() == true)
        	tmp = RobotEnviromentSensorPanel.WALL;
        else
         	tmp = RobotEnviromentSensorPanel.FREE;	
        left_sensor_ = new RobotEnviromentSensorPanel(brick_images_, tmp);
        if (condition.isWallRight() == true)
        	tmp = RobotEnviromentSensorPanel.WALL;
        else
         	tmp = RobotEnviromentSensorPanel.FREE;	
        right_sensor_ = new RobotEnviromentSensorPanel(brick_images_, tmp);
        if (condition.isWallAhead() == true)
        	tmp = RobotEnviromentSensorPanel.WALL;
        else
         	tmp = RobotEnviromentSensorPanel.FREE;	
        ahead_sensor_ = new RobotEnviromentSensorPanel(brick_images_, tmp);        
        bottom_sensor_ = new RobotBottomSensorPanel(brick_images_, condition.getCellMark()+1, labyrinth_);        
        state_panel_ = new RobotStatePanel(brick_images_,condition.getState()+1, labyrinth_, false);
        initComponents();   
	}
	
	private void initComponents() 
	{
        JLabel top_label = new JLabel();
        this.setLayout(null);
        this.add(top_label);
        this.add(left_sensor_);
        this.add(right_sensor_);
        this.add(ahead_sensor_);
        this.add(bottom_sensor_);
        this.add(state_panel_); 
        top_label.setText("Sensorregel:");
        top_label.setBounds(3, 0, 150, 15);        
        left_sensor_.setBounds(8, 38, 20, 20);
        ahead_sensor_.setBounds(30, 16, 20, 20);
        right_sensor_.setBounds(52, 38, 20, 20);
        bottom_sensor_.setBounds(30, 38, 20, 20);
        state_panel_.setBounds(25, 60, 30, 15);

        setBackGround();
        repaint();
        System.out.println("\n ConditionPanel  --> initComponents");  
    }
    
	public Condition getCondition() 
    {        
    	boolean ahead_sensor_bool, left_sensor_bool, right_sensor_bool;      
    	if (ahead_sensor_.getInternalState() == RobotEnviromentSensorPanel.FREE)
        	ahead_sensor_bool = false;
        else
         	ahead_sensor_bool = true;
       	if (left_sensor_.getInternalState() == RobotEnviromentSensorPanel.FREE)
         	left_sensor_bool = false;
    	else
         	left_sensor_bool = true;
    	if (right_sensor_.getInternalState() == RobotEnviromentSensorPanel.FREE)
           	right_sensor_bool = false;
        else
          	right_sensor_bool = true;
    
        Condition ret_condition = new Condition(state_panel_.getInternalState()-1,
                bottom_sensor_.getInternalState()-1, left_sensor_bool, right_sensor_bool, ahead_sensor_bool);
        return ret_condition;
    }
    
    private void setBackGround()
    {
	    if(highlight_==false)
        	this.setBackground(Color.GRAY);
        else
        	this.setBackground(Color.RED.darker());
	}
    
    public void setHighlighting (boolean highlight)
    {
		highlight_=highlight;
		setBackGround();
		repaint();   
	}
	
	public boolean isHighlighting()
	{
		return highlight_;
	}
}
