package ui.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.Timer;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import classes.HolonElement;
import classes.HolonObject;
import classes.HolonSwitch;
import classes.SubNet;
import ui.controller.Control;
import ui.model.Model;
import javax.swing.BoxLayout;

/**
 * This Class represents a Panel where the User can start and stop the
 * Simulation. He Can also reset the Simulation and click through every
 * Iteration.
 * 
 * @author Gruppe14
 */
public class TimePanel extends JPanel {

	private Model model;
	private Control controller;

	JSlider timeSlider = new JSlider();
	private final JPanel timeBtnPanel = new JPanel();
	final JButton playBtn = new JButton();
	final JButton timeResetBtn = new JButton();
	final JButton timeForwardBtn = new JButton();
	final JButton timeBackwardBtn = new JButton();
	private Timer timer;
	private boolean running = false;

	private static final long serialVersionUID = 1L;

	/**
	 * Constructor.
	 * 
	 * @param mod
	 *            the Model
	 * @param cont
	 *            the Controller
	 */
	public TimePanel(Model mod, Control cont) {
		super();
		this.model = mod;
		this.controller = cont;

		// One Iteration
		timer = new Timer(model.getTimerSpeed(), new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				timeSlider.setValue(timeSlider.getValue() + 1);
				controller.setCurIteration(timeSlider.getValue());
				timer.setDelay(model.getTimerSpeed());
				if (timeSlider.getValue() == model.getIterations() - 1) {
					running = false;
					playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/play.png"))
							.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
					timer.stop();
				}
			}
		});

		// Time Slider. Panels and Buttons
		this.setLayout(new BorderLayout(0, 0));
		this.setBorder(null);
		// Slider
		timeSlider.setPaintTicks(true);
		timeSlider.setPaintLabels(true);
		timeSlider.setMinorTickSpacing(1);
		timeSlider.setMajorTickSpacing(5);
		timeSlider.setToolTipText(Languages.getLanguage()[93]);
		timeSlider.setMaximum(model.getIterations() - 1);
		timeSlider.setValue(0);
		timeSlider.setBackground(Color.WHITE);
		timeSlider.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				controller.setCurIteration(timeSlider.getValue());
			}
		});

		// Panel
		timeBtnPanel.setBorder(null);
		timeBtnPanel.setBackground(Color.WHITE);
		timeBtnPanel.setLayout(new BoxLayout(timeBtnPanel, BoxLayout.X_AXIS));

		// Buttons
		playBtn.setToolTipText(Languages.getLanguage()[89]);

		playBtn.setBackground(Color.WHITE);
		playBtn.setBorderPainted(false);
		playBtn.setBorder(new LineBorder(Color.WHITE, 4));
		playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/play.png")).getImage()
				.getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
		playBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				running = !running;
				if (running) {
					timer.start();
					timer.setDelay(model.getTimerSpeed());
					playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/pause.png"))
							.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
				} else {
					timer.stop();
					playBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/play.png"))
							.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
				}
			}
		});
		timeResetBtn.setToolTipText(Languages.getLanguage()[90]);

		timeResetBtn.setBorderPainted(false);
		timeResetBtn.setBackground(Color.WHITE);
		timeResetBtn.setBorder(new LineBorder(Color.WHITE, 4));
		timeResetBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/reset.png"))
				.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
		timeResetBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				timeSlider.setValue(timeSlider.getMinimum());
				controller.setCurIteration(timeSlider.getValue());
				controller.calculateStateForCurrentTimeStep();
			}
		});
		timeForwardBtn.setToolTipText(Languages.getLanguage()[91]);

		timeForwardBtn.setBorderPainted(false);
		timeForwardBtn.setContentAreaFilled(false);
		timeForwardBtn.setBorder(new LineBorder(Color.WHITE, 4));
		timeForwardBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/forward.png"))
				.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
		timeForwardBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				timeSlider.setValue(timeSlider.getValue() + 1);
				controller.setCurIteration(timeSlider.getValue());
			}
		});
		timeBackwardBtn.setToolTipText(Languages.getLanguage()[92]);

		timeBackwardBtn.setBackground(Color.WHITE);
		timeBackwardBtn.setBorderPainted(false);
		timeBackwardBtn.setBorder(new LineBorder(Color.WHITE, 4));
		timeBackwardBtn.setIcon(new ImageIcon(new ImageIcon(this.getClass().getResource("/Button_Images/backward.png"))
				.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH)));
		timeBackwardBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				timeSlider.setValue(timeSlider.getValue() - 1);
				controller.setCurIteration(timeSlider.getValue());
			}
		});

		timeBtnPanel.add(playBtn);
		timeBtnPanel.add(timeResetBtn);
		timeBtnPanel.add(timeBackwardBtn);
		timeBtnPanel.add(timeForwardBtn);

		this.add(timeBtnPanel, BorderLayout.WEST);
		this.add(timeSlider);
	}

	/**
	 * Retunrs the TimeSlider.
	 * 
	 * @return the TimeSlider
	 */
	public JSlider getTimeSlider() {
		return timeSlider;
	}
}
