/*
        FileName: prologGUI.java
        Name: Todd Klasik
        Class: Artificial Intelligence
        Date: December 4, 2005

        This program creates a GUI that will then attempt to communicate into a prolog engine
*/

import javax.swing.*; //we gonna use da window creation stuff
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class SudokuGUI extends JFrame {
        
        static boolean arguement;
        private JButton ButtonArr[][] = new JButton[9][9];         //set up the buttons
        private JButton okButton;
        
        ArrayHandler handleArray[][] = new ArrayHandler[9][9];    //event handlers (lots!)
        okButtonHandler okHandler;
        
        
        public SudokuGUI() {
                int Y,X;
                setTitle("Sudoku Puzzle Builder");      //da title

		//                Container puzzlePad = getContentPane();      //make a pane called...puzzlePad!
		//                Container puzzlePad = new Container();      //make a pane called...puzzlePad!
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
                JPanel puzzlePad = new JPanel();      //make a pane called...puzzlePad!
                puzzlePad.setLayout(new GridLayout(9,9));    //its a 9x9 grid
                
                for(int i = 0; i < 9 ; i++) {   //constructs the entier gui by array defintions
                        for(int j = 0; j < 9 ; j++) {
                                ButtonArr[i][j] = new JButton("");
                                handleArray[i][j] = new ArrayHandler(i,j);
                                ButtonArr[i][j].addActionListener(handleArray[i][j]);
                                puzzlePad.add(ButtonArr[i][j]);
                        }
                }
                
                okButton = new JButton("Save to sudoku_puzzle.pl");    //button for converstion and handlers
		//		okButton.setPreferredSize(new Dimension(100,20));
                okHandler = new okButtonHandler();
                okButton.addActionListener(okHandler);

		mainPanel.add(puzzlePad,"Center");
		mainPanel.add(okButton,"South");
		getContentPane().add(mainPanel);
		//                puzzlePad.add(okButton);

                setSize(500, 500);      //size it so it doesnt look to big/small
                setVisible(true);       //make it visible
                setDefaultCloseOperation(EXIT_ON_CLOSE);        //set that up too
        }
        
        public class ArrayHandler implements ActionListener {    //this handles all button actions
                int corY,corX;
                
                public ArrayHandler(int a, int b) {     //log the location of each button as its coordinates
                        corY = a;
                        corX = b;
                }
                
                public void actionPerformed(ActionEvent e) {    //if its blank, increment it to 10, then reset
                        if(arguement) {
                                QueryBox box = new QueryBox(ButtonArr[corY][corX]);
                        } else {
                                if(ButtonArr[corY][corX].getText().compareTo("") == 0) {
                                        ButtonArr[corY][corX].setText("1");
                                } else {
                                        ButtonArr[corY][corX].setText((Integer.parseInt(ButtonArr[corY][corX].getText()) + 1) + "");
                                }
                                
                                if(ButtonArr[corY][corX].getText().compareTo("10") == 0) {
                                        ButtonArr[corY][corX].setText("");
                                }
                        }
                }
        }
        
        public class okButtonHandler implements ActionListener {
                public void actionPerformed(ActionEvent e) {    //make the file
                        try {
                                File prologFile = new File("sudoku_puzzle.pl");       //opens the file.
                                BufferedWriter out = new BufferedWriter(new FileWriter(prologFile));
                                
                                out.write("Copy the following lines into the prolog_puzzles file, also add puzzle(9,7). to the beginning ofhe file.");
                                out.newLine();out.newLine();
                                
                                out.write("puzzle(9,7) :- %%moocow!");
                                out.newLine();
                                
                                for(int i = 0;i<9;i++) {        //writes out all the squares
                                        for(int j = 0;j<9;j++) {
                                                if(ButtonArr[i][j].getText().compareTo("") != 0)
                                                        out.write("assertz(square(" + currentPosition(i,j) + "," + ButtonArr[i][j].getText() + ")),");
                                        }
                                        out.newLine();
                                }
                                
                                out.write("nl.");       //ends the prolog statement 
                                out.close();
                                System.out.println("Values exported to sudoku_puzzle.pl");
                                System.exit(0);
                        } catch (Exception exce) {
                                System.out.println("Your ballsed now arent you?");
                        }
                }
        }
        
        public int currentPosition(int i, int j) {
                return i*9 + j;
        }
        
        public static void main(String argv[]) {
                
                if(argv.length == 0) 
                        arguement = false;
                else
                        if(argv[0].compareTo("1") == 0)
                                arguement = true;
                        else
                                arguement = false;
                SudokuGUI w00t = new SudokuGUI();
        }
}
