package org.edx.mobile.view.adapters;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.inject.Inject;
import com.qualcomm.qlearn.sdk.discussion.APICallback;
import com.qualcomm.qlearn.sdk.discussion.DiscussionAPI;
import com.qualcomm.qlearn.sdk.discussion.DiscussionComment;

import org.edx.mobile.R;
import org.edx.mobile.core.IEdxEnvironment;
import org.edx.mobile.task.FlagCommentTask;
import org.edx.mobile.third_party.iconify.IconView;
import org.edx.mobile.third_party.iconify.Iconify;

public class DiscussionCommentsAdapter extends BaseListAdapter <DiscussionComment> {

    private final Context context;
    private FlagCommentTask flagCommentTask;

    @Inject
    public DiscussionCommentsAdapter(Context context, IEdxEnvironment environment) {
        super(context, R.layout.row_discussion_comment, environment);
        this.context = context;
    }

    @Override
    public void render(BaseViewHolder tag, final DiscussionComment discussionComment) {
        ViewHolder holder = (ViewHolder) tag;

        String commentBody = discussionComment.getRawBody();
        holder.discussionCommentBody.setText(commentBody);

        int childrenSize = discussionComment.getChildren().size();
        Iconify.IconValue icon = childrenSize == 0 ? Iconify.IconValue.fa_flag : Iconify.IconValue.fa_comment; // show flag icon for comment, and comment icon for response
        holder.discussionCommentCountReportIcon.setIcon(icon);

        holder.discussionCommentAuthorTextView.setText(discussionComment.getAuthor());

        CharSequence formattedDate = DateUtils.getRelativeTimeSpanString(
                discussionComment.getCreatedAt().getTime(),
                System.currentTimeMillis(),
                DateUtils.MINUTE_IN_MILLIS,
                DateUtils.FORMAT_ABBREV_RELATIVE);

        holder.discussionCommentDateTextView.setText(formattedDate);
        // TODO: localization
        String report = discussionComment.isAbuseFlagged() ? "Reported" : "Report";
        holder.discussionCommentCountReportTextView.setText(childrenSize == 0 ? report : (childrenSize + " comment" + (childrenSize == 1 ? "" : "s")));

        holder.discussionCommentCountReportTextView.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View v) {
                flagComment(v, discussionComment);
            }
        });
    }


    protected void flagComment(final View v, final DiscussionComment discussionComment) {

        if ( flagCommentTask != null ){
            flagCommentTask.cancel(true);
        }
        flagCommentTask = new FlagCommentTask(getContext(), discussionComment, true) {
            @Override
            public void onSuccess(DiscussionComment comment) {
                // TODO: change Report to Reported and the flag icon color to red?
                TextView reportTextView = (TextView) v;
                reportTextView.setText("Reported");
            }

            @Override
            public void onException(Exception ex) {
                logger.error(ex);
                //  hideProgress();

            }
        };
        flagCommentTask.execute();

    }

    @Override
    public BaseViewHolder getTag(View convertView) {
        ViewHolder holder = new ViewHolder();

        holder.discussionCommentRow = (LinearLayout) convertView.findViewById(R.id.row_discussion_comment_linear_layout);
        holder.discussionCommentBody = (TextView) convertView.findViewById(R.id.discussion_comment_body);
        holder.discussionCommentAuthorTextView = (TextView) convertView.findViewById(R.id.discussion_comment_author_text_view);
        holder.discussionCommentDateTextView = (TextView) convertView.findViewById(R.id.discussion_comment_date_text_view);
        holder.discussionCommentCountReportIcon = (IconView) convertView.findViewById(R.id.discussion_comment_count_report_icon);
        holder.discussionCommentCountReportTextView = (TextView) convertView.findViewById(R.id.discussion_comment_count_report_text_view);

        return holder;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}

    private static class ViewHolder extends BaseViewHolder {
        LinearLayout discussionCommentRow;
        TextView discussionCommentBody;
        TextView discussionCommentAuthorTextView;
        TextView discussionCommentDateTextView;
        IconView discussionCommentCountReportIcon;
        TextView discussionCommentCountReportTextView;

    }

}
@@ -269,120 +269,8 @@
 ct;%0A
-import com.qualcomm.qlearn.sdk.discussion.APICallback;%0Aimport com.qualcomm.qlearn.sdk.discussion.DiscussionAPI;%0A
 impo
@@ -606,17 +606,16 @@
 tAdapter
- 
 %3CDiscuss
@@ -1018,32 +1018,38 @@
 ment) %7B%0A        
+final 
 ViewHolder holde
@@ -2369,17 +2369,22 @@
 Comment(
-v
+holder
 , discus
@@ -2472,18 +2472,29 @@
 nal View
- v
+Holder holder
 , final 
@@ -2532,17 +2532,16 @@
 ment) %7B%0A
-%0A
         
@@ -2544,17 +2544,16 @@
     if (
- 
 flagComm
@@ -2571,10 +2571,10 @@
 null
- 
 )
+ 
 %7B%0A  
@@ -2615,32 +2615,290 @@
 rue);%0A        %7D%0A
+        // TODO: Smarter way to handle check if reported than string comparison%0A        boolean isReport = holder.discussionCommentCountReportTextView.getText().toString().equalsIgnoreCase(getContext().getString(R.string.discussion_responses_report_label));%0A
         flagComm
@@ -2960,20 +2960,24 @@
 omment, 
-true
+isReport
 ) %7B%0A    
@@ -3077,180 +3077,477 @@
     
-// TODO: change Report to Reported and the flag icon color to red?%0A                TextView reportTextView = (TextView) v;%0A                reportTextView.setText(%22Reported%22
+int reportStringResId = comment.isAbuseFlagged() ? R.string.discussion_responses_reported_label :%0A                        R.string.discussion_responses_report_label;%0A                holder.discussionCommentCountReportTextView.setText(context.getString(reportStringResId));%0A                holder.discussionCommentCountReportIcon.setIconColor(context.getResources().getColor(comment.isAbuseFlagged() ? R.color.edx_brand_primary_base : R.color.edx_grayscale_neutral_base)
 );%0A 
@@ -4767,16 +4767,21 @@
 ng id) %7B
+%0A    
 %7D%0A%0A    p

Fix comment report button