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.*;
import java.awt.geom.Point2D;
import java.util.ArrayList;

/**
 * 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, ArrayList<CollidableGameObject> objectsToCollideWith) {
        super(gameView,objectsToCollideWith);

        size=1;
        speedInPixel = 2;

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

        this.hitBox.width = width;
        this.hitBox.height = height;
    }

    public HexagonalBubble(GameView gameView, ArrayList<CollidableGameObject> objectsToCollideWith, double size, double speedInPixel, Position position, boolean spawning, Point2D velocity){
        super(gameView, objectsToCollideWith, size, speedInPixel, position, spawning, velocity);

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

        this.hitBox.width = width;
        this.hitBox.height = height;
    }

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

    @Override
    public void addToCanvas() {
        gameView.addImageToCanvas("hexagon.png",position.x,position.y,size,rotation);
        gameView.addRectangleToCanvas(hitBox.x,hitBox.y,hitBox.width,hitBox.height,1,false, Color.GREEN);
    }

    @Override
    public void updateStatus() {

    }

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

    @Override
    public void destroy() {
        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, speedInPixel, positionLeft, false, velocityLeft);
            Bubble bubbleRight = new HexagonalBubble(gameView, new ArrayList<>(), size/2, speedInPixel, positionRight, false, velocityRight);

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