package org.hanuna.gitalk.graph.new_mutable;

import org.hanuna.gitalk.common.Executor;
import org.hanuna.gitalk.common.compressedlist.Replace;
import org.hanuna.gitalk.graph.NewGraph;
import org.hanuna.gitalk.graph.elements.NodeRow;
import org.hanuna.gitalk.graph.new_mutable.elements.MutableNodeRow;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * @author erokhins
 */
public class MutableGraph implements NewGraph {
    private final EdgeController edgeController = new EdgeController();
    private final ElementVisibilityController visibilityController = new ElementVisibilityController();
    private final List<MutableNodeRow> allRows = new ArrayList<MutableNodeRow>();
    private final List<Integer> indexVisibleRows = new ArrayList<Integer>();

    private final List<Executor<Replace>> listenerList = new ArrayList<Executor<Replace>>();

    @NotNull
    public EdgeController getEdgeController() {
        return edgeController;
    }

    public void addRow(@NotNull MutableNodeRow row) {
        allRows.add(row);
    }

    @NotNull
    public ElementVisibilityController getVisibilityController() {
        return visibilityController;
    }

    @Override
    public int size() {
        return indexVisibleRows.size();
    }

    @NotNull
    @Override
    public NodeRow getNodeRow(int rowIndex) {
        return allRows.get(indexVisibleRows.get(rowIndex));
    }

    public void updateVisibleRows() {
        indexVisibleRows.clear();
        for (int  i = 0; i < allRows.size(); i++) {
            MutableNodeRow row = allRows.get(i);
            if (row.hasVisibleNodes()) {
                row.setRowIndex(indexVisibleRows.size());
                indexVisibleRows.add(i);
            }
        }
    }

    public void runUpdate(@NotNull Replace replace) {
        for (Executor<Replace> listener : listenerList) {
            listener.execute(replace);
        }
    }

    @Override
    public void addUpdateListener(@NotNull Executor<Replace> listener) {
        listenerList.add(listener);
    }

    @Override
    public void removeAllListeners() {
        listenerList.clear();
    }
}

@@ -751,23 +751,25 @@
 ist%3C
-Integer%3E indexV
+MutableNodeRow%3E v
 isib
@@ -795,15 +795,22 @@
 ist%3C
-Integer
+MutableNodeRow
 %3E();
@@ -1268,22 +1268,17 @@
  return 
-indexV
+v
 isibleRo
@@ -1387,26 +1387,9 @@
 urn 
-allRows.get(indexV
+v
 isib
@@ -1408,17 +1408,16 @@
 owIndex)
-)
 ;%0A    %7D%0A
@@ -1455,38 +1455,33 @@
 ows() %7B%0A        
-indexV
+v
 isibleRows.clear
@@ -1658,22 +1658,17 @@
 owIndex(
-indexV
+v
 isibleRo
@@ -1699,14 +1699,9 @@
     
-indexV
+v
 isib
@@ -1711,17 +1711,19 @@
 ows.add(
-i
+row
 );%0A     

fix bad integer index in MutableGraph