# Sudoku Show
# 02/10/2006
# Partially based on demo code by Steve Lidie, April 1999, on the PerlTK.org website
# http://www.perltk.org/index.php?option=com_content&task=view&id=15&Itemid=28

use Tk;
use Tk::widgets qw/Dialog/;
use Tk::NumEntry;
use Text::ParseWords;
use strict;
use warnings;

our ($SudokuShow, $VERSION, @Sudoku, @Answer);
$SudokuShow = MainWindow->new;
# print "@ARGV\nor else not";
build_window(@ARGV); 
MainLoop; 

sub build_window {		# Where the action happens

    $VERSION = '0.0.4 Alpha'; 	# What version I'm on

    $SudokuShow->title("Sudoku Show"); 
    $SudokuShow->iconname('S'); 
    my $bmp = $SudokuShow->Photo(-file => "s.bmp", -format => 'bmp');		# A nifty little corner-icon thingy
    $SudokuShow->Icon(-image => $bmp);

	my $completeFile = shift;						# I haven't gotten the hang of passing arguments into the file yet
	my $incompleteFile = shift;
#	my $fileName = "sudoku.txt";			# So I've hard-coded the filename in.
	open (INPUTCF, $completeFile) or die "\nCan't open $completeFile : $!";
	open (INPUTICF, $incompleteFile) or die "\nCan't open $incompleteFile : $!";
	my @slurpCF = <INPUTCF>;					# I had to try it.
	my @slurpICF = <INPUTICF>;
	@Answer = nested_quotewords(" ", 0, @slurpCF);		# When I get a better handle on regular expressions, the input file can be more flexible than single space delimiting.
	@Sudoku = nested_quotewords(" ", 0, @slurpICF);
	close INPUTCF;
	close INPUTICF;
	check();
#	print "how many times do I go through?\n";
#	my $error = 0;
#	for my $i (0..8) { for my $j (0..8) { if ($Sudoku[$i][$j]==$Answer[$i][$j]) { $error = 1; } } }
#	if ($error == 1) { print "That's right!"; }
#	elsif ($error == 0) { print "Nope! Try again!"; }
#	@Sudoku = (	[1,2,3,4,5,6,7,8,9],		# A full Sudoku, for testing without inputs.
#				[4,5,6,7,8,9,1,2,3],
#				[7,8,9,1,2,3,4,5,6],
#				[2,3,4,5,6,7,8,9,1],
#				[5,6,7,8,9,1,2,3,4],
#				[8,9,1,2,3,4,5,6,7],
#				[3,4,5,6,7,8,9,1,2],
#				[6,7,8,9,1,2,3,4,5],
#				[9,1,2,3,4,5,6,7,8]);
#	@Sudoku = (	[11,12,13],		# A small test grid to get the GUI working
#				[14,15,16],
#				[17,18,19]);
	for my $i (0..$#Sudoku) {		# Once I've got things working, I may split this into 9 pieces to get better spacing.
		for my $j (0..$#{$Sudoku[$i]}) {
#			print " $Sudoku[$i][$j]";
#			if ($Sudoku[$i][$j] == ".") { $Sudoku[$i][$j] = "0"; }
			my $box = build_box($Sudoku[$i][$j],$i,$j);
#			$box->place(-x => $i, -y => $j);
		}
	}
    my $menubar = build_menubar(); 
#    my $frame = $SudokuShow->Frame(qw/-width 500 -height 500 -background cyan/)->pack; 

}

sub build_box {		# Creates a label widget for the number
	my $num = shift;
	my $yPos = shift;
	my $xPos = shift;
	my $min = 0;
	my $max = 9;
	if ($num != 0) { $min = $max = $num; }
#	$xPos = $xPos*10;			# For when I tried place
#	$yPos = $yPos*10;
#	print "[$xPos][$yPos]=$num ";
#	my $box = $SudokuShow->NumEntry(-value => $num,
#									-minvalue => $min,
#									-maxvalue => $max,);
	my $box = $SudokuShow->Label(	-text => $num,
									-background => 'white',
									-relief => 'groove',	# The border of the boxes
									-height => 5,
									-width => 10);	# Size and color of the boxes
	if ($num == 0) {
		my $change_dialog = $SudokuShow->Dialog(-title   => 'Edit', 
												-text    => 'Select a new value for this square.',	# I sure hope so.
												-default_button => '0',
												-buttons => [qw/1 2 3 4 5 6 7 8 9/]); 
		$box = $SudokuShow->Button(	-text => "",
									-background => 'white',
									-relief => 'groove',	# The border of the boxes
									-height => 5,
									-width => 10,
#									-command => [$box->configure(-text => '1')]);
									-command => [$Sudoku[$yPos][$xPos] = $change_dialog => 'Show']);
	}
#	$box->pack(-fill => 'both');
#	$box->pack;					# Pack just doesn't do it
	$box->grid(	-column => $xPos,	# Positional Data
				-row => $yPos,
				-ipadx => 1,		# I'm trying to put a border on the boxes here, but groove did it better
				-ipady => 1);
#	$box->place(-in => $SudokuShow,
#				-x => $xPos);		# Positional data
#				-y => $yPos);
#	$box->grid(-column => 1
#			-row => 1);
	$box;						# To return something, and because I was trying to map it outside the subroutine for a while.
}

sub build_menubar {		# It builds the menu bar.  It will be important later, and it was useful practice.

    my $menubar = $SudokuShow->Menu; 
    $SudokuShow->configure(-menu => $menubar); 
    my $file = $menubar->cascade(-label => '~File', -tearoff => 0);		# I don't care for the tearoff option.  It's ugly.
    my $help = $menubar->cascade(-label => '~Help', -tearoff => 0); 
	
	$file->command(-label => 'Generate');
	$file->command(-label => 'Check', -command => \&SudokuShow::check);
	$file->command(-label => 'Quit', -command => \&exit);	# In case somebody doesn't want to use the corner x.

    $help->command(-label => 'Version');	# Version info
    $help->separator; 
    $help->command(-label => 'About');		# What it does

    my $version_dialog =   $SudokuShow->Dialog(-title   => 'SudokuShow Version', 
                                -text    => "SudokuShow\n\nVersion $VERSION",		#Reads the version info and displays it
                                -buttons => ['OK'], 
                                -bitmap  => 'info'); 
    my $about_dialog = $SudokuShow->Dialog(-title   => 'About SudokuShow', 
                                -text    => 'This program will someday be able to show sudoku puzzles.',	# I sure hope so.
                                -buttons => ['OK']); 
    my $check_dialog = $SudokuShow->Dialog(-title   => 'Answer', 
                                -text    => 'What is the answer?',	# I sure hope so.
                                -buttons => ['OK']); 
    my $menu = $help->cget('-menu'); 
    $menu->entryconfigure('Version', -command => [$version_dialog   => 'Show']); 
    $menu->entryconfigure('About',   -command => [$about_dialog => 'Show']); 
#	$menu->entryconfigure('Checker', -command => [$check_dialog => 'Show']); 

    $menubar;
}

sub check {
	my $error = 0;
	for my $i (0..8) { for my $j (0..8) { if ($Sudoku[$i][$j]==$Answer[$i][$j]) { $error = 1; } } }
	if ($error == 0) { print "That's right!"; }
	elsif ($error == 1) { print "Nope! Try again!"; }
	if ($error == 1) {
		my $check_dialog = $SudokuShow->Dialog(	-title   => 'Answer', 
												-text    => 'This isn\'t the correct answer.',	# I sure hope so.
												-buttons => ['OK']); 
		$check_dialog => 'Show';
	}
}