package org.hanuna.gitalk.refs;

import org.hanuna.gitalk.commitmodel.Commit;
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 {
    public static RefsModel existedCommitRefs(List<Ref> allRefs, List<Commit> commits) {
        Set<Hash> refCommits = new HashSet<Hash>();
        for (Ref ref : allRefs) {
            refCommits.add(ref.getCommitHash());
        }
        Set<Hash> existedCommitsRefs = new HashSet<Hash>();
        for (Commit commit : commits) {
            if (refCommits.contains(commit.hash())) {
                existedCommitsRefs.add(commit.hash());
            }
        }

        List<Ref> existedRef = new ArrayList<Ref>();
        for (Ref ref : allRefs) {
            if (existedCommitsRefs.contains(ref.getCommitHash())) {
                existedRef.add(ref);
            }
        }
        return new RefsModel(existedRef);
    }

    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 ReadOnlyList<Ref> refsToCommit(@NotNull Hash hash) {
        List<Ref> refs = new ArrayList<Ref>();
        if (trackedHash.contains(hash)) {
            for (Ref ref : allRefs) {
                if (ref.getCommitHash().equals(hash)) {
                    refs.add(ref);
                }
            }
        }
        return ReadOnlyList.newReadOnlyList(refs);
    }

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

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

}

@@ -205,51 +205,8 @@
 ull;
-%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aim
@@ -642,32 +642,105 @@
 ashSet%3CHash%3E();%0A
+        List%3CCommit%3E orderedLogExistedCommits = new ArrayList%3CCommit%3E();%0A
         for (Com
@@ -864,32 +864,86 @@
 commit.hash());%0A
+                orderedLogExistedCommits.add(commit);%0A
             %7D%0A  
@@ -1206,16 +1206,42 @@
 istedRef
+, orderedLogExistedCommits
 );%0A    %7D
@@ -1369,116 +1369,39 @@
 ist%3C
-Ref%3E localBranches = new ArrayList%3CRef%3E();%0A    private final List%3CRef%3E remoteBranches = new ArrayList%3CRef%3E()
+Commit%3E orderedLogTrackedCommit
 ;%0A%0A 
@@ -1429,32 +1429,70 @@
 ist%3CRef%3E allRefs
+, List%3CCommit%3E orderedLogTrackedCommit
 ) %7B%0A        this
@@ -1523,82 +1523,62 @@
     
-for (Ref ref : allRefs) %7B%0A            trackedHash.add(ref.getCommitHash())
+this.orderedLogTrackedCommit = orderedLogTrackedCommit
 ;%0A  
@@ -1587,225 +1587,83 @@
     
-    if (ref.getType() == Ref.Type.LOCAL_BRANCH) %7B%0A                localBranches.add(ref);%0A            %7D%0A            if (ref.getType() == Ref.Type.REMOTE_BRANCH) %7B%0A                remoteBranches.add(ref);%0A            %7D
+for (Ref ref : allRefs) %7B%0A            trackedHash.add(ref.getCommitHash());
 %0A   
@@ -1686,15 +1686,14 @@
   @N
+otN
 ull
-able
 %0A   
@@ -2079,161 +2079,75 @@
     
-public ReadOnlyList%3CRef%3E localBranches() %7B%0A        return ReadOnlyList.newReadOnlyList(localBranches);%0A    %7D%0A%0A    public ReadOnlyList%3CRef%3E remoteBranches
+@NotNull%0A    public ReadOnlyList%3CCommit%3E getOrderedLogTrackedCommit
 () %7B
@@ -2195,22 +2195,31 @@
 ist(
-remoteBranches
+orderedLogTrackedCommit
 );%0A 
@@ -2223,11 +2223,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Refactor RefsModel