package org.hanuna.gitalk.ui_controller;

import org.hanuna.gitalk.common.Executor;
import org.hanuna.gitalk.common.Get;
import org.hanuna.gitalk.common.MyTimer;
import org.hanuna.gitalk.common.compressedlist.Replace;
import org.hanuna.gitalk.graph.Graph;
import org.hanuna.gitalk.graph.elements.Node;
import org.hanuna.gitalk.graph.mutable.GraphBuilder;
import org.hanuna.gitalk.graph.mutable.MutableGraph;
import org.hanuna.gitalk.graphmodel.FragmentManager;
import org.hanuna.gitalk.graphmodel.GraphModel;
import org.hanuna.gitalk.graphmodel.impl.GraphModelImpl;
import org.hanuna.gitalk.log.commit.Commit;
import org.hanuna.gitalk.log.commit.Hash;
import org.hanuna.gitalk.log.commitdata.CommitDataGetter;
import org.hanuna.gitalk.printmodel.GraphPrintCellModel;
import org.hanuna.gitalk.printmodel.SelectController;
import org.hanuna.gitalk.printmodel.impl.GraphPrintCellModelImpl;
import org.hanuna.gitalk.refs.RefsModel;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Set;


/**
 * @author erokhins
 */
public class DataPack {
    private final RefsModel refsModel;
    private final List<Commit> commits;
    private final CommitDataGetter commitDataGetter;
    private MutableGraph graph;
    private GraphModel graphModel;
    private GraphPrintCellModel printCellModel;

    public DataPack(RefsModel refsModel, List<Commit> commits, CommitDataGetter commitDataGetter) {
        this.refsModel = refsModel;
        this.commits = commits;
        this.commitDataGetter = commitDataGetter;
        MyTimer graphTimer = new MyTimer("graph build");
        //graph = GraphBuilder.build(commits, refsModel);
        graph = GraphBuilder.build(commits);
        graphModel = new GraphModelImpl(graph);

        graphTimer.print();

        updatePrintModel();
    }

    public void setShowBranches(@NotNull final Set<Hash> startedCommit) {
        graphModel.setVisibleBranchesNodes(new Get<Node, Boolean>() {
            @NotNull
            @Override
            public Boolean get(@NotNull Node key) {
                return startedCommit.contains(key.getCommitHash());
            }
        });
    }

    @NotNull
    public RefsModel getRefsModel() {
        return refsModel;
    }

    @NotNull
    public Graph getGraph() {
        return graph;
    }

    @NotNull
    public GraphPrintCellModel getPrintCellModel() {
        return printCellModel;
    }

    @NotNull
    public FragmentManager getFragmentManager() {
        return graphModel.getFragmentManager();
    }

    @NotNull
    public SelectController getSelectController() {
        return printCellModel.getSelectController();
    }

    public CommitDataGetter getCommitDataGetter() {
        return commitDataGetter;
    }

    public void hideAll() {
        graphModel.getFragmentManager().hideAll();
    }

    public void updatePrintModel() {
        MyTimer printModelTimer = new MyTimer("print model build");
        printCellModel = new GraphPrintCellModelImpl(graph);
        printModelTimer.print();

        graphModel.removeAllListeners();
        graphModel.addUpdateListener(new Executor<Replace>() {
            @Override
            public void execute(Replace key) {
                printCellModel.recalculate(key);
            }
        });
    }
}

@@ -964,16 +964,42 @@
 tNull;%0A%0A
+import java.util.HashSet;%0A
 import j
@@ -1357,16 +1357,22 @@
 ataPack(
+final 
 RefsMode
@@ -1561,32 +1561,38 @@
 Getter;%0A        
+final 
 MyTimer graphTim
@@ -1624,24 +1624,25 @@
 uild%22);%0A
+%0A
         
 //graph 
@@ -1637,45 +1637,89 @@
     
-//graph = GraphBuilder.build(
+final Set%3CHash%3E trackedHashes = new HashSet%3CHash%3E();%0A        for (Commit 
 commit
-s,
+ :
  ref
@@ -1728,101 +1728,504 @@
 odel
-);%0A        graph = GraphBuilder.build(commits);%0A        graphModel = new GraphModelImpl(graph
+.getOrderedLogTrackedCommit()) %7B%0A            trackedHashes.add(commit.getCommitHash());%0A        %7D%0A%0A        graph = GraphBuilder.build(commits);%0A        graphModel = new GraphModelImpl(graph);%0A        graphModel.getFragmentManager().setUnhiddenNodes(new Get%3CNode, Boolean%3E() %7B%0A            @NotNull%0A            @Override%0A            public Boolean get(@NotNull Node key) %7B%0A                return key.getDownEdges().size() == 0 %7C%7C trackedHashes.contains(key.getCommitHash());%0A            %7D%0A        %7D
 );%0A%0A

add unhidden nodes