package uk.co.patrickhaston.mars;
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JButton;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.Insets;
//import javax.swing.JWindow;

public class MarsStartFrame extends JDialog  
{
  private MPanel startPanel = new MPanel();
  private JTextArea text1 = new JTextArea();
  private JButton jButton1 = new JButton();
  private GridLayout gridLayout1 = new GridLayout(1,2);
  private GridLayout buttonLayout = new GridLayout(6,1);
  private MPanel buttonPanel = new MPanel();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  private JButton jButton4 = new JButton();
  private JButton jButton5 = new JButton();
  private JButton jButton6 = new JButton();
  private MarsFrame theFrame;
  
  private int panelMode;
  
  public static int START = 1;
  public static int NEWGAME = 2;

  public MarsStartFrame(MarsFrame frame, int mode)
  {
    super(frame, true);
    theFrame = frame;
    try
    {
      panelMode = mode;
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

  }

  private void jbInit() throws Exception
  {
    this.setTitle("Welcome to Mars");
    startPanel.setLayout(gridLayout1);
    text1.setText("textArea");
    text1.setBackground(Color.black);
    text1.setForeground(Color.orange);
    text1.setFont(new Font("Arial", 0, 12));
    text1.setLineWrap(true);
    text1.setMargin(new Insets(5, 5, 5, 5));
    text1.setWrapStyleWord(true);
    buttonPanel.setLayout(buttonLayout);
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    jButton1.setText("Start a New Game");
    jButton1.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton1_actionPerformed(e);
        }
      });
    jButton2.setText("Load a Saved Game");
    jButton2.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton2_actionPerformed(e);
        }
      });
    jButton3.setText("Tour Guide");
    jButton3.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton3_actionPerformed(e);
        }
      });
    jButton4.setText("Marspedia");
    jButton4.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton4_actionPerformed(e);
        }
      });
    jButton5.setText("jButton5");
    jButton5.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton5_actionPerformed(e);
        }
      });
    jButton6.setText("jButton6");
    jButton6.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton6_actionPerformed(e);
        }
      });
    buttonPanel.add(jButton1, BorderLayout.CENTER);
    buttonPanel.add(jButton2, BorderLayout.CENTER);
    buttonPanel.add(jButton3, BorderLayout.CENTER);
    buttonPanel.add(jButton4, BorderLayout.CENTER);
    buttonPanel.add(jButton5, BorderLayout.CENTER);
    //buttonPanel.add(jButton6, null);
    startPanel.add(buttonPanel, null);
    startPanel.add(text1, BorderLayout.NORTH);
    this.getContentPane().add(startPanel, null);
    setButtons();
    this.setSize(new Dimension(300,300));

  }

  private void setButtons()
  {
    if (panelMode == START)
    {
    jButton1.setText("Start a New Game");
    jButton2.setText("Load a Saved Game");
    jButton3.setText("Tour Guide");
    jButton4.setText("Marspedia");
    jButton5.setText("Close");
    jButton6.setText("jButton6");
    String text = new String("Welcome to the game of Mars.");
    text = text + "  You can choose to start a new game or continue a game you have already begun.";
    text = text + "  For instructions, choose the Tour Guide, or browse the Marspedia for facts and trivia about Mars.";
    text1.setText(text);
    }
    if (panelMode == NEWGAME)
    {
    jButton1.setText("Easy Game");
    jButton2.setText("Medium Difficulty");
    jButton3.setText("Hard");
    jButton4.setText("Custom Game");
    jButton5.setText("Cancel");
    jButton6.setText("jButton6");
    String text = new String("Starting a new game.");
    text = text + "  Choose the level of difficulty you wish to play.";
    text = text + "  The harder the level, the longer and more involved the game will be.";
    text = text + "  You may prefer to make your own unique challenge by creating a custom game.";
    text1.setText(text);
    }
  }

  private void jButton1_actionPerformed(ActionEvent e)
  {
    if(panelMode == NEWGAME)
    {
      // start a new easy game
    theFrame.mars.LoadNewEasyGame();
    theFrame.createTreeNodes();
    this.hide();
    }
    if(panelMode == START)
    {
      panelMode = NEWGAME;
      setButtons();
      repaint();
    }
  }

  private void jButton2_actionPerformed(ActionEvent e)
  {
    if(panelMode == NEWGAME)
    {
      // start a new medium game
    theFrame.mars.LoadNewMediumGame();
    theFrame.createTreeNodes();
    this.hide();
    }
  }

  private void jButton3_actionPerformed(ActionEvent e)
  {
    if(panelMode == NEWGAME)
    {
      // start a new hard game
    theFrame.mars.LoadNewHardGame();
    theFrame.createTreeNodes();
    this.hide();
    }
  }

  private void jButton4_actionPerformed(ActionEvent e)
  {
    if(panelMode == NEWGAME)
    {
      // start a new easy game
    theFrame.mars.LoadNewEasyGame();
    theFrame.createTreeNodes();
    this.hide();
    }
  }

  private void jButton5_actionPerformed(ActionEvent e)
  {
    if(panelMode == START)
    {
      this.hide();
    }
    if(panelMode == NEWGAME)
    {
      panelMode = START;
      setButtons();
      repaint();
    }
  }

  private void jButton6_actionPerformed(ActionEvent e)
  {
  }

}
