package game.player1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.haru.component.gui.MainPanel;

public class GameFrameOne extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private MainPanel mainPanel1;

	public static void main(String[] args) {
		GameFrameOne g = new GameFrameOne();
		g.gameStart(); // すぐにスタート
	}

	public GameFrameOne() {

		// MainPanel1
		mainPanel1 = new MainPanel();
		mainPanel1.setLayout(null);
		mainPanel1.setSize(500, 600);
		getContentPane().add(mainPanel1);

		// GameFrame
		setLayout(null);
		setSize(500, 600);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// メニューバーの追加
		JMenuBar menuBar = new JMenuBar();
		JMenu menuFile = new JMenu("ゲーム");
		JMenuItem menuStart = new JMenuItem("スタート");
		JMenuItem menuExit = new JMenuItem("終了");
		menuFile.setMnemonic('G');
		menuStart.setMnemonic('N');
		menuExit.setMnemonic('X');
		getRootPane().setJMenuBar(menuBar);
		menuBar.add(menuFile);
		menuFile.add(menuStart);
		menuFile.add(menuExit);
		menuStart.addActionListener(this);
		menuExit.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e) {
		JMenuItem item = (JMenuItem)e.getSource();
		System.out.println(item.getText());
		if (item.getText().equals("スタート")) {
			gameStart();
		} else if (item.getText() == "終了") {
//			gameEnd();
			System.exit(0);
		}
	}

	//--------------------------------------------------------------------------//
	private GameThreadOne thread;

	public void gameStart() {
		if (thread != null) {
			gameEnd();
		}
		thread = new GameThreadOne(mainPanel1);
		thread.start();
	}

	public void gameEnd() {
		thread.setEnd(true);
		try {
			thread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		thread = null;
	}

}
