import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.TableColumn;

public class LottozahlenGenerator implements ActionListener{
	
	JFrame frameApplikation;
	Container containerApplikation;
	JTextArea textAreaLottoReihen;
	JButton buttonNewLottozahlen;
	JPanel containerPanel;
	JPanel einstellungen;
	JTextField reihenAmount;
	JLabel reihenAmountLabel;
	JLabel labelReihen;
	JLabel labelStats;
	JTextArea stats;
	
	int anzahl[];
	JPanel center;

	int i;
	int lottozahl;
	ArrayList<Integer> lottozahlen;
	boolean zahlNochNichtVorhanden = true; 
	
	Font textArea = new Font("Courier", Font.PLAIN, 12);
	
	public LottozahlenGenerator(){
	    frameApplikation = new JFrame();
	    frameApplikation.setTitle("Lottozahlen Generator! (c) 2009 by Frank Roth");

	    containerApplikation = frameApplikation.getContentPane();
	    containerPanel = new JPanel(new BorderLayout());
	    einstellungen = new JPanel(new GridLayout(1,2));
	    center = new JPanel(new GridLayout(2,2));	
	    
	    buttonNewLottozahlen = new JButton("generiere neue Lottozahlen!");
	    buttonNewLottozahlen.addActionListener(this);
	    
	    reihenAmountLabel = new JLabel("Anzahl der Reihen: ");
	    reihenAmount = new JTextField("1");

	    labelReihen  = new JLabel("Generierte Reihen: ");
		textAreaLottoReihen = new JTextArea();
		textAreaLottoReihen.setFont(textArea);
	    
	    labelStats = new JLabel("Statistik: ");
	    stats = new JTextArea("");
	    stats.setFont(textArea);
	    
	    einstellungen.add(reihenAmountLabel);
	    einstellungen.add(reihenAmount);
	    
	    center.add(labelReihen);
	    center.add(new JScrollPane(textAreaLottoReihen));
	    center.add(labelStats);
	    center.add(new JScrollPane(stats));
	    
	    
	    containerPanel.add(einstellungen, BorderLayout.NORTH);
	    containerPanel.add(center, BorderLayout.CENTER);
	    containerPanel.add(buttonNewLottozahlen, BorderLayout.SOUTH);
	    
	    containerApplikation.add(containerPanel);

	    frameApplikation.setSize(600, 300);
	    frameApplikation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	    frameApplikation.setVisible(true);	    
	}
	
	public static void main(String[] args) {
		new LottozahlenGenerator();
	}
	
	public ArrayList<Integer> randomLottozahl(){
		
		lottozahlen = new ArrayList<Integer>();
		i = 1;
		while (i <= 6){
			zahlNochNichtVorhanden = true;
			lottozahl = (int)(Math.random()*49)+1;
			for (Integer zahl : lottozahlen){
				if (lottozahl == zahl){
					zahlNochNichtVorhanden = false;
				}
			}
			if (zahlNochNichtVorhanden != false){
				lottozahlen.add(lottozahl);
				i++;
			}
		}
		Collections.sort(lottozahlen);
		return lottozahlen;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonNewLottozahlen){
				int amount = 0;
				// Anzahl der Reihen generieren
				try {
					amount = Integer.parseInt(reihenAmount.getText());				
				} catch (NumberFormatException invalidValue) {
					// TODO: handle exception
					 JOptionPane.showMessageDialog(null,"Ungütlige Eingabe, bitte nur Zahlen eingeben!","Fehlermeldung", JOptionPane.ERROR_MESSAGE);
				}
	
				StringBuffer output = new StringBuffer();
	
				int x = 1;
				ArrayList<Integer> reihe = new ArrayList<Integer>();
				ArrayList<Integer> alleZahlen = new ArrayList<Integer>();
				
				while (x <= amount){
					reihe = this.randomLottozahl();
					for (Integer lottozahl : reihe){
						alleZahlen.add(lottozahl);
						output.append(lottozahl);
						if (lottozahl > 0 && lottozahl < 10){
							output.append("   ");
						}else{
							output.append("  ");
						}
					}
					output.append("\n");
					
					x++;
				}
				
				// Auswertung
				anzahl = new int[49];
				
				for(Integer zahl : alleZahlen){
					anzahl[zahl-1]++;
				}

				StringBuffer statsString = new StringBuffer();
				
				for(int i = 0; i <= anzahl.length - 1; i++)
				{
					statsString.append((i+1));
					statsString.append(": ");
					statsString.append(anzahl[i] + " mal");
					statsString.append("\n");
				}
				stats.setText(statsString.toString());
				textAreaLottoReihen.setText(output.toString());
			    frameApplikation.setVisible(true);	    
		}
	}
}
