package com.gabrielavara.choiceplayer.playlist;

import com.gabrielavara.choiceplayer.dto.Mp3;
import com.gabrielavara.choiceplayer.messages.SelectionChangedMessage;
import com.gabrielavara.choiceplayer.messenger.Messenger;
import com.gabrielavara.choiceplayer.util.Opinion;
import com.gabrielavara.choiceplayer.views.PlaylistItemView;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import lombok.Setter;

import java.util.Optional;

public class PlaylistSelectionChangedListener implements ChangeListener<PlaylistItemView> {
    @Setter
    @SuppressWarnings("OptionalUsedAsFieldOrParameterType")
    private Optional<Opinion> opinion = Optional.empty();

    @Override
    public void changed(ObservableValue<? extends PlaylistItemView> observable, PlaylistItemView oldValue, PlaylistItemView newValue) {
        if (newValue == null) {
            return;
        }
        changed(oldValue == null ? null : oldValue.getMp3(), newValue.getMp3(), oldValue);
    }

    private void changed(Mp3 oldValue, Mp3 newValue, PlaylistItemView oldItemView) {
        Messenger.send(new SelectionChangedMessage(newValue, oldValue, oldItemView, true, opinion));
    }
}

@@ -417,30 +417,8 @@
 lue;
-%0Aimport lombok.Setter;
 %0A%0Aim
@@ -539,20 +539,8 @@
 %3E %7B%0A
-    @Setter%0A
     
@@ -652,16 +652,230 @@
 empty();
+%0A    private boolean opinionSet;%0A%0A    @SuppressWarnings(%22OptionalUsedAsFieldOrParameterType%22)%0A    public void setOpinion(Optional%3COpinion%3E opinion) %7B%0A        this.opinion = opinion;%0A        opinionSet = true;%0A    %7D
 %0A%0A    @O
@@ -1259,24 +1259,101 @@
 ItemView) %7B%0A
+        if (!opinionSet) %7B%0A            opinion = Optional.empty();%0A        %7D%0A
         Mess
@@ -1441,16 +1441,44 @@
 nion));%0A
+        opinionSet = false;%0A
     %7D%0A%7D%0A

Fix for changing selection with mouse