package View;

import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.List;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Controller.Controller;
import Model.Meaning;
import Model.Situation;
import Model.Word;

/**
 * Panel to show the situation, as the original utterance and the produced scene as pictures.
 * @author simon
 *
 */

public class ScenePanel extends DataPanel {

	private GridBagConstraints c;
	
	public ScenePanel(Controller controller) {
		super(controller);
		this.setLayout(new GridBagLayout());
		c = new GridBagConstraints();
	}

	/**
	 * This method is called every time the iteration changes. Then the Panel has to get filled with the new situation
	 */
	
	public void updatePanel(){
		
		this.removeAll();
		
		//get the current situation
		Situation current=cont.getSituationData();
		if(current!=null){
			List<Meaning> scene=current.getScene();
			List<Word> utterance=current.getUtterance();
			
			
			//place utterance in the middle over the scene images
			c.gridx=0;
			c.gridy=0;
			c.weightx=1.0;
			c.weighty=0.1;
			c.fill = GridBagConstraints.NONE;
			c.anchor=GridBagConstraints.CENTER;
			
			String utt = current.getOriginalUtterance();
			
			JLabel uttLabel=new JLabel(utt);
			this.add(uttLabel,c);
			
			// include the images of the scene into a container with GridLayout and place it under the utterance label
			c.gridy=1;
			c.weighty=0.9;
			c.fill = GridBagConstraints.BOTH;
			
			// check for the best amount of images per row
			int row=1;
			while(row*row < scene.size()){
				row++;
			}
			JPanel container= new JPanel();
			container.setLayout(new GridLayout(row,row));
			
			int height=this.getHeight();
			int width=this.getWidth();
			height/=row+1;
			width/=row+1;
			
			
			
			//include the scene Images
			for(int i=0;i<scene.size();i++){	
				Meaning meaning=scene.get(i);
				Image image=meaning.getImage();
				if(image==null)
					container.add(new JLabel(new ImageIcon("images/Information24.gif")));
				else
					container.add(new JLabel(new ImageIcon(image.getScaledInstance(width, height, Image.SCALE_REPLICATE))));
				
			}
				
			this.add(container,c);
		}
		
	}

	
}
