package org.hanuna.gitalk.commitmodel;

import org.hanuna.gitalk.common.HashMultiMap;
import org.hanuna.gitalk.common.MultiMap;
import org.hanuna.gitalk.common.ReadOnlyList;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;

/**
 * @author erokhins
 */
public class CommitsModel implements ReadOnlyList<Commit> {
    private static final int HIDE_LIMIT = 25;
    private static final int COUNT_SHOW = 3;

    public static CommitsModel buildModel(ReadOnlyList<Commit> commits) {
        CommitsModel model = new CommitsModel(commits);
        model.buildHideShowMap();
        return model;
    }

    private final ReadOnlyList<Commit> commits;
    private final MultiMap<Integer, Commit> commitsHide = new HashMultiMap<Integer, Commit>();
    private final MultiMap<Integer, Commit> commitsShow = new HashMultiMap<Integer, Commit>();

    private CommitsModel(ReadOnlyList<Commit> commits) {
        this.commits = commits;
    }

    private void buildHideShowMap() {
        for (int i = 0; i < commits.size(); i++) {
            Commit commit = commits.get(i);
            for (Commit parent : commit.getParents()) {
                if (parent.getChildren().size() == 1) {
                    if (parent.wasRead()) {
                        if (parent.index() - i > HIDE_LIMIT) {
                            commitsHide.add(i + COUNT_SHOW, parent);
                            commitsShow.add(parent.index() - COUNT_SHOW, parent);
                        }
                    } else {
                        commitsHide.add(i + COUNT_SHOW, parent);
                    }
                }
            }
        }
    }


    @NotNull
    public ReadOnlyList<Commit> hidesCommits(int index) {
        return commitsHide.get(index);
    }

    @NotNull
    public ReadOnlyList<Commit> showsCommits(int index) {
        return commitsShow.get(index);
    }


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

    @Override
    public Commit get(int index) {
        return commits.get(index);
    }

    @Override
    public Iterator<Commit> iterator() {
        return commits.iterator();
    }
}

@@ -1095,186 +1095,1481 @@
     
-for (Commit parent : commit.getParents()) %7B%0A                if (parent.getChildren().size() == 1) %7B%0A                    if (parent.wasRead()) %7B%0A                        if (parent
+ReadOnlyList%3CCommit%3E currentChildrens = commit.getChildren();%0A            if (currentChildrens.size() %3E 0) %7B%0A                Commit lastChildren = currentChildrens.get(currentChildrens.size() - 1);%0A                assert lastChildren.wasRead() : %22last children not read%22;%0A                if (i - lastChildren.index() %3E HIDE_LIMIT) %7B%0A                    commitsShow.add(i - COUNT_SHOW, commit);%0A                %7D%0A            %7D%0A            for (Commit parent : commit.getParents()) %7B%0A                int childrenIndex = -1;%0A                ReadOnlyList%3CCommit%3E childrens = parent.getChildren();%0A                for (int  j = 0; j %3C childrens.size(); j++) %7B%0A                    if (commit == childrens.get(j)) %7B%0A                        childrenIndex = j;%0A                    %7D%0A                %7D%0A                assert childrenIndex != -1 : %22bad commits model%22;%0A                if (childrenIndex %3E 0) %7B%0A                    Commit prevChildren = childrens.get(childrenIndex - 1);%0A                    assert prevChildren.wasRead() : %22prev children not read%22;%0A                    if (i - prevChildren.index() %3E HIDE_LIMIT) %7B%0A                        commitsShow.add(i - COUNT_SHOW, parent);%0A                    %7D%0A                %7D%0A                if (childrenIndex %3C childrens.size() - 1) %7B%0A                    Commit nextChildren = childrens.get(childrenIndex + 1);%0A                    assert nextChildren.wasRead() : %22next children not read%22;%0A                    if (nextChildren
 .ind
@@ -2585,36 +2585,32 @@
 %3E HIDE_LIMIT) %7B%0A
-    
                 
@@ -2682,48 +2682,189 @@
     
-        commitsShow.add(parent.index() -
+%7D%0A                %7D else %7B%0A                    if (parent.wasRead()) %7B%0A                        if (parent.index() - i %3E HIDE_LIMIT) %7B%0A                            commitsHide.add(i +
  COU

fix hide commits with more childrens