package com.gh4a.adapter.timeline;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.gh4a.R;
import com.gh4a.activities.ReviewCommentsActivity;
import com.gh4a.loader.TimelineItem;
import com.gh4a.utils.AvatarHandler;
import com.gh4a.utils.StringUtils;

import org.eclipse.egit.github.core.CommitFile;
import org.eclipse.egit.github.core.Review;

import java.util.HashSet;
import java.util.Set;

class ReviewViewHolder
        extends TimelineItemAdapter.TimelineItemViewHolder<TimelineItem.TimelineReview>
        implements View.OnClickListener {

    private final Context mContext;
    private final String mRepoOwner;
    private final String mRepoName;
    private final int mIssueNumber;
    private final boolean mIsPullRequest;

    private final ImageView mAvatarView;
    private final TextView mMessageView;
    private final TextView mBodyView;
    private final TextView mDetailsView;
    private final Button mShowDetailsButton;

    public ReviewViewHolder(View itemView, String repoOwner, String repoName, int issueNumber,
            boolean isPullRequest) {
        super(itemView);

        mContext = itemView.getContext();
        mRepoOwner = repoOwner;
        mRepoName = repoName;
        mIssueNumber = issueNumber;
        mIsPullRequest = isPullRequest;

        mAvatarView = (ImageView) itemView.findViewById(R.id.iv_gravatar);
        mMessageView = (TextView) itemView.findViewById(R.id.tv_message);
        mBodyView = (TextView) itemView.findViewById(R.id.tv_desc);
        mDetailsView = (TextView) itemView.findViewById(R.id.tv_details);
        mShowDetailsButton = (Button) itemView.findViewById(R.id.btn_show_details);
        mShowDetailsButton.setOnClickListener(this);
    }

    @Override
    public void bind(TimelineItem.TimelineReview item) {
        Review review = item.review;
        mShowDetailsButton.setTag(item);

        AvatarHandler.assignAvatar(mAvatarView, review.getUser());
        mAvatarView.setTag(review.getUser());

        formatTitle(review);

        if (!TextUtils.isEmpty(review.getBody())) {
            mBodyView.setText(review.getBody());
            mBodyView.setVisibility(View.VISIBLE);
        } else {
            mBodyView.setVisibility(View.GONE);
        }

        if (!item.chunks.isEmpty()) {
            String text = "Code comments in ";
            Set<String> usedNames = new HashSet<>();
            for (TimelineItem.Diff diff : item.chunks.values()) {
                if (!diff.comments.isEmpty()) {
                    CommitFile file = diff.getInitialTimelineComment().file;
                    if (file != null) {
                        String filename = file.getFilename();
                        if (!usedNames.contains(filename)) {
                            text += filename + ", ";
                            usedNames.add(filename);
                        }
                    }
                }
            }
            mDetailsView.setText(text);

            mDetailsView.setVisibility(View.VISIBLE);
            mShowDetailsButton.setVisibility(View.VISIBLE);
        } else {
            mDetailsView.setVisibility(View.GONE);
            mShowDetailsButton.setVisibility(View.GONE);
        }
    }

    private void formatTitle(Review review) {
        String login = review.getUser().getLogin();
        CharSequence time = review.getSubmittedAt() != null
                ? StringUtils.formatRelativeTime(mContext, review.getSubmittedAt(), true) : "";

        switch (review.getState()) {
            case Review.STATE_APPROVED:
                mMessageView.setText(login + " approved these changes " + time);
                break;
            case Review.STATE_CHANGES_REQUESTED:
                mMessageView.setText(login + " requested changes" + time);
                break;
            case Review.STATE_DISMISSED:
            case Review.STATE_COMMENTED:
                mMessageView.setText(login + " reviewed " + time);
                break;
            case Review.STATE_PENDING:
                mMessageView.setText(login + " started a review " + time);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        TimelineItem.TimelineReview review = (TimelineItem.TimelineReview) v.getTag();

        mContext.startActivity(ReviewCommentsActivity.makeIntent(mContext, mRepoOwner, mRepoName,
                mIssueNumber, mIsPullRequest, review));
    }
}

@@ -394,16 +394,67 @@
 Utils;%0A%0A
+import org.eclipse.egit.github.core.CommitComment;%0A
 import o
@@ -2492,16 +2492,44 @@
 ring
- text = 
+Builder builder = new StringBuilder(
 %22Cod
@@ -2543,16 +2543,17 @@
 nts in %22
+)
 ;%0A      
@@ -2595,24 +2595,65 @@
 ashSet%3C%3E();%0A
+%0A            boolean isOutdated = true;%0A%0A
             
@@ -2726,44 +2726,312 @@
     
-if (!diff.comments.isEmpty()) %7B%0A    
+TimelineItem.TimelineComment timelineComment = diff.getInitialTimelineComment();%0A%0A                CommitComment commitComment = timelineComment.getCommitComment();%0A                if (commitComment != null && commitComment.getPosition() != -1) %7B%0A                    isOutdated = false;%0A                %7D%0A
     
@@ -3060,32 +3060,17 @@
  file = 
-diff.getInitialT
+t
 imelineC
@@ -3079,20 +3079,16 @@
 ment
-()
 .file;%0A
-   
+%0A
     
@@ -3095,25 +3095,24 @@
             
- 
 if (file != 
@@ -3115,28 +3115,24 @@
  != null) %7B%0A
-    
             
@@ -3193,28 +3193,24 @@
             
-  
-  
 if (!usedNam
@@ -3262,41 +3262,47 @@
     
-    text += filename + 
+builder.append(filename).append(
 %22, %22
+)
 ;%0A
-    
     
@@ -3349,32 +3349,43 @@
 me);%0A           
+         %7D%0A
              %7D%0A 
@@ -3373,32 +3373,35 @@
  %7D%0A             
+   
 %7D%0A              
@@ -3387,32 +3387,38 @@
      %7D%0A         
+   %7D%0A%0A
            %7D%0A   
@@ -3412,35 +3412,99 @@
         
-%7D%0A                %7D
+ if (isOutdated) %7B%0A                builder.append(%22%5Cn%5CnAll comments are outdated%22);
 %0A       
@@ -3502,32 +3502,33 @@
 ;%0A            %7D%0A
+%0A
             mDet
@@ -3544,20 +3544,34 @@
 setText(
-text
+builder.toString()
 );%0A%0A    

Add review outdated indicator text