// The MIT License (MIT)
//
// Copyright (c) 2016 Tim Jones
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package io.github.jonestimd.finance.swing.transaction;

import java.util.List;

import io.github.jonestimd.finance.domain.UniqueId;
import io.github.jonestimd.finance.domain.transaction.TransactionSummary;
import io.github.jonestimd.finance.swing.event.DomainEventListener;
import io.github.jonestimd.finance.swing.event.DomainEventPublisher;
import io.github.jonestimd.swing.table.model.ColumnAdapter;
import io.github.jonestimd.swing.table.model.ValidatedBeanListTableModel;

public abstract class TransactionSummaryTableModel<T extends Comparable<? super T> & UniqueId<Long>, S extends TransactionSummary<T>> extends ValidatedBeanListTableModel<S> {
    @SuppressWarnings("FieldCanBeLocal") // need strong reference to avoid garbage collection
    private final DomainEventListener<Long, T> domainEventListener = event -> {
        if (event.isDelete()) {
            removeAll(getBeans().stream().filter(event::containsAttribute)::iterator);
        }
        else if (event.isReplace()) {
            long delta = getBeans().stream().filter(event::containsAttribute).mapToLong(this::resetCount).sum();
            updateCount(event.getReplacement(), delta);
        }
        else if (event.isAdd()) {
            for (T bean : event.getDomainObjects()) {
                addRow(newSummary(bean));
            }
        }
    };

    public TransactionSummaryTableModel(List<? extends ColumnAdapter<? super S, ?>> columnAdapters, Class<T> summaryAttributeClass,
                                        DomainEventPublisher domainEventPublisher) {
        super(columnAdapters);
        domainEventPublisher.register(summaryAttributeClass, domainEventListener);
    }

    protected abstract S newSummary(T bean);

    /**
     * @return summary count for the row.
     */
    private long resetCount(S summary) {
        long count = summary.getTransactionCount();
        summary.setTransactionCount(0);
        fireTableRowUpdated(summary);
        return count;
    }

    private void fireTableRowUpdated(S summary) {
        int row = indexOf(summary);
        fireTableRowsUpdated(row, row);
    }

    private void updateCount(T summaryAttribute, long delta) {
        S summary = getBeans().stream().filter(TransactionSummary.isSummary(summaryAttribute)).findFirst().get();
        summary.setTransactionCount(summary.getTransactionCount() + delta);
        fireTableRowUpdated(summary);
    }
}

@@ -1604,16 +1604,57 @@
 leModel;
+%0Aimport io.github.jonestimd.util.Streams;
 %0A%0Apublic
@@ -2054,35 +2054,23 @@
 All(
-getBeans().s
+S
 tream
-()
+s
 .filter(
 even
@@ -2057,32 +2057,44 @@
 (Streams.filter(
+getBeans(), 
 event::containsA
@@ -2106,18 +2106,8 @@
 ute)
-::iterator
 );%0A 

Fix error when deleting categories