package org.hanuna.gitalk.refs;

import org.hanuna.gitalk.commitmodel.Hash;
import org.hanuna.gitalk.common.ReadOnlyList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author erokhins
 */
public class RefsModel {
    private final List<Ref> allRefs;
    private final Set<Hash> trackedHash = new HashSet<Hash>();
    private final List<Ref> localBranches = new ArrayList<Ref>();
    private final List<Ref> remoteBranches = new ArrayList<Ref>();

    public RefsModel(List<Ref> allRefs) {
        this.allRefs = allRefs;
        for (Ref ref : allRefs) {
            trackedHash.add(ref.getCommitHash());
            if (ref.getType() == Ref.Type.LOCAL_BRANCH) {
                localBranches.add(ref);
            }
            if (ref.getType() == Ref.Type.REMOTE_BRANCH) {
                remoteBranches.add(ref);
            }
        }
    }

    @Nullable
    public Ref refInToCommit(@NotNull Hash hash) {
        if (trackedHash.contains(hash)) {
            for (Ref ref : allRefs) {
                if (ref.getCommitHash().equals(hash)) {
                    return ref;
                }
            }
        }
        return null;
    }

    public ReadOnlyList<Ref> localBranches() {
        return ReadOnlyList.newReadOnlyList(localBranches);
    }

    public ReadOnlyList<Ref> remoteBranches() {
        return ReadOnlyList.newReadOnlyList(remoteBranches);
    }

}

@@ -26,16 +26,61 @@
 .refs;%0A%0A
+import org.hanuna.gitalk.commitmodel.Commit;%0A
 import o
@@ -114,16 +114,16 @@
 l.Hash;%0A
-
 import o
@@ -399,16 +399,749 @@
 Model %7B%0A
+    public static RefsModel existedCommitRefs(List%3CRef%3E allRefs, List%3CCommit%3E commits) %7B%0A        Set%3CHash%3E refCommits = new HashSet%3CHash%3E();%0A        for (Ref ref : allRefs) %7B%0A            refCommits.add(ref.getCommitHash());%0A        %7D%0A        Set%3CHash%3E existedCommitsRefs = new HashSet%3CHash%3E();%0A        for (Commit commit : commits) %7B%0A            if (refCommits.contains(commit.hash())) %7B%0A                existedCommitsRefs.add(commit.hash());%0A            %7D%0A        %7D%0A%0A        List%3CRef%3E existedRef = new ArrayList%3CRef%3E();%0A        for (Ref ref : allRefs) %7B%0A            if (existedCommitsRefs.contains(ref.getCommitHash())) %7B%0A                existedRef.add(ref);%0A            %7D%0A        %7D%0A        return new RefsModel(existedRef);%0A    %7D%0A%0A
     priv
@@ -1781,16 +1781,16 @@
 ullable%0A
+
     publ
@@ -1803,10 +1803,8 @@
  ref
-In
 ToCo

add existedCommitRefs