package com.github.mobile.android.issue;

import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.github.mobile.android.R.id;
import com.github.mobile.android.R.layout;
import com.github.mobile.android.comment.CommentViewHolder;
import com.github.mobile.android.util.Avatar;
import com.github.mobile.android.util.GitHubIntents;
import com.github.mobile.android.util.Html;
import com.github.mobile.android.util.HttpImageGetter;
import com.github.mobile.android.util.Time;
import com.google.inject.Inject;
import com.madgag.android.listviews.ViewHoldingListAdapter;
import com.madgag.android.listviews.ViewInflator;

import java.util.List;

import org.eclipse.egit.github.core.Comment;
import org.eclipse.egit.github.core.Issue;
import org.eclipse.egit.github.core.service.IssueService;

import roboguice.activity.RoboFragmentActivity;
import roboguice.inject.ContextScopedProvider;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;
import roboguice.util.RoboAsyncTask;

/**
 * Activity to view a specific issue
 */
public class ViewIssueActivity extends RoboFragmentActivity {

    public static Intent viewIssueIntentFor(Issue issue) {
        return new GitHubIntents.Builder("repo.issue.VIEW").issue(issue).toIntent();
    }

    @Inject
    private ContextScopedProvider<IssueService> service;

    @InjectView(R.id.list)
    private ListView comments;

    private IssueBodyViewHolder body;

    @InjectExtra(GitHubIntents.EXTRA_REPOSITORY_NAME) String repository;
    @InjectExtra(GitHubIntents.EXTRA_REPOSITORY_OWNER) String repositoryOwner;
    @InjectExtra(GitHubIntents.EXTRA_ISSUE_NUMBER) int issueNumber;


    private HttpImageGetter imageGetter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.issue_view);

        imageGetter = new HttpImageGetter(this);

        ((TextView) findViewById(id.tv_issue_number)).setText("Issue #" + issueNumber);

        loadIssue();
    }

    private void loadIssue() {
        new RoboAsyncTask<Issue>(this) {

            public Issue call() throws Exception {
                return service.get(ViewIssueActivity.this).getIssue(repositoryOwner,
                        repository, issueNumber);
            }

            protected void onSuccess(Issue issue) throws Exception {
                ((TextView) findViewById(id.tv_issue_title)).setText(issue.getTitle());
                String reported = "<b>" + issue.getUser().getLogin() + "</b> "
                        + Time.relativeTimeFor(issue.getCreatedAt());
                ((TextView) findViewById(id.tv_issue_creation)).setText(Html.encode(reported));
                Avatar.bind(ViewIssueActivity.this, (ImageView) findViewById(id.iv_gravatar), issue.getUser());
                View view = getLayoutInflater().inflate(layout.issue_view_body, null);
                body = new IssueBodyViewHolder(ViewIssueActivity.this, imageGetter, view);
                body.updateViewFor(issue);
                comments.addHeaderView(view);
                loadComments();
            }
        }.execute();
    }

    private void loadImages(final TextView view, final String html) {
        view.setText(Html.encode(html));
        new RoboAsyncTask<CharSequence>(this) {

            public CharSequence call() throws Exception {
                return Html.encode(html, imageGetter);
            }

            protected void onSuccess(CharSequence html) throws Exception {
                view.setText(html);
            }
        }.execute();
    }

    private void loadComments() {
        new RoboAsyncTask<List<Comment>>(this) {

            public List<Comment> call() throws Exception {
                return service.get(ViewIssueActivity.this).getComments(repositoryOwner,
                                        repository, issueNumber);
            }

            protected void onSuccess(List<Comment> issueComments) throws Exception {
                comments.setAdapter(new ViewHoldingListAdapter<Comment>(issueComments, ViewInflator.viewInflatorFor(
                        ViewIssueActivity.this, layout.comment_view_item), CommentViewHolder.createFactory(
                        ViewIssueActivity.this, imageGetter)));
            }
        }.execute();
    }

}

@@ -475,16 +475,77 @@
 ntents;%0A
+import com.github.mobile.android.util.GitHubIntents.Builder;%0A
 import c
@@ -1304,24 +1304,124 @@
 Activity %7B%0A%0A
+    /**%0A     * Create intent to view issue%0A     *%0A     * @param issue%0A     * @return intent%0A     */%0A
     public s
@@ -1486,30 +1486,16 @@
 urn new 
-GitHubIntents.
 Builder(
@@ -1766,16 +1766,20 @@
 RY_NAME)
+%0A   
  String 
@@ -1844,16 +1844,20 @@
 Y_OWNER)
+%0A   
  String 
@@ -1923,16 +1923,20 @@
 _NUMBER)
+%0A   
  int iss
@@ -1946,17 +1946,16 @@
 umber;%0A%0A
-%0A
     priv
@@ -2512,32 +2512,8 @@
 ner,
-%0A                       
  rep
@@ -3409,449 +3409,8 @@
  %7D%0A%0A
-    private void loadImages(final TextView view, final String html) %7B%0A        view.setText(Html.encode(html));%0A        new RoboAsyncTask%3CCharSequence%3E(this) %7B%0A%0A            public CharSequence call() throws Exception %7B%0A                return Html.encode(html, imageGetter);%0A            %7D%0A%0A            protected void onSuccess(CharSequence html) throws Exception %7B%0A                view.setText(html);%0A            %7D%0A        %7D.execute();%0A    %7D%0A%0A
     
@@ -3639,48 +3639,8 @@
 ner,
-%0A                                       
  rep
@@ -4092,11 +4092,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Add comments and remove unused method