package com.gh4a;

import android.content.Intent;
import android.os.Bundle;

import com.gh4a.utils.StringUtils;

public class BrowseFilter extends BaseActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String url = getIntent().getDataString();
        
        String[] urlPart = url.split("/");
        
        String host = urlPart[2];
        
        String first = urlPart[3];
        if ("languages".equals(first)
                || "training".equals(first)
                || "login".equals(first)
                || "contact".equals(first)
                || "features".equals(first)) {//skip this
            
        }
        else if ("explore".equals(first)) {//https://github.com/explore
            Intent intent = new Intent().setClass(this, ExploreActivity.class);
            startActivity(intent);
        }
        else if ("blog".equals(first)) {//https://github.com/blog
            Intent intent = new Intent().setClass(this, BlogListActivity.class);
            startActivity(intent);
        }
        else {
            Gh4Application context = getApplicationContext();
            if (urlPart.length == 4) {
                String user = urlPart[3];
                
                context.openUserInfoActivity(this, user, null);
            }
            else if (urlPart.length == 5) {
                String user = urlPart[3];
                String repo = urlPart[4];
                
                context.openRepositoryInfoActivity(this, user, repo, 0);
            }
            else if (urlPart.length == 6) {
                String user = urlPart[3];
                String repo = urlPart[4];
                String action = urlPart[5];
                
                if ("issues".equals(action)) {
                    context.openIssueListActivity(this, user, repo, Constants.Issue.ISSUE_STATE_OPEN);
                }
                else if ("pulls".equals(action)) {
                    context.openPullRequestListActivity(this, user, repo, Constants.Issue.ISSUE_STATE_OPEN);
                }
                else if ("wiki".equals(action)) {
                    Intent intent = new Intent().setClass(this, WikiListActivity.class);
                    intent.putExtra(Constants.Repository.REPO_OWNER, user);
                    intent.putExtra(Constants.Repository.REPO_NAME, repo);
                    startActivity(intent);
                }
            }
            else if (urlPart.length >= 7) {
                String user = urlPart[3];
                String repo = urlPart[4];
                String action = urlPart[5];
                String id = urlPart[6];
                
                if ("issues".equals(action)) {
                    if (!StringUtils.isBlank(id)) {
                        try {
                            context.openIssueActivity(this, user, repo, Integer.parseInt(id));
                        }
                        catch (NumberFormatException e) {
                            // Ignore non-numeric ids
                        }
                    }
                }
                else if ("pull".equals(action)) {
                    if (!StringUtils.isBlank(id)) {
                        try {
                            context.openPullRequestActivity(this, user, repo, Integer.parseInt(id));
                        }
                        catch (NumberFormatException e) {
                            // Ignore non-numeric ids
                        }
                    }
                }
                else if ("commit".equals(action)) {
                    if (!StringUtils.isBlank(id)) {
                        try {
                            context.openCommitInfoActivity(this, user, repo, id, 0);
                        }
                        catch (NumberFormatException e) {
                            // Ignore non-numeric ids
                        }
                    }
                }
            }
        }
        finish();
    }
}

@@ -68,16 +68,47 @@
 .Bundle;
+%0Aimport android.text.TextUtils;
 %0A%0Aimport
@@ -3067,467 +3067,1397 @@
  // 
-Ignore non-numeric ids%0A                        %7D%0A                    %7D%0A                %7D%0A                else if (%22pull%22.equals(action)) %7B%0A                    if (!StringUtils.isBlank(id)) %7B%0A                        try %7B%0A                            context.openPullRequestActivity(this, user, repo, Integer.parseInt(id));%0A                        %7D%0A                        catch (NumberFormatException e) %7B%0A                            // Ignore non-numeric ids
+try issue with fragment url%0A                            // https://github.com/slapperwan/gh4a/issues/87#issuecomment-7680638%0A                            if (id.indexOf(%22#%22) != -1) %7B%0A                                id = id.substring(0, id.indexOf(%22#%22));%0A                                if (TextUtils.isDigitsOnly(id)) %7B%0A                                    context.openIssueActivity(this, user, repo, Integer.parseInt(id));%0A                                %7D%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A                else if (%22pull%22.equals(action)) %7B%0A                    if (!StringUtils.isBlank(id)) %7B%0A                        try %7B%0A                            context.openPullRequestActivity(this, user, repo, Integer.parseInt(id));%0A                        %7D%0A                        catch (NumberFormatException e) %7B%0A                            // try issue with fragment url%0A                            // https://github.com/slapperwan/gh4a/pull/50#issuecomment-2342877%0A                            if (id.indexOf(%22#%22) != -1) %7B%0A                                id = id.substring(0, id.indexOf(%22#%22));%0A                                if (TextUtils.isDigitsOnly(id)) %7B%0A                                    context.openPullRequestActivity(this, user, repo, Integer.parseInt(id));%0A                                %7D%0A                            %7D
 %0A   

Handle issue/pull request URL with fragment