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

import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.GameObject;
import de.thdeg.greiner.superpangworld.graphics.base.MovementPatterns;
import de.thdeg.greiner.superpangworld.graphics.base.MovingGameObject;
import de.thdeg.greiner.superpangworld.graphics.base.Position;

import java.awt.*;
import java.util.*;
import java.util.List;

/** Ball with random movement. */
public class RandomBall extends GameObject implements MovingGameObject {
    private final Random random;
    private ArrayList<Position> movementPattern;
    private int movementPatternIndex;
    private Position targetPosition;
    private MovementPatterns movementPatterns;


    /**
     * Creates the GameObject with the GameView to be displayed on.
     *
     * @param gameView Window to show the GameObject on.
     */
    public RandomBall(GameView gameView) {
        super(gameView);
        this.random = new Random();
        this.size = 50;
        this.speedInPixel = 4;

        movementPatterns = new MovementPatterns();

        movementPattern = movementPatterns.getPattern("Centered");
        movementPatternIndex = 0;
        targetPosition = movementPattern.get(0);
    }

    @Override
    public void updatePosition() {
        double distance = position.distance(targetPosition);
        if (distance >= speedInPixel) {
            position.right((targetPosition.x - position.x) / distance * speedInPixel);
            position.down((targetPosition.y - position.y) / distance * speedInPixel);
        } else {
            if(movementPatternIndex < movementPattern.size()-1){
                movementPatternIndex++;
            }else{
                movementPattern = movementPatterns.getPattern("Centered");
                movementPatternIndex = 0;
            }
            targetPosition = movementPattern.get(movementPatternIndex);
        }
    }


    @Override
    protected void updateStatus() {

    }

   /** Set position to aim at */
    public void setRandomTargetPosition() {
        targetPosition.x = random.nextInt(GameView.WIDTH);
        targetPosition.y = random.nextInt(GameView.HEIGHT);
    }

    @Override
    public void addToCanvas() {
        gameView.addOvalToCanvas(position.x, position.y, size, size, 2, true, Color.YELLOW);
        gameView.addOvalToCanvas(targetPosition.x, targetPosition.y, size, size, 2, false, Color.WHITE);
    }
}
