package de.gymnew.sudoku.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import de.gymnew.sudoku.core.Solver;
import de.gymnew.sudoku.model.Sudoku;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	
	private MainFrameHandler handler;
	private SudokuPanel panel;
	private int scale;
	private Solver solver;
	
	public MainFrame() {
		
		/*==================================================*/
		// The Basics
		/*==================================================*/
		
		handler = new MainFrameHandler(this);
		scale = 5;
		
		setSize(800, 600); // TODO dynamic size
		setResizable(false);
		
		setTitle("SudokuSolver");
		
		panel = new SudokuPanel(this);
		panel.addMouseListener(handler);
		panel.setOpaque(true);
		setContentPane(panel);
		
		addWindowListener(new WindowAdapter(){
			@Override
			public void windowClosing(WindowEvent event) {
				handler.onMenuQuit();
			}
		});
		
		/*==================================================*/
		// The Menu
		/*==================================================*/
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		
		JMenu menuSudoku = new JMenu("Sudoku");
		menuBar.add(menuSudoku);
		
		JMenuItem itemLoad = new JMenuItem("Laden");
		itemLoad.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {
				handler.onMenuLoad();
			}
		});
		menuSudoku.add(itemLoad);
		
		JMenuItem itemSave = new JMenuItem("Speichern");
		itemSave.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {
				handler.onMenuSave();
			}
		});
		menuSudoku.add(itemSave);
		
		menuSudoku.addSeparator();
		
		JMenuItem itemCredits = new JMenuItem("Credits");
		itemCredits.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {
				handler.onMenuCredits();
			}
		});
		menuSudoku.add(itemCredits);
		
		menuSudoku.addSeparator();
		
		JMenuItem itemQuit = new JMenuItem("Beenden");
		itemQuit.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {
				handler.onMenuQuit();
			}
		});
		menuSudoku.add(itemQuit);
		
		
		JMenu menuSolve = new JMenu("L\u00F6sen");
		menuBar.add(menuSolve);
		
		JMenuItem itemSolverStart = new JMenuItem("L\u00F6ser starten");
		itemSolverStart.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {
				handler.onMenuStartSolver();
			}
		});
		menuSolve.add(itemSolverStart);
		
		JMenuItem itemSolverStop = new JMenuItem("L\u00F6ser stoppen");
		itemSolverStop.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {
				handler.onMenuStopSolver();
			}
		});
		menuSolve.add(itemSolverStop);
		
		menuSolve.addSeparator();
		
		JMenu menuAlgorithm = new JMenu("Algorithmus");
		menuSolve.add(menuAlgorithm);
		
	}

	public int getScale() {
		return scale;
	}

	public void setScale(int scaling) {
		this.scale = scaling;
		// TODO update frame size
		repaint();
	}
	
	public Sudoku getSudoku(){
		return panel.getSudoku();
	}
	
	public void setSudoku(Sudoku sudoku){
		panel.setSudoku(sudoku);
		panel.repaint();
	}

	public Solver getSolver() {
		return solver;
	}

	public void setSolver(Solver solver) {
		this.solver = solver;
	}
}

@@ -357,16 +357,66 @@
 .Sudoku;
+%0Aimport static de.gymnew.sudoku.gui.SudokuPanel.*;
 %0A%0A@Suppr
@@ -790,21 +790,8 @@
 s);%0A
-%09%09scale = 5;%0A
 %09%09%0A%09
@@ -813,29 +813,8 @@
 00);
- // TODO dynamic size
 %0A%09%09s
@@ -832,24 +832,42 @@
 (false);%0A%09%09%0A
+%09%09setScale(5);%0A%09%09%0A
 %09%09setTitle(%22
@@ -3238,19 +3238,17 @@
 int scal
-ing
+e
 ) %7B%0A%09%09th
@@ -3266,40 +3266,226 @@
 scal
-ing
+e
 ;%0A%09%09
-// TODO update frame size
+%0A%09%09int height = (OFFSET_TOP + OFFSET_BOTTOM + 9*FIELD_SIZE + 4*BLOCK_SEPARATOR_WIDTH) * scale + 50;%0A%09%09int width = (2*OFFSET_SIDE + 9*FIELD_SIZE + 4*BLOCK_SEPARATOR_WIDTH) * scale;%0A%09%09this.setSize(width, height);%0A%09%09
 %0A%09%09r

Make frame size dynamic