package game.net.player2;

import game.constant.GameState;

import java.awt.Graphics;

import javax.swing.JFrame;

import com.haru.component.gui.MainPanel;
import com.haru.net.NetConstant;

public class GameServerFrame extends JFrame implements GameState, NetConstant {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {

		// 画面の準備
		GameServerFrame g = new GameServerFrame();

		// GameServer(プレイヤーとソケット)のマネージャー
		GameServerManager2 manager;
		manager = new GameServerManager2(
				new GameServer(g.mainPanel1, P_1), PORT1,
				new GameServer(g.mainPanel2, P_2), PORT2
				);




		// 接続準備
		while ( !manager.listen() )
			;

		// １試合１周
		while (true) {
			// 試合開始合図を送る
			if (!manager.isReady())
				break;


			// 開始
			Thread th = new Thread(manager);
			th.start();

			// 試合終了待ち (エラーの場合もここ)

			try {
				th.join();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			System.out.println("試合終了");


		}

	}





	private static final int PANEL_W = 500;
	private static final int PANEL_H = 600;

	private MainPanel mainPanel1;
	private MainPanel mainPanel2;


	public GameServerFrame() {

		// MainPanel1 (クライアント1)
		mainPanel1 = new MainPanel();
		mainPanel1.setLayout(null);
		mainPanel1.setSize(PANEL_W, PANEL_H);
		getContentPane().add(mainPanel1);

		// MainPanel2 (クライアント2)
		mainPanel2 = new MainPanel();
		mainPanel2.setLayout(null);
		mainPanel2.setSize(PANEL_W, PANEL_H);
		mainPanel2.setBounds(PANEL_W*1, 0, PANEL_W, PANEL_H);
		getContentPane().add(mainPanel2);



		// GameFrame
		setTitle("監視者");
		setSize(PANEL_W*2, PANEL_H);
		setLayout(null);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private int state;


//	private GameServerManager manager;
//	public boolean gameStartServer() {
//
//		if (manager == null) {
//			manager = new GameServerManager(
//					new GameServer(mainPanel1, P_1),
//					new GameServer(mainPanel2, P_2)
//					);
//
//		} else {
//
//			manager.setEnd(true);
//			try {
//				manager.join();
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//
//			// エラーになったsocketはnullなはず
//		}
//
//		state = ST_LISTEN1;
//		repaint();
//
//		manager.listen();
//
//
//		state = ST_GAME;
//		repaint();
//		manager.start();
//		return true;
//
//	}






	public void paint(Graphics g) {
		super.paint(g);

		switch (state) {
		case ST_LISTEN1:
			g.drawString("接続待機", 50, PANEL_H / 2);
			break;

		case ST_GAME:
			g.drawString("ゲーム中", 50, PANEL_H / 2);
			break;

		}
	}







}
