//Name: TO DO: Fill this comment in
//Date: TO DO: Fill this comment in
//Purpose: TO DO: Fill this comment in
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
public class GridStarter extends Applet implements ActionListener
{
    //For screens
    Panel p_card;
    Panel card1, card2, card3, card4;
    CardLayout cdLayout = new CardLayout ();

    //Game screen's grid
    int row = 5;
    int col = 5;
    int b[] [] = {{2, 2, 0, 2, 2},
	    {2, 0, 0, 0, 2},
	    {0, 1, 1, 0, 0},
	    {2, 1, 1, 0, 2},
	    {2, 2, 0, 2, 2}};

    JButton pics[] = new JButton [row * col];
    int sqDimension = 65;
    String picStart = "c";
    String picFileType = ".png";
    JLabel turnpic;

    //Settings
    JTextField choice, name, first;

    //Formatting
    Color backgroundColour = Color.white;
    Color buttonColour = Color.lightGray;
    Color buttonText = Color.black;
    Color titleColour = Color.black;
    Font titleFont = new Font ("Arial", Font.PLAIN, 30);
    Font promptFont = new Font ("Arial", Font.PLAIN, 20);
    Dimension boardSquare = new Dimension (96, 96);

    public void init ()
    {
	p_card = new Panel ();
	p_card.setLayout (cdLayout);
	//TO DO: Bring back in when you are ready to work on other screens
	//opening ();
	//instructions ();
	//settings ();
	gameScreen ();
	resize (350, 500);
	setLayout (new BorderLayout ());
	add ("Center", p_card);
    }


    public void opening ()
    { //TO DO: Fill this comment in
	card1 = new Panel ();
	card1.setBackground (backgroundColour);
	JLabel title = new JLabel ("Welcome to Tic Tac Toe!");
	title.setFont (new Font ("Arial", Font.PLAIN, 30));
	title.setForeground (titleColour);
	JButton next = new JButton ("Enter");
	next.setPreferredSize (new Dimension (300, 50));
	next.setActionCommand ("s2");
	next.addActionListener (this);
	next.setBackground (buttonColour);
	next.setForeground (buttonText);
	card1.add (title);
	card1.add (next);
	p_card.add ("1", card1);
    }


    public void instructions ()
    { //TO DO: Fill this comment in
	card2 = new Panel ();
	card2.setBackground (backgroundColour);
	JLabel title = new JLabel ("The Instructions");
	title.setFont (titleFont);
	title.setForeground (titleColour);
	Panel p = new Panel ();
	JButton settings = new JButton ("Settings");
	settings.setActionCommand ("s3");
	settings.addActionListener (this);
	settings.setPreferredSize (new Dimension (150, 50));
	settings.setBackground (buttonColour);
	settings.setForeground (buttonText);
	JButton gameScreen = new JButton ("Game");
	gameScreen.setActionCommand ("s4");
	gameScreen.addActionListener (this);
	gameScreen.setPreferredSize (new Dimension (150, 50));
	gameScreen.setBackground (buttonColour);
	gameScreen.setForeground (buttonText);

	card2.add (title);
	p.add (settings);
	p.add (gameScreen);
	card2.add (p);
	p_card.add ("2", card2);
    }


    public void settings ()
    { //TO DO: Fill this comment in
	card3 = new Panel ();
	card3.setBackground (backgroundColour);
	JLabel title = new JLabel ("Choose your settings:");
	title.setFont (titleFont);
	title.setForeground (titleColour);
	Panel p = new Panel ();
	JLabel namePmt = new JLabel ("Your name:");
	namePmt.setFont (promptFont);
	name = new JTextField (10);
	name.setFont (promptFont);
	Panel p2 = new Panel ();
	JLabel choicePmt = new JLabel ("X or O:");
	choicePmt.setFont (promptFont);
	choice = new JTextField (4);
	choice.setFont (promptFont);
	Panel p3 = new Panel ();
	JLabel firstPmt = new JLabel ("Go first? y or n?");
	firstPmt.setFont (promptFont);
	first = new JTextField (4);
	first.setFont (promptFont);
	JButton entrance = new JButton ("To the game");
	entrance.setActionCommand ("s4");
	entrance.addActionListener (this);
	entrance.setPreferredSize (new Dimension (300, 50));
	entrance.setBackground (buttonColour);
	entrance.setForeground (buttonText);

	card3.add (title);
	p.add (namePmt);
	p.add (name);
	p2.add (choicePmt);
	p2.add (choice);
	p3.add (firstPmt);
	p3.add (first);
	card3.add (p);
	card3.add (p2);
	card3.add (p3);
	card3.add (entrance);
	p_card.add ("3", card3);
    }


    public void gameScreen ()
    { //TO DO: Fill this comment in
	card4 = new Panel ();
	card4.setBackground (backgroundColour);
	JLabel title = new JLabel ("Grid Game Title");
	title.setFont (titleFont);
	title.setForeground (titleColour);
	Panel p2 = new Panel ();
	turnpic = new JLabel (createImageIcon ("c4.png"));
	JLabel ins = new JLabel ("The current turn:");
	p2.add (ins);
	p2.add (turnpic);

	Panel grid = new Panel (new GridLayout (row, col));
	int m = 0;
	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		pics [m] = new JButton (createImageIcon (picStart + b [i] [j] + picFileType));
		pics [m].setPreferredSize (new Dimension (sqDimension, sqDimension));
		pics [m].setActionCommand (m + "");
		pics [m].addActionListener (this);
		pics [m].setBorder (null);
		grid.add (pics [m]);
		m++;
	    }
	}
	add (grid);
	//TO DO: Fill this comment in
	Panel p3 = new Panel ();
	JButton reset = new JButton ("Again");
	reset.addActionListener (this);
	reset.setActionCommand ("reset");
	reset.setPreferredSize (new Dimension (100, 30));
	reset.setBackground (buttonColour);
	reset.setForeground (buttonText);
	p3.add (reset);
	JButton instruct = new JButton ("Instructions");
	instruct.addActionListener (this);
	instruct.setActionCommand ("instruct");
	instruct.setPreferredSize (new Dimension (120, 30));
	instruct.setBackground (buttonColour);
	instruct.setForeground (buttonText);
	p3.add (instruct);
	JButton settings = new JButton ("Settings");
	settings.addActionListener (this);
	settings.setActionCommand ("settings");
	settings.setPreferredSize (new Dimension (100, 30));
	settings.setBackground (buttonColour);
	settings.setForeground (buttonText);
	p3.add (settings);

	card4.add (title);
	card4.add (p2);
	card4.add (grid);
	card4.add (p3);
	p_card.add ("4", card4);
    }



    public void actionPerformed (ActionEvent e)
    { //TO DO: Fill this comment in
	if (e.getActionCommand ().equals ("s1"))
	    cdLayout.show (p_card, "1");
	else if (e.getActionCommand ().equals ("s2"))
	    cdLayout.show (p_card, "2");
	else if (e.getActionCommand ().equals ("s3"))
	    cdLayout.show (p_card, "3");
	else if (e.getActionCommand ().equals ("s4"))
	    cdLayout.show (p_card, "4");

	//TO DO: Fill this comment in
	else if (e.getActionCommand ().equals ("reset"))
	{

	}
	else if (e.getActionCommand ().equals ("settings"))
	{
	    cdLayout.show (p_card, "3");
	}
	else if (e.getActionCommand ().equals ("instruct"))
	{
	    cdLayout.show (p_card, "2");
	}
	//TO DO: Fill this comment in
	else
	{
	    int n = Integer.parseInt (e.getActionCommand ());
	    int x = n / col;
	    int y = n % col;

	}
    }




    protected static ImageIcon createImageIcon (String path)
    {
	java.net.URL imgURL = GridStarter.class.getResource (path);
	if (imgURL != null)
	{
	    return new ImageIcon (imgURL);
	}


	else
	{
	    System.err.println ("Couldn't find file: " + path);
	    return null;
	}
    }
}


