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

import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
import de.thdeg.greiner.superpangworld.graphics.base.CollidableGameObject;
import de.thdeg.greiner.superpangworld.graphics.base.Position;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * The rare hexagonal bubble.
 */
public class HexagonalBubble extends Bubble {

    /**
     * Create a hexagonal bubble with default values.
     *
     * @param gameView the {@link GameView} to display the bubble
     * @param objectsToCollideWith the list of {@link CollidableGameObject} the bubble can collide with
     */
    public HexagonalBubble(GameView gameView, List<CollidableGameObject> objectsToCollideWith) {
        super(gameView,objectsToCollideWith);

        size=1;
        speedInPixel = 2;

        width = (int) (128 * size);
        height = (int) (128 * size);

        this.hitBox.width = (int)(width - (width*0.1) );
        this.hitBox.height = (int)(width - (width*0.1) );
    }

    /**
     * Create a hexagonal bubble.
     *
     * @param gameView the game view
     * @param objectsToCollideWith the objects this bubble may collide with
     * @param size the size
     * @param position the position
     * @param spawning if the bubble is in the spawning state
     * @param velocity the velocity of the bubble
     */
    public HexagonalBubble(GameView gameView, List<CollidableGameObject> objectsToCollideWith, double size, Position position, boolean spawning, Point2D velocity){
        super(gameView, objectsToCollideWith, size, position, spawning, velocity);

        width = (int) (128 * size);
        height = (int) (128 * size);

        this.hitBox.width = (int)(width - (width*0.1) );
        this.hitBox.height = (int)(width - (width*0.1) );
    }

    @Override
    protected void updateHitBoxPosition() {
        hitBox.x = (int) (position.x + (width*0.05));
        hitBox.y = (int) (position.y + (width*0.05));
    }

    @Override
    public void addToCanvas() {
        gameView.addImageToCanvas("hexagon.png",position.x,position.y,size,rotation);
    }

    @Override
    public void updateStatus() {}

    @Override
    public void updatePosition() {
        super.updatePosition();
        rotation = (rotation + 2) % 360;
    }

    @Override
    public int getBurstScore() {
        switch((int)(size*1000)){
            case 1000:
                return 200;
            case 500:
                return 400;
            case 250:
                return 600;
            case 125:
                return 1200;
            default:
                throw new RuntimeException();
        }
    }

    @Override
    public void destroy() {
        gameView.playSound("burst.wav",false);
        if (size > 0.125) {
            Position positionLeft = new Position(position.x - (width/2.0/2.0), position.y);
            Position positionRight = new Position(position.x + width - (width/2.0/2.0), position.y);

            Point2D.Double velocityLeft = new Point2D.Double(Math.min(this.velocity.getX(),this.velocity.getX() * -1.0),Math.min(this.velocity.getY(),this.velocity.getY() * -1.0));
            Point2D.Double velocityRight = new Point2D.Double(Math.max(this.velocity.getX(),this.velocity.getX() * -1.0),Math.min(this.velocity.getY(),this.velocity.getY() * -1.0));

            Bubble bubbleLeft = new HexagonalBubble(gameView, new ArrayList<>(), size/2, positionLeft, false, velocityLeft);
            Bubble bubbleRight = new HexagonalBubble(gameView, new ArrayList<>(), size/2, positionRight, false, velocityRight);

            gamePlayManager.addBubbles(Arrays.asList(bubbleLeft, bubbleRight));
        }
    }
}
