package de.stoermer.xxx.client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import de.stoermer.xxx.domain.Session;
import de.stoermer.xxx.domain.aufgaben.Aufgabe;
import de.stoermer.xxx.domain.aufgaben.Timer;

public class IngamePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private boolean doContinue;
	private MainFrame mainFrame;
	private Session session;
	private Clip clip;
	private JPanel northPanel;

	public IngamePanel(MainFrame mainFrame, Session session) {
		super(new BorderLayout());
		this.mainFrame = mainFrame;
		this.session = session;
	}
	
	private JPanel getNorthPanel() {
		if (northPanel == null) {
			northPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
			
			JButton hauptmenue = new JButton("Hauptmenü");
			hauptmenue.addActionListener((e) -> mainFrame.hauptMenu());
			northPanel.add(hauptmenue );
			
			JButton debug = new JButton("Debug Statistiken");
			debug.addActionListener((e) -> new DebugDialog(mainFrame, session));
			northPanel.add(debug);
		}
		return northPanel;
	}
	
	public void init() {
		naechsteAufgabe(session.naechsteAufgabe());
	}
	
	private void naechsteAufgabe(Aufgabe aufgabe) {
		if (aufgabe.getSpieler2() != null) {
			showName(aufgabe);
		} else {
			showAufgabe(aufgabe);
		}
	}
	
	@Override
	public void removeAll() {
		super.removeAll();
	
		this.add(getNorthPanel(), BorderLayout.NORTH);
	}
	
	private void showName(Aufgabe aufgabe) {	
		this.removeAll();
		
		JPanel panel = new JPanel(new BorderLayout());

		JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		namePanel.add(new JLabel("<html><b>" + aufgabe.getSpieler2().getName() + ", du bist an der Reihe</b></html>"));
		
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton button = new JButton("LOS");
		button.addActionListener((e) -> showAufgabe(aufgabe));
		buttons.add(button);

		panel.add(namePanel, BorderLayout.CENTER);
		panel.add(buttons, BorderLayout.SOUTH);
		
		this.add(panel, BorderLayout.CENTER);
		validate();
	}

	private void showAufgabe(Aufgabe aufgabe) {
		this.removeAll();
		JPanel panel = new JPanel(new BorderLayout());
		JTextArea area = new JTextArea(10, 50);
		area.setText(aufgabe.anzeigeText());
		area.setEditable(false);
		area.setWrapStyleWord(true);
		area.setLineWrap(true);
		panel.add(area, BorderLayout.CENTER);
		
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton abbrechen = new JButton("Abbrechen");
		abbrechen.addActionListener((e) -> {
			aufgabe.abbrechen();
			List<String> fertigeLocks = session.pruefeLocks();
			if (fertigeLocks != null && !fertigeLocks.isEmpty()) {
				showFertigeLocks(fertigeLocks);
			} else {
				naechsteAufgabe(session.naechsteAufgabe());				
			}
		});
		buttons.add(abbrechen);
		if (aufgabe.getTimer() != null) {
			JButton button = new JButton("LOS");
			button.addActionListener((e) -> showTimer(aufgabe));
			buttons.add(button);
		} else {
			JButton ok = new JButton("LOS");
			ok.addActionListener((e) -> showOk(aufgabe));
			buttons.add(ok);
		}
		panel.add(buttons, BorderLayout.SOUTH);
		
		this.add(panel, BorderLayout.CENTER);
		validate();
	}
	
	private void showOk(Aufgabe aufgabe) {
		this.removeAll();
		JPanel panel = new JPanel(new BorderLayout());
		
		JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		textPanel.add(new JLabel("<html><b>Alles erledigt?</b></html>"), BorderLayout.CENTER);
		
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton weiter = new JButton("Weiter");
		weiter.addActionListener((e) -> {
			aufgabe.ok();
			List<String> fertigeLocks = session.pruefeLocks();
			if (fertigeLocks != null && !fertigeLocks.isEmpty()) {
				showFertigeLocks(fertigeLocks);
			} else {
				naechsteAufgabe(session.naechsteAufgabe());				
			}
		});
		buttons.add(weiter);
		
		panel.add(textPanel, BorderLayout.CENTER);
		panel.add(buttons, BorderLayout.SOUTH);
		this.add(panel, BorderLayout.CENTER);
		validate();
	}
	
	private void showFertigeLocks(List<String> uebergabe) {
		this.removeAll();
		final List<String> fertigeLocks = new ArrayList<>(uebergabe);
		String zuOeffnen = fertigeLocks.remove(0);
		
		JPanel panel = new JPanel(new BorderLayout());
		
		JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		textPanel.add(new JLabel("<html><b>" + zuOeffnen + "</b></html>"), BorderLayout.CENTER);
		
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton weiter = new JButton("Weiter");
		weiter.addActionListener((e) -> {
			if (fertigeLocks.isEmpty()) {
				naechsteAufgabe(session.naechsteAufgabe());	
			} else {
				showFertigeLocks(fertigeLocks);
			}
		});
		buttons.add(weiter);
		
		panel.add(textPanel, BorderLayout.CENTER);
		panel.add(buttons, BorderLayout.SOUTH);
		this.add(panel, BorderLayout.CENTER);
		validate();
		
		
	}

	private void showTimer(Aufgabe aufgabe) {
		this.removeAll();

		doContinue = true;
		JPanel panel = new JPanel(new BorderLayout());
	
		JPanel timerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JLabel timerLabel = new JLabel("0:00:00");
		timerPanel.add(timerLabel);
		Timer timer = aufgabe.getTimer();
		timer.start();
		new Thread(() -> {
			while (doContinue) {
				int a = timer.getRestSekunden();
			    int stunden = a / 3600;
			    int minuten = (a % 3600) / 60;
			    int sekunden = (a % 3600) % 60;
			    StringBuilder timeBuilder = new StringBuilder();
			    if (stunden > 0) {
			    	timeBuilder.append(stunden);
			    	if (minuten < 10) {
			    		timeBuilder.append(":0");
			    	} else {
			    		timeBuilder.append(":");
			    	}
			    	timeBuilder.append(minuten);
			    	if (sekunden < 10) {
			    		timeBuilder.append(":0");
			    	} else {
			    		timeBuilder.append(":");
			    	}
			    	
			    } else if (minuten > 0) {
			    	timeBuilder.append(minuten);
			    	if (sekunden < 10) {
			    		timeBuilder.append(":0");
			    	} else {
			    		timeBuilder.append(":");
			    	}
			    }
			    timeBuilder.append(sekunden);
			    
			    
				timerLabel.setText("<html><b>" + timeBuilder.toString() + "</b></html>");
				repaint();
				if (timer.getRestSekunden() <= 0) {
					doContinue = false;
					showAlarm(aufgabe);
				} else {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}
			}
		}).start();
		
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton abbrechen = new JButton("Abbrechen");
		abbrechen.addActionListener((e) -> {
			doContinue = false;
			aufgabe.abbrechen();
			List<String> fertigeLocks = session.pruefeLocks();
			if (fertigeLocks != null && !fertigeLocks.isEmpty()) {
				showFertigeLocks(fertigeLocks);
			} else {
				naechsteAufgabe(session.naechsteAufgabe());				
			}
		});
		buttons.add(abbrechen);

		panel.add(timerPanel, BorderLayout.CENTER);
		panel.add(buttons, BorderLayout.SOUTH);

		this.add(panel, BorderLayout.CENTER);
		validate();
	}
	
	private void showAlarm(Aufgabe aufgabe) {
		this.removeAll();
		JPanel panel = new JPanel(new BorderLayout());
		
		JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		textPanel.add(new JLabel("<html><b>Fertig!</b></html>"), BorderLayout.CENTER);
		
		try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResourceAsStream("Ring01.wav"))) {
			clip = AudioSystem.getClip();
			clip.open(audioInputStream);
			clip.loop(Clip.LOOP_CONTINUOUSLY);
			clip.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton weiter = new JButton("Weiter");
		weiter.addActionListener((e) -> {
			if (clip != null) {
				clip.stop();
				clip.close();
			}
			aufgabe.ok();
			List<String> fertigeLocks = session.pruefeLocks();
			if (fertigeLocks != null && !fertigeLocks.isEmpty()) {
				showFertigeLocks(fertigeLocks);
			} else {
				naechsteAufgabe(session.naechsteAufgabe());				
			}
		});
		buttons.add(weiter);
		
		panel.add(textPanel, BorderLayout.CENTER);
		panel.add(buttons, BorderLayout.SOUTH);
		this.add(panel, BorderLayout.CENTER);
		validate();
	}
}
