package app.bvk.library;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import app.bvk.entity.Creature;
import app.bvk.utils.Utils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;

public class LibraryGui extends BorderPane
{
    private static final Logger LOGGER = LoggerFactory.getLogger(LibraryGui.class);

    @FXML
    private Button addLibraryEntryButton;
    @FXML
    private Text entryAmountText;
    @FXML
    private TextField filterTextField;
    @FXML
    private ListView<LibraryEntry> libraryList;

    private FXMLLoader loader;

    public LibraryGui()
    {
        this.loader = new FXMLLoader(this.getClass().getClassLoader().getResource("LibraryGui.fxml"));
        this.loader.setRoot(this);
        this.loader.setController(this);

        try
        {
            this.loader.load();
        }
        catch (final IOException e)
        {
            LOGGER.error("ERROR while loading library fxml", e);
        }
    }

    @FXML
    private void initialize()
    {
        this.addLibraryEntryButton.setOnAction(event -> Utils.newLibraryEntryWindow(this.getScene().getWindow()));
        this.addLibraryTextFieldFilter();
        CreatureLibrary.getInstance();
        final ObservableList<LibraryEntry> leList = FXCollections.observableArrayList();
        for (final Creature c : CreatureLibrary.getInstance().getCreatures())
        {
            leList.add(new LibraryEntry(c));
        }

        this.libraryList.setItems(leList);
    }

    private void addLibraryTextFieldFilter()
    {
        final FilteredList<LibraryEntry> fData = new FilteredList<>(this.libraryList.getItems(), p -> true);
        this.filterTextField.textProperty().addListener((obs, oldValue, newValue) ->
        {
            fData.setPredicate(t ->
            {
                final boolean isEmpty = newValue == null || newValue.isEmpty();
                final boolean nameMatch = t.getCreature().getName().get().toLowerCase().contains(newValue.toLowerCase());
                return isEmpty || nameMatch;
            });
            this.libraryList.setItems(fData);
        });
    }

}

@@ -46,16 +46,67 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -959,16 +959,155 @@
  loader;
+%0A    private final List%3CLibraryEntry%3E libraryEntries = new ArrayList%3C%3E();%0A    private ObservableList%3CLibraryEntry%3E libraryEntryDisplayList;
 %0A%0A    pu
@@ -1675,166 +1675,35 @@
     
-this.addLibraryTextFieldFilter();%0A        CreatureLibrary.getInstance();%0A        final ObservableList%3CLibraryEntry%3E leList = FXCollections.observableArrayList
+CreatureLibrary.getInstance
 ();%0A
@@ -1731,16 +1731,23 @@
 eature c
+reature
  : Creat
@@ -1809,22 +1809,35 @@
         
-leList
+this.libraryEntries
 .add(new
@@ -1851,16 +1851,23 @@
 yEntry(c
+reature
 ));%0A    
@@ -1876,49 +1876,243 @@
   %7D%0A
-%0A        this.libraryList.setItems(leList
+        this.libraryEntryDisplayList = FXCollections.observableArrayList(this.libraryEntries);%0A        this.libraryList.setItems(this.libraryEntryDisplayList);%0A        this.addLibraryTextFieldFilter();%0A%0A        this.setEntryAmountText(
 );%0A 
@@ -2210,16 +2210,23 @@
 Entry%3E f
+iltered
 Data = n
@@ -2273,16 +2273,24 @@
 ems(), p
+redicate
  -%3E true
@@ -2400,16 +2400,23 @@
        f
+iltered
 Data.set
@@ -2425,17 +2425,28 @@
 edicate(
-t
+libraryEntry
  -%3E%0A    
@@ -2561,29 +2561,42 @@
 nal 
-boolean nameMatch = t
+String creatureName = libraryEntry
 .get
@@ -2639,40 +2639,152 @@
 se()
-.contains(newValue.toLowerCase()
+;%0A                final String filterValue = newValue.toLowerCase();%0A                final boolean nameMatch = creatureName.contains(filterValue
 );%0A 
@@ -2882,16 +2882,23 @@
 tItems(f
+iltered
 Data);%0A 
@@ -2900,25 +2900,212 @@
 a);%0A        
-%7D
+    this.setEntryAmountText();%0A        %7D);%0A    %7D%0A%0A    private void setEntryAmountText()%0A    %7B%0A        this.entryAmountText.setText(String.format(%22# %25d%22, this.libraryList.getItems().size())
 );%0A    %7D%0A%0A%7D%0A

fix library filter