package View;

import Controller.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * The Navigationbar in the left bottom of the screen.
 * As elements it has a button to go to the first iteration, to go N steps back, a Textfield to specify N, a button to go N steps forward, one to go to the last iteration and a Slider. 
 * @author simon
 *
 */

public class NavigationPanel extends MyPanel {

	
	
	
	private JButton firstButton;
	private JButton previousButton;
	private JButton nextButton;
	private JButton lastButton;
	private JTextField field;
	
	private JSlider slidebar;
	private JLabel label;
	
	public NavigationPanel(final Controller controller){
		super(controller);
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		//Buttons
		c.weightx = 0.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth=1;
        c.gridheight=2;
    	c.gridx = 0;
    	c.gridy = 0;
	
    	
    	//first button
    	
    	String urlString="images/Rewind24.gif";
    	URL iconURL = this.getClass().getResource( urlString );
    	ImageIcon first = new ImageIcon(urlString);
		
    	firstButton=new JButton(first);
    	this.add(firstButton,c);
    	
    	//add an action listener
    	firstButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                cont.goToFirstIteration();
                GUI.updatePanels();
            }
        });
    	
    	// previous button
    	c.gridx = 1;
    	
    	urlString="images/StepBack24.gif";
    	iconURL = this.getClass().getResource( urlString );
    	ImageIcon left = new ImageIcon(urlString);
		
    	previousButton=new JButton(left);
    	this.add(previousButton,c);
    	
    	//add an action listener
    	previousButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	int stepSize=1;
            	try{
            		stepSize=Integer.parseInt(field.getText());
            	}catch(NumberFormatException e){
            		
            	}
            	
                cont.goBackward(stepSize);
                GUI.updatePanels();
            }
        });
    	
    	
    	//Text Field for the stepsize
    	c.gridx = 2;
    	c.fill = GridBagConstraints.NONE;
    	field=new JTextField("1");
    	field.setFont(new Font("mine",1,30));
    	field.setColumns(3);
    	
    	this.add(field,c);

    	
    	// next button
    	c.gridx = 3;
    	c.fill = GridBagConstraints.HORIZONTAL;

    	urlString="images/StepForward24.gif";
    	iconURL = this.getClass().getResource( urlString );
    	ImageIcon right = new ImageIcon(urlString);
		
    	nextButton=new JButton(right);
    	this.add(nextButton,c);
    	
    	//add the actionlistener to go forward
    	nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            	int stepSize=1;
            	try{
            		stepSize=Integer.parseInt(field.getText());
            	}catch(NumberFormatException e){
            		
            	}
                cont.goForward(stepSize);
                GUI.updatePanels();
            }
        });
    	
    	
    	// last button
    	c.gridx = 4;
    	
    	urlString="images/FastForward24.gif";
    	iconURL = this.getClass().getResource( urlString );
    	ImageIcon last = new ImageIcon(urlString);
		
    	lastButton=new JButton(last);
    	this.add(lastButton,c);
    	
    	//add an action listener
    	lastButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                cont.goToLastIteration();
                GUI.updatePanels();
            }
        });
    	
    	
    	// add the slide bar
    	c.weightx = 0.7;
    	c.gridx = 5;
    	c.gridheight=1;
    	
		slidebar = new JSlider(JSlider.HORIZONTAL,1, 2, cont.getCurrentIteration());
        slidebar.setPaintTicks(true);

        slidebar.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent arg0) {
            	int iteration=slidebar.getValue();
                cont.goToStep(iteration);
                GUI.updatePanels();
            }
        });
        
		this.add(slidebar,c);
		
		
		//add a label of the current iteration
		c.gridy=1;
		c.fill=GridBagConstraints.NONE;
		c.anchor=GridBagConstraints.CENTER;
		
		label=new JLabel(Integer.toString(cont.getCurrentIteration()));
		this.add(label,c);
		
	}
	
	/**
	 * This method is called every time the iteration changed. It set the slider and label to the current iteration.
	 */
	
	public void updatePanel(){
		if(cont.getLastIteration()>1)
			slidebar.setMaximum(cont.getLastIteration());
		int iteration=cont.getCurrentIteration();
		slidebar.setValue(iteration);
		label.setText(Integer.toString(iteration));
	}
	
}
