package de.thdeg.greiner.superpangworld.game.managers;

import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.Bubble;
import de.thdeg.greiner.superpangworld.graphics.base.GameObject;
import de.thdeg.greiner.superpangworld.graphics.immovable.*;
import de.thdeg.greiner.superpangworld.graphics.moveable.*;

import java.awt.*;
import java.util.LinkedList;

/**
 * The manager handling the display and passive movement of the game objects.
 */
class GameObjectManager {

    private final LinkedList<Bubble> bubbles;
    private final LinkedList<Harpoon> harpoons;

    private final LevelProgressBar levelProgressBar;
    private final LevelLabel levelLabel;
    private final ScoreLabel scoreLabel;
    private final LivesLabel livesLabel;
    private final Background background;
    private final GameFrame frame;
    private final GameOverlay gameOverlay;

    private final PlayerObject playerObject;

    private final LinkedList<GameObject> gameObjects;

    /**
     * Create a manager handling the display and passive movement of the game objects.
     * @param gameView the GameView
     */
    public GameObjectManager(GameView gameView){
        gameView.setColorForBlockImage('k', Color.LIGHT_GRAY);

        bubbles = new LinkedList<>();
        harpoons = new LinkedList<>();
        gameObjects = new LinkedList<>();

        levelProgressBar = new LevelProgressBar(gameView);
        levelLabel = new LevelLabel(gameView);
        scoreLabel = new ScoreLabel(gameView);
        livesLabel = new LivesLabel(gameView);
        background = new Background(gameView);
        frame = new GameFrame(gameView);
        playerObject = new PlayerObject(gameView);
        gameOverlay = new GameOverlay(gameView);
    }

    /**
     * Clear all game object lists.
     */
    public void clearAll(){
        gameObjects.clear();
        bubbles.clear();
        harpoons.clear();
    }

    /**
     * Add the objects to the canvas and handle update their positions.
     */
    public void updateGameObjects(){
        gameObjects.clear();
        gameObjects.add(background);
        gameObjects.addAll(harpoons);
        gameObjects.add(frame);
        gameObjects.add(levelProgressBar);
        gameObjects.add(levelLabel);
        gameObjects.add(scoreLabel);
        gameObjects.add(livesLabel);
        gameObjects.add(playerObject);
        gameObjects.addAll(bubbles);
        gameObjects.add(gameOverlay);

        gameObjects.forEach(gameObject ->{
            gameObject.update();
            gameObject.addToCanvas();
        });
    }

    /**
     * Get the player object.
     *
     * @return the player
     */
    public PlayerObject getPlayerObject(){
        return playerObject;
    }

    /**
     * Get the bubbles.
     *
     * @return the bubbles
     */
    public LinkedList<Bubble> getBubbles() {
        return bubbles;
    }

    /**
     * Get the level label.
     *
     * @return the level label
     */
    public LevelLabel getLevelLabel(){
        return levelLabel;
    }

    /**
     * Get the lives label.
     *
     * @return the lives label
     */
    public LivesLabel getLivesLabel() {
        return livesLabel;
    }

    /**
     * Get the score label.
     *
     * @return the score label
     */
    public ScoreLabel getScoreLabel() {
        return scoreLabel;
    }

    /**
     * Get the level progress bar.
     *
     * @return the level progress bar
     */
    public LevelProgressBar getLevelProgressBar(){
        return levelProgressBar;
    }

    /**
     * Get the harpoons.
     *
     * @return the harpoons
     */
    public LinkedList<Harpoon> getHarpoons() {
        return harpoons;
    }

    /**
     * Get the game overlay.
     *
     * @return the game overlay
     */
    public GameOverlay getGameOverlay(){
        return gameOverlay;
    }
}
