package de.thdeg.greiner.superpangworld.graphics.moveable;

import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.*;
import de.thdeg.greiner.superpangworld.graphics.helper.HelperValues;

import java.util.ArrayList;

/**
 * A harpoon which can be fired upwards by the player.
 */
public class Harpoon extends CollidingGameObject implements MovingGameObject {

    /** The pixel string representing a harpoon */
    private final static String HARPOON =
                    "   B   \n"+
                    "  BBB  \n"+
                    " kkkkk \n"+
                    "k kkk k\n"+
                    "k kkk k\n";
    /** The pixel string representing the harpoon rope */
    private final String harpoonRope;
    /** The identifier for the sound timer */
    private int sound;

    /**
     * Create a harpoon with default values.
     *
     * @param gameView the {@link GameView} to display the bubble
     * @param objectsToCollideWith the list of {@link CollidableGameObject} the harpoon can collide with
     * @param position the position
     */
    public Harpoon(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith, Position position){
        super(gameView, objectsToCollideWith);
        this.position = position;
        speedInPixel = 2;
        size = 3;
        width = (int) (7 * size);
        height = (int) (5 * size);
        rotation = 0;
        this.hitBox.width = width - 2;
        this.hitBox.height = (int) (440 - position.y);
        sound = gameView.playSound("harpoon.wav",true);

        harpoonRope = generateRope();
    }

    /**
     * Generate the rope's pixel string.
     *
     * @return the pixel representation of the harpoon rope
     */
    private String generateRope(){
        int pre = 3;
        int post = 3;
        boolean right = true;
        StringBuilder stringBuilder = new StringBuilder();

        for(int i = 0; i< HelperValues.FRAME_WINDOW_HEIGHT; i++){
            stringBuilder.append(" ".repeat(pre) + "k" + " ".repeat(post)+"\n");
            if(right){
                pre++;
                post--;
            }else{
                pre--;
                post++;
            }
            if(post==0 || pre==0){
                right = !right;
            }
        }
        return stringBuilder.toString();
    }

    @Override
    protected void updateHitBoxPosition() {
        hitBox.x = (int) (position.x) + 1;
        hitBox.y = (int) (position.y);
        hitBox.height = (int) (440 - position.y);
    }

    @Override
    public void reactToCollision(CollidableGameObject otherObject) {}

    /**
     * Draws the harpoon onto the  {@link GameView}.
     */
    @Override
    public void addToCanvas(){
        gameView.addBlockImageToCanvas(HARPOON, getPosition().x, getPosition().y,size, rotation);
        gameView.addBlockImageToCanvas(harpoonRope, position.x + (width/2.0) - 7,position.y + height,2,0);
    }

    /**
     * Moves the harpoon a tick further upwards.
     */
    @Override
    public void updatePosition(){
        if(position.y > 8){
            getPosition().up(Math.min(speedInPixel, position.y-8));
        }else{
            gamePlayManager.destroy(this);
        }
    }

    /**
     * Deletes the harpoon if it hits the roof.
     */
    @Override
    public void updateStatus() {
    }

    /**
     * Stop the sound.
     */
    public void stopSound(){
        gameView.stopSound(sound);
    }
}
