package net.liquidpineapple.pang.screens;

import lombok.Getter;

import net.liquidpineapple.pang.Application;
import net.liquidpineapple.pang.gui.Board;
import net.liquidpineapple.pang.gui.ScoreSystem;
import net.liquidpineapple.pang.gui.TimeSystem;
import net.liquidpineapple.pang.objects.Ball;

import net.liquidpineapple.pang.objects.GameObject;

import org.xml.sax.SAXException;

import java.awt.Graphics2D;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Class that represents a level.
 *
 * @author Govert de Gans
 * @date 2016-09-07
 */
public class LevelScreen extends Screen {

  @Getter
  private LinkedList<GameObject> hudObjectList;


  public LevelScreen() {
    super();
    hudObjectList = new LinkedList<GameObject>();
  }

  @Override
  /**
   * Update method for the level class.
   * Checks if the level is completed and if so, it loads a new level.
   * Updates all components in level when the level is not ended.
   */
  public void doUpdate() {
    boolean levelEnded = true;
    for (GameObject object : objectList) {
      if (object instanceof Ball) {
        levelEnded = false;
      }
    }

    if (levelEnded) {
      try {
        ScoreSystem.addScore(TimeSystem.getTime());
        nextLevel();
      } catch (ParserConfigurationException | SAXException ex) {
        ex.printStackTrace();
      }
    } else {
      new ArrayList<>(objectList).forEach(GameObject::doUpdate);
    }
  }

  /**
   * Attempst to load the new level (WIP).
   */
  private void nextLevel() throws ParserConfigurationException, SAXException {
    Board currentBoard = Application.getBoard();
    Screen newScreen;

    if (currentBoard.getLevels().hasNext()) {
      newScreen = (Screen) currentBoard.getLevels().next();
    } else {
      //set behaviour when all levels have been completed here.
      newScreen = new WinScreen();
    }
    currentBoard.changeScreen(newScreen);
  }

  @Override
  public void doDrawing(Graphics2D graphics2D, ImageObserver imageObserver) {
    super.doDrawing(graphics2D, imageObserver);
    for (GameObject hudObject : hudObjectList) {
      hudObject.doDrawing(graphics2D, imageObserver);
    }
  }
}

@@ -290,17 +290,16 @@
 s.Ball;%0A
-%0A
 import n
@@ -347,42 +347,8 @@
 t;%0A%0A
-import org.xml.sax.SAXException;%0A%0A
 impo
@@ -468,63 +468,8 @@
 ist;
-%0Aimport javax.xml.parsers.ParserConfigurationException;
 %0A%0A/*
@@ -989,450 +989,504 @@
     
-boolean levelEnded = true;%0A    for (GameObject object : objectList) %7B%0A      if (object instanceof Ball) %7B%0A        levelEnded = false;%0A      %7D%0A    %7D%0A%0A    if (levelEnded) %7B%0A      try %7B%0A        ScoreSystem.addScore(TimeSystem.getTime());%0A        nextLevel();%0A      %7D catch (ParserConfigurationException %7C SAXException ex) %7B%0A        ex.printStackTrace();%0A      %7D%0A    %7D else %7B%0A      new ArrayList%3C%3E(objectList).forEach(GameObject::doUpdate);%0A    %7D
+if (noBallsLeft()) %7B%0A        ScoreSystem.addScore(TimeSystem.getTime());%0A        nextLevel();%0A    %7D else %7B%0A      new ArrayList%3C%3E(objectList).forEach(GameObject::doUpdate);%0A    %7D%0A  %7D%0A%0A  /**%0A   * Checks if there are any balls left in the level.%0A   * @return true if there is at least one ball in the level and false otherwise.%0A   */%0A  private boolean noBallsLeft() %7B%0A    for (GameObject object : objectList) %7B%0A      if (object instanceof Ball) %7B%0A        return false;%0A      %7D%0A    %7D%0A    return true;
 %0A  %7D
@@ -1572,58 +1572,8 @@
 el()
- throws ParserConfigurationException, SAXException
  %7B%0A 

Refactor end of level detection