/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.core.support.dispute;

import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.proto.persistable.PersistedDataHost;
import bisq.common.storage.Storage;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.util.List;
import java.util.stream.Stream;

import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ToString
/*
 * Holds a List of Dispute objects.
 *
 * Calls to the List are delegated because this class intercepts the add/remove calls so changes
 * can be saved to disc.
 */
public abstract class DisputeList<T extends PersistableEnvelope> implements PersistableEnvelope, PersistedDataHost {
    transient protected final Storage<T> storage;

    @Getter
    protected final ObservableList<Dispute> list = FXCollections.observableArrayList();

    public DisputeList(Storage<T> storage) {
        this.storage = storage;
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    // PROTO BUFFER
    ///////////////////////////////////////////////////////////////////////////////////////////

    protected DisputeList(Storage<T> storage, List<Dispute> list) {
        this.storage = storage;
        this.list.addAll(list);
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    // API
    ///////////////////////////////////////////////////////////////////////////////////////////

    public boolean add(Dispute dispute) {
        if (!list.contains(dispute)) {
            list.add(dispute);
            persist();
            return true;
        } else {
            return false;
        }
    }

    public boolean remove(Object dispute) {
        //noinspection SuspiciousMethodCalls
        boolean changed = list.remove(dispute);
        if (changed)
            persist();
        return changed;
    }

    public void persist() {
        storage.queueUpForSave();
    }

    public int size() {
        return list.size();
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    @SuppressWarnings({"SuspiciousMethodCalls"})
    public boolean contains(Object o) {
        return list.contains(o);
    }

    public Stream<Dispute> stream() {
        return list.stream();
    }
}

@@ -759,24 +759,20 @@
 sistable
-Envelope
+List
 ;%0Aimport
@@ -813,56 +813,62 @@
 sist
-edDataHost;%0Aimport bisq.common.storage.Storage;%0A
+ablePayload;%0A%0Aimport javafx.collections.FXCollections;
 %0Aimp
@@ -886,37 +886,42 @@
 collections.
-FXCollections
+ListChangeListener
 ;%0Aimport jav
@@ -980,62 +980,8 @@
 st;%0A
-import java.util.stream.Stream;%0A%0Aimport lombok.Getter;
 %0Aimp
@@ -1280,27 +1280,23 @@
 able
-Envelope%3E implement
+Payload%3E extend
 s Pe
@@ -1308,151 +1308,69 @@
 able
-Envelope, PersistedDataHost %7B%0A    transient protected final Storage%3CT%3E storage;%0A%0A    @Getter%0A    protected final ObservableList%3CDispute%3E list =
+List%3CT%3E %7B%0A    protected List%3CT%3E createList() %7B%0A        return
  FXC
@@ -1402,16 +1402,22 @@
 yList();
+%0A    %7D
 %0A%0A    pu
@@ -1437,1449 +1437,452 @@
 ist(
-Storage%3CT%3E storage) %7B%0A        this.storage = storage;%0A    %7D%0A%0A%0A    ///////////////////////////////////////////////////////////////////////////////////////////%0A    // PROTO BUFFER%0A    ///////////////////////////////////////////////////////////////////////////////////////////%0A%0A    protected DisputeList(Storage%3CT%3E storage, List%3CDispute%3E list) %7B%0A        this.storage = storage;%0A        this.list.addAll(list);%0A    %7D%0A%0A%0A    ///////////////////////////////////////////////////////////////////////////////////////////%0A    // API%0A    ///////////////////////////////////////////////////////////////////////////////////////////%0A%0A    public boolean add(Dispute dispute) %7B%0A        if (!list.contains(dispute)) %7B%0A            list.add(dispute);%0A            persist();%0A            return true;%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A    public boolean remove(Object dispute) %7B%0A        //noinspection SuspiciousMethodCalls%0A        boolean changed = list.remove(dispute);%0A        if (changed)%0A            persist();%0A        return changed;%0A    %7D%0A%0A    public void persist() %7B%0A        storage.queueUpForSave();%0A    %7D%0A%0A    public int size() %7B%0A        return list.size();%0A    %7D%0A%0A    public boolean isEmpty() %7B%0A        return list.isEmpty();%0A    %7D%0A%0A    @SuppressWarnings(%7B%22SuspiciousMethodCalls%22%7D)%0A    public boolean contains(Object o) %7B%0A        return list.contains(o);%0A    %7D%0A%0A    public Stream%3CDispute%3E stream() %7B%0A        return list.stream
+) %7B%0A    %7D%0A%0A    protected DisputeList(List%3CT%3E list) %7B%0A        super(list);%0A    %7D%0A%0A    public void addListener(ListChangeListener%3CT%3E listener) %7B%0A        ((ObservableList%3CT%3E) getList()).addListener(listener);%0A    %7D%0A%0A    public void removeListener(ListChangeListener%3CT%3E listener) %7B%0A        ((ObservableList%3CT%3E) getList()).removeListener(listener);%0A    %7D%0A%0A    public ObservableList%3CT%3E getObservableList() %7B%0A        return (ObservableList%3CT%3E) getList
 ();%0A

Use PersistableList as base class