package io.mattw.youtube.commentsuite.fxml;

import io.mattw.youtube.commentsuite.ConfigData;
import io.mattw.youtube.commentsuite.ImageCache;
import io.mattw.youtube.commentsuite.db.GroupItem;
import io.mattw.youtube.commentsuite.util.BrowserUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Cursor;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

import java.io.IOException;

import static javafx.application.Platform.runLater;

public class MGMVGroupItemView extends HBox {

    private GroupItem groupItem;

    @FXML private ImageView icon;
    @FXML private Label title;
    @FXML private Label author;
    @FXML private Label type;

    private BrowserUtil browserUtil = new BrowserUtil();

    public MGMVGroupItemView(final GroupItem groupItem) {
        this.groupItem = groupItem;

        FXMLLoader loader = new FXMLLoader(getClass().getResource("MGMVGroupItemView.fxml"));
        loader.setController(this);
        loader.setRoot(this);
        try {
            loader.load();

            if (ConfigData.FAST_GROUP_ADD_THUMB_PLACEHOLDER.equals(groupItem.getThumbUrl())) {
                icon.setManaged(false);
                icon.setVisible(false);
            } else {
                icon.setImage(ImageCache.findOrGetImage(groupItem.getId(), groupItem.getThumbUrl()));
                icon.setManaged(true);
                icon.setVisible(true);
            }

            title.setText(groupItem.getDisplayName());
            author.setText(groupItem.getChannelTitle());
            type.setText(groupItem.getType().getDisplay());

            icon.setCursor(Cursor.HAND);
            icon.setOnMouseClicked(me -> browserUtil.open(groupItem.toYouTubeLink()));

            new Thread(() -> {
                Image image = ImageCache.findOrGetImage(groupItem.getId(), groupItem.getThumbUrl());
                runLater(() -> icon.setImage(image));
            }).start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public GroupItem getGroupItem() {
        return this.groupItem;
    }
}

@@ -1107,24 +1107,197 @@
 er.load();%0A%0A
+            title.setText(groupItem.getDisplayName());%0A            author.setText(groupItem.getChannelTitle());%0A            type.setText(groupItem.getType().getDisplay());%0A%0A
             
@@ -1508,352 +1508,66 @@
 .set
-Image(ImageCache.findOrGetImage(groupItem.getId(), groupItem.getThumbUrl()));%0A                icon.setManaged(true);%0A                icon.setVisible(true);%0A            %7D%0A%0A            title.setText(groupItem.getDisplayName());%0A            author.setText(groupItem.getChannelTitle());%0A            type.setText(groupItem.getType().getDisplay());%0A%0A
+Managed(true);%0A                icon.setVisible(true);%0A    
     
@@ -1603,16 +1603,20 @@
 .HAND);%0A
+    
         
@@ -1707,16 +1707,20 @@
         
+    
 new Thre
@@ -1730,16 +1730,20 @@
 () -%3E %7B%0A
+    
         
@@ -1851,16 +1851,20 @@
         
+    
 runLater
@@ -1905,16 +1905,20 @@
         
+    
 %7D).start
@@ -1920,16 +1920,30 @@
 start();
+%0A            %7D
 %0A%0A      

fix group manager freezing on loading lots of group item images