package com.gh4a.loader;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.egit.github.core.RepositoryContents;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.service.ContentsService;
import org.eclipse.egit.github.core.util.EncodingUtils;

import android.content.Context;

import com.gh4a.Gh4Application;
import com.gh4a.utils.StringUtils;

public class GitModuleParserLoader extends BaseLoader<Map<String, String>> {

    private String mRepoOwner;
    private String mRepoName;
    private String mPath;
    private String mRef;

    public GitModuleParserLoader(Context context, String repoOwner, String repoName, String path, String ref) {
        super(context);
        mRepoOwner = repoOwner;
        mRepoName = repoName;
        mPath = path;
        mRef = ref;
    }

    @Override
    public Map<String, String> doLoadInBackground() throws IOException {
        ContentsService contentService = (ContentsService)
                Gh4Application.get().getService(Gh4Application.CONTENTS_SERVICE);
        List<RepositoryContents> contents =
                contentService.getContents(new RepositoryId(mRepoOwner, mRepoName), mPath, mRef);

        if (contents == null || contents.isEmpty()) {
            return null;
        }
        String data = new String(EncodingUtils.fromBase64(contents.get(0).getContent()));
        if (StringUtils.isBlank(data)) {
            return null;
        }
        Map<String, String> gitModuleMap = new HashMap<>();
        String[] lines = data.split("\n");
        String path = null;
        for (String line : lines) {
            line = line.trim();
            if (line.startsWith("path = ")) {
                String[] pathPart = line.split("=");
                path = pathPart[1].trim();
            }

            if (line.startsWith("url = ")) {
                String[] urlPart = line.split("=");
                String url = urlPart[1].trim();
                String[] userRepoPart = url.split("/");
                String user = userRepoPart[3];
                String repo = userRepoPart[4];

                if (repo.lastIndexOf(".") != -1) {
                    repo = repo.substring(0, repo.lastIndexOf("."));
                }
                gitModuleMap.put(path, user + "/" + repo);
            }
        }
        return gitModuleMap;
    }
}

@@ -374,16 +374,71 @@
 Context;
+%0Aimport android.net.Uri;%0Aimport android.text.TextUtils;
 %0A%0Aimport
@@ -1917,29 +1917,21 @@
        %7D
-%0A%0A           
+ else
  if (lin
@@ -2000,32 +2000,33 @@
 ine.split(%22=%22);%0A
+%0A
                 
@@ -2059,158 +2059,691 @@
 im()
-;%0A                String%5B%5D userRepoPart = url.split(%22/%22);%0A                String user = userRepoPart%5B3%5D;%0A                String repo = userRepoPart%5B4%5D
+.replace(%22github.com:%22, %22github.com/%22);%0A                int pos = url.indexOf(%22git@%22);%0A                if (pos == 0) %7B%0A                    url = %22ssh://%22 + url.substring(4);%0A                %7D%0A%0A                Uri uri = Uri.parse(url);%0A                if (!TextUtils.equals(uri.getHost(), %22github.com%22)) %7B%0A                    continue;%0A                %7D%0A                List%3CString%3E pathSegments = uri.getPathSegments();%0A                if (pathSegments == null %7C%7C pathSegments.size() %3C 2) %7B%0A                    continue;%0A                %7D%0A                String user = pathSegments.get(pathSegments.size() - 2);%0A                String repo = pathSegments.get(pathSegments.size() - 1)
 ;%0A%0A 
@@ -2757,20 +2757,22 @@
         
-if (
+pos = 
 repo.las
@@ -2784,16 +2784,41 @@
 xOf(%22.%22)
+;%0A                if (pos
  != -1) 
@@ -2864,37 +2864,19 @@
 ring(0, 
-repo.lastIndexOf(%22.%22)
+pos
 );%0A     

Make module parser more lenient.