//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 REPLACE extends Applet implements ActionListener
{
    //For screens
    Panel p_card;
    Panel card1, card2, card3, card4;
    CardLayout cdLayout = new CardLayout ();
    JTextField txtName, txtName2;

    //Game screen
    JLabel turnPic;
    int turn = 1;
    //grid
    int row = 8;
    int col = 8;
    JButton a[] = new JButton [row * col];
    int b[] [] = new int [row] [col];
    int levelCount = 10;

    //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 (40, 40);

    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 ________!");
	title.setFont (new Font ("Arial", Font.PLAIN, 30));
	title.setForeground (titleColour);
	JButton next = new JButton ("Enter");
	next.setPreferredSize (new Dimension (300, 50));
	next.setActionCommand ("card2");
	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 ("card3");
	settings.addActionListener (this);
	settings.setPreferredSize (new Dimension (150, 50));
	settings.setBackground (buttonColour);
	settings.setForeground (buttonText);
	JButton gameScreen = new JButton ("Game");
	gameScreen.setActionCommand ("card4");
	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 ("Player 1 Name:");
	namePmt.setFont (promptFont);
	txtName = new JTextField (10);
	txtName.setFont (promptFont);
	Panel p2 = new Panel ();
	JLabel namePmt2 = new JLabel ("Player 2 Name:");
	namePmt2.setFont (promptFont);
	txtName2 = new JTextField (10);
	txtName2.setFont (promptFont);
	JButton entrance = new JButton ("To the game");
	entrance.setActionCommand ("card4");
	entrance.addActionListener (this);
	entrance.setPreferredSize (new Dimension (300, 50));
	entrance.setBackground (buttonColour);
	entrance.setForeground (buttonText);

	JLabel lvlPmt = new JLabel ("Choose Level:");
	lvlPmt.setFont (promptFont);
	Panel lvlPanel = new Panel (new GridLayout (2, levelCount / 2));
	JButton lvl[] = new JButton [levelCount];
	for (int i = 0 ; i < levelCount ; i++)
	{
	    lvl [i] = new JButton ("" + (i + 1));
	    lvl [i].setBackground (backgroundColour);
	    lvl [i].setPreferredSize (new Dimension (60, 40));
	    lvl [i].addActionListener (this);
	    lvl [i].setFont (promptFont);
	    lvl [i].setActionCommand ("lvl" + (i + 1));
	    lvlPanel.add (lvl [i]);
	}

	card3.add (title);
	p.add (namePmt);
	p.add (txtName);
	p2.add (namePmt2);
	p2.add (txtName2);
	card3.add (p);
	card3.add (p2);
	card3.add (lvlPmt);
	card3.add (lvlPanel);
	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 ("______Title______");
	title.setFont (titleFont);
	title.setForeground (titleColour);

	Panel p = new Panel ();
	JLabel curturn = new JLabel ("Current Turn:");
	curturn.setFont (promptFont);
	turnPic = new JLabel (createImageIcon ("1.png"));
	p.add (curturn);
	p.add (turnPic);

	//Set up grid
	Panel p2 = new Panel (new GridLayout (row, col));
	int m = 0;
	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		a [m] = new JButton (createImageIcon (b [i] [j] + ".png"));
		a [m].setBackground (backgroundColour);
		a [m].setPreferredSize (boardSquare);
		a [m].addActionListener (this);
		a [m].setBorder (null);
		a [m].setActionCommand ("" + m);
		p2.add (a [m]);
		m++;
	    }
	}


	//TO DO: Fill this comment in
	Panel p3 = new Panel ();
	JButton reset = new JButton ("Reset");
	reset.addActionListener (this);
	reset.setActionCommand ("reset");
	reset.setPreferredSize (new Dimension (68, 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 (102, 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 (82, 30));
	settings.setBackground (buttonColour);
	settings.setForeground (buttonText);
	p3.add (settings);
	JButton next = new JButton ("Next");
	next.addActionListener (this);
	next.setActionCommand ("next");
	next.setPreferredSize (new Dimension (60, 30));
	next.setBackground (buttonColour);
	next.setForeground (buttonText);
	p3.add (next);

	card4.add (title);
	card4.add (p);
	card4.add (p2);
	card4.add (p3);
	p_card.add ("4", card4);
    }


    public void redraw ()
    {
	int m = 0;
	for (int i = 0 ; i < row ; i++)
	{
	    for (int j = 0 ; j < col ; j++)
	    {
		a [m].setIcon (createImageIcon (b [i] [j] + ".png"));
		m++;
	    }
	}
    }




    public void actionPerformed (ActionEvent e)
    { //TO DO: Fill this comment in
	if (e.getActionCommand ().equals ("card1"))
	    cdLayout.show (p_card, "1");
	else if (e.getActionCommand ().equals ("card2"))
	    cdLayout.show (p_card, "2");
	else if (e.getActionCommand ().equals ("card3"))
	    cdLayout.show (p_card, "3");
	else if (e.getActionCommand ().equals ("card4"))
	    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;

	    redraw ();

	}
    }


    protected static ImageIcon createImageIcon (String path)
    {
	java.net.URL imgURL = REPLACE.class.getResource (path);
	if (imgURL != null)
	{
	    return new ImageIcon (imgURL);
	}


	else
	{
	    System.err.println ("Couldn't find file: " + path);
	    return null;
	}
    }
}


