package game.net.test2;

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_port2 extends JFrame implements GameState, NetConstant {
	private static final long serialVersionUID = 1L;

	public static void main(String[] args) {

		// ServerManagerの立ち上げ,管理


		GameServerFrame_port2 g = new GameServerFrame_port2();

		GameServerManager manager;
		manager = new GameServerManager(
				new GameServer(g.mainPanel1, P_1),
				new GameServer(g.mainPanel2, P_2)
				);

		// １試合１周
		while (true) {
			manager.listen();
			manager.start();


			// 試合終了待ち (エラーの場合もここ)
			try {
				manager.join();
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}

			// 後処理
			manager.setEnd(true);
			try {
				manager.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// エラーになったsocketをdisconnect

		}

	}





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

	private MainPanel mainPanel1;
	private MainPanel mainPanel2;


	public GameServerFrame_port2() {

		// 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;

		}
	}







}
