import java.util.Random;

public class LocalPlayer extends Player {


    /**
     * erstellt einen LocalPlayer und setzt myCoin random
     * @author Peer Ole Wachtel
     */
    public LocalPlayer(){
        super();
        Random random = new Random();
        this.myCoin = random.nextBoolean();
    }

    /**
     * receives a shot onto a point from the enemy
     * @param point the location to be shot
     * @author Luca Conte, Peer Ole Wachtel
     */
    @Override
    public synchronized void receiveShoot(Point point) {
        if (!this.enemy.myTurn) return;
        
        HitResponse hitResponse = board.getHitResponseOnPoint(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 -> GameController.getMainFrame().showPanelLoose("", this); //TODO Was halt bei victory passiert ist hier wurder verloheren
        }
        GameController.getMainFrame().refreshGameBoard();
    }

    /**
     * receives a hit response from the enemy as a response to a receiveShoot call
     * @param hitResponse the hitresponse
     * @author Peer Ole Wachtel
     */
    @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 -> GameController.getMainFrame().showPanelWin("", this); // TODO was halt beim victory passier ist hier wurde gewonnen
        }
        GameController.getMainFrame().refreshGameBoard();
    }

    /**
     * receives the enemies coin toss result
     * this method does nothing if the player has already received the enemies coin
     * it will also call `determineCoinToss`
     * @param coin the coin of the enemy player
     * @author Luca Conte
     */
    @Override
    public synchronized void receiveCoin(boolean coin) {
        if (!this.hasReceivedCoin) {
            this.hasReceivedCoin = true;
            this.determineCoinToss();
        }
    }

    /**
     * sends shot to enemy player.
     * should ONLY be called on HumanPlayer
     */
    @Override
    public void shoot(Point point){
        return;
    }

    /**
     * marks the player as ready, if all ships have been placed
     * @author Luca Conte
     */
    @Override
    public synchronized void ready() {
        for (Ship ship : this.board.getShips()) {
            if (!ship.isPlaced()) return;
        }
        super.ready();
    }
	
}