import java.util.Random;

public class LocalPlayer extends Player {

    public LocalPlayer(){
        super();
        Random random = new Random();
        this.myCoin = random.nextBoolean();
    }

    @Override
    public synchronized void receiveShoot(Point point) {
        HitResponse hitResponse = board.getHitResponsOnPoint(point);
        if (!(hitResponse == null)){
            enemy.receiveHit(hitResponse);

        } else {
            hitResponse = this.board.hit(point);
            enemy.receiveHit(hitResponse);
        }

        switch (hitResponse.getType()) {
            case HIT, SUNK -> this.myTurn = false;
            case MISS -> this.myTurn = true;
            case VICTORY -> System.out.println("Game Over"); //TODO Was halt bei victory passiert ist hier wurder verloheren
        }
    }

    @Override
    public synchronized void receiveHit(HitResponse hitResponse) {
        enemy.board.addHits(hitResponse);
        switch (hitResponse.getType()) {
            case HIT, SUNK -> this.myTurn = true;
            case MISS -> this.myTurn = false;
            case VICTORY -> System.out.println("Win"); // TODO was halt beim victory passier ist hier wurde gewonnen
        }
    }

    @Override
    public synchronized void receiveCoin(boolean coin) {
        if (!this.haseReceivedCoin) {
            boolean result = coin ^ this.myCoin; // XOR
            this.myTurn = result == this.isServer;
            this.haseReceivedCoin = true;
        }
    }

    public void sendCoin() {
        enemy.receiveCoin(this.myCoin);
    }

    @Override
    public void shoot(Point point){
        this.myTurn = false;
        enemy.receiveShoot(point);
    }
	
}