package li.seiji.minichess.player;

import li.seiji.minichess.Player;
import li.seiji.minichess.Board;
import li.seiji.minichess.imcs.Client;
import li.seiji.minichess.move.Move;

import java.io.IOException;

public class IMCSPlayer implements IPlayer {

    private Client imcs;
    private String gameId = null;
    private Player player = Player.NONE;

    public IMCSPlayer(String domain, int port, String username, String password) throws IOException {
        imcs = new Client(domain, Integer.toString(port), username, password);
    }

    public void setGameConfig(String gameId, Player player) {
        this.gameId = gameId;
        this.player = player;
    }


    @Override
    public void start() {
        if(gameId == null || player == Player.NONE)
            throw new RuntimeException("Accepting gameId and player has not been set.");
        try {
            imcs.accept(gameId, player.toString().charAt(0));
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Move getMove(Board state) {
        try {
            return new Move(imcs.getMove());
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void setMove(Board state, Move move) {
        try {
            imcs.sendMove(move.toString());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}

@@ -257,26 +257,164 @@
    p
-rivate Client imcs
+ublic enum IMCSGameMode %7B%0A        NONE,%0A        ACCEPT,%0A        OFFER%0A    %7D%0A%0A    private Client imcs;%0A%0A    private IMCSGameMode gameMode = IMCSGameMode.NONE
 ;%0A  
@@ -698,18 +698,160 @@
  set
-GameConfig
+OfferGame(Player ownPlayer) throws IOException %7B%0A        player = ownPlayer;%0A        gameMode = IMCSGameMode.OFFER;%0A    %7D%0A%0A    public void setAcceptGame
 (Str
@@ -865,25 +865,28 @@
 eId, Player 
-p
+ownP
 layer) %7B%0A   
@@ -934,24 +934,67 @@
 layer = 
-p
+ownP
 layer;%0A
+        gameMode = IMCSGameMode.ACCEPT;%0A
     %7D%0A%0A%0A
@@ -1043,16 +1043,52 @@
       if
+(gameMode == IMCSGameMode.ACCEPT && 
 (gameId 
@@ -1120,16 +1120,17 @@
 er.NONE)
+)
 %0A       
@@ -1211,38 +1211,328 @@
 set.%22);%0A        
-try %7B%0A
+if(gameMode == IMCSGameMode.OFFER && player == Player.NONE)%0A            throw new RuntimeException(%22Own player for game offering has not been set.%22);%0A%0A        try %7B%0A            if(gameMode == IMCSGameMode.OFFER) %7B%0A                imcs.offer(player.toString().charAt(0));%0A            %7D else %7B%0A    
             imcs
@@ -1569,32 +1569,46 @@
 g().charAt(0));%0A
+            %7D%0A
         %7D catch 

Implement Game offering in IMCSPlayer