package app.lsgui.gui.streamList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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

import app.lsgui.model.twitch.TwitchStreamModel;
import app.lsgui.service.twitch.TwitchProcessor;
import javafx.application.Platform;
import javafx.beans.property.ListProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;

public class StreamList extends AnchorPane {
    private static final Logger LOGGER = LoggerFactory.getLogger(StreamList.class);
    private static FXMLLoader loader;

    private ListProperty<TwitchStreamModel> streamProperty;

    @FXML
    private ListView<TwitchStreamModel> streamList;

    public StreamList() {
        LOGGER.debug("Construct StreamList");
        loader = new FXMLLoader(getClass().getResource("/StreamList.fxml"));
        getStylesheets().add(getClass().getResource("/ListView.css").toExternalForm());
        loader.setRoot(this);
        loader.setController(this);
        try {
            loader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
        streamProperty = new SimpleListProperty<TwitchStreamModel>();
        streamList.itemsProperty().bind(streamProperty);
        streamList.setCellFactory(new Callback<ListView<TwitchStreamModel>, ListCell<TwitchStreamModel>>() {
            @Override
            public ListCell<TwitchStreamModel> call(ListView<TwitchStreamModel> param) {
                return new StreamCell();
            }
        });
    }

    public void addStream(final String name) {
        LOGGER.debug("Add Stream to List");
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                List<TwitchStreamModel> streams = new ArrayList<TwitchStreamModel>(
                        getStreams().subList(0, getStreams().getSize()));
                streams.add(new TwitchStreamModel(name));
                getStreams().setValue(FXCollections.observableArrayList(streams));
            }
        });
    }

    public void removeSelectedStream() {
        if (!getListView().getSelectionModel().getSelectedItem().equals("")
                || !getListView().getSelectionModel().getSelectedItem().equals(null)) {
            TwitchStreamModel selectedStream = getListView().getSelectionModel().getSelectedItem();
            LOGGER.debug("Remove stream {} from list", selectedStream.getName());
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    List<TwitchStreamModel> streams = getStreams().subList(0, getStreams().getSize());
                    streams.remove(selectedStream);
                    getStreams().setValue(FXCollections.observableArrayList(streams));
                }
            });
        }
    }

    public void addFollowedStreams(final String username) {
        LOGGER.debug("Import followed Streams for user {}", username);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Set<String> set = TwitchProcessor.instance().getListOfFollowedStreams(username);
                List<TwitchStreamModel> list = new ArrayList<TwitchStreamModel>();
                for (String s : set) {
                    list.add(new TwitchStreamModel(s));
                }
                getStreams().setValue(FXCollections.observableArrayList(list));
            }
        });
    }

    /**
     * @return the streams
     */
    public ListProperty<TwitchStreamModel> getStreams() {
        return streamProperty;
    }

    /**
     * @param streams
     *            the streams to set
     */
    public void setStreams(List<TwitchStreamModel> streams) {
        streamProperty.set(FXCollections.observableList(streams));
    }

    /**
     * @return the streamList
     */
    public ListView<TwitchStreamModel> getListView() {
        return streamList;
    }

    /**
     * @param streamList
     *            the streamList to set
     */
    public void setListView(ListView<TwitchStreamModel> streamList) {
        this.streamList = streamList;
    }
}
@@ -80,63 +80,11 @@
 til.
-ArrayList;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Se
+Lis
 t;%0D%0A
@@ -173,119 +173,19 @@
 del.
-twitch.TwitchStreamModel;%0D%0Aimport app.lsgui.service.twitch.TwitchProcessor;%0D%0Aimport javafx.application.Platform
+StreamModel
 ;%0D%0Ai
@@ -716,38 +716,32 @@
 te ListProperty%3C
-Twitch
 StreamModel%3E str
@@ -780,38 +780,32 @@
 rivate ListView%3C
-Twitch
 StreamModel%3E str
@@ -1288,38 +1288,32 @@
 pleListProperty%3C
-Twitch
 StreamModel%3E();%0D
@@ -1419,38 +1419,32 @@
 llback%3CListView%3C
-Twitch
 StreamModel%3E, Li
@@ -1442,38 +1442,32 @@
 odel%3E, ListCell%3C
-Twitch
 StreamModel%3E%3E() 
@@ -1520,22 +1520,16 @@
 istCell%3C
-Twitch
 StreamMo
@@ -1539,38 +1539,32 @@
 %3E call(ListView%3C
-Twitch
 StreamModel%3E par
@@ -1653,1995 +1653,8 @@
 %0D%0A%0D%0A
-    public void addStream(final String name) %7B%0D%0A        LOGGER.debug(%22Add Stream to List%22);%0D%0A        Platform.runLater(new Runnable() %7B%0D%0A            @Override%0D%0A            public void run() %7B%0D%0A                List%3CTwitchStreamModel%3E streams = new ArrayList%3CTwitchStreamModel%3E(%0D%0A                        getStreams().subList(0, getStreams().getSize()));%0D%0A                streams.add(new TwitchStreamModel(name));%0D%0A                getStreams().setValue(FXCollections.observableArrayList(streams));%0D%0A            %7D%0D%0A        %7D);%0D%0A    %7D%0D%0A%0D%0A    public void removeSelectedStream() %7B%0D%0A        if (!getListView().getSelectionModel().getSelectedItem().equals(%22%22)%0D%0A                %7C%7C !getListView().getSelectionModel().getSelectedItem().equals(null)) %7B%0D%0A            TwitchStreamModel selectedStream = getListView().getSelectionModel().getSelectedItem();%0D%0A            LOGGER.debug(%22Remove stream %7B%7D from list%22, selectedStream.getName());%0D%0A            Platform.runLater(new Runnable() %7B%0D%0A                @Override%0D%0A                public void run() %7B%0D%0A                    List%3CTwitchStreamModel%3E streams = getStreams().subList(0, getStreams().getSize());%0D%0A                    streams.remove(selectedStream);%0D%0A                    getStreams().setValue(FXCollections.observableArrayList(streams));%0D%0A                %7D%0D%0A            %7D);%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    public void addFollowedStreams(final String username) %7B%0D%0A        LOGGER.debug(%22Import followed Streams for user %7B%7D%22, username);%0D%0A        Platform.runLater(new Runnable() %7B%0D%0A            @Override%0D%0A            public void run() %7B%0D%0A                Set%3CString%3E set = TwitchProcessor.instance().getListOfFollowedStreams(username);%0D%0A                List%3CTwitchStreamModel%3E list = new ArrayList%3CTwitchStreamModel%3E();%0D%0A                for (String s : set) %7B%0D%0A                    list.add(new TwitchStreamModel(s));%0D%0A                %7D%0D%0A                getStreams().setValue(FXCollections.observableArrayList(list));%0D%0A            %7D%0D%0A        %7D);%0D%0A    %7D%0D%0A%0D%0A
     
@@ -1719,22 +1719,16 @@
 roperty%3C
-Twitch
 StreamMo
@@ -1900,22 +1900,16 @@
 ms(List%3C
-Twitch
 StreamMo
@@ -2063,38 +2063,32 @@
 public ListView%3C
-Twitch
 StreamModel%3E get
@@ -2264,14 +2264,8 @@
 iew%3C
-Twitch
 Stre

change to streammodel and remove add, remove and import