// Path: app/src/main/java/com/hexforhn/hex/adapter/helper/CommentListManager.java
// public class CommentListManager {
//     private final List<CommentViewModel> mComments;
// 
//     public CommentListManager(List<CommentViewModel> comments) {
//         mComments = comments;
//     }
// 
//     public List<CommentViewModel> getVisibleComments() {
//         List<CommentViewModel> comments = new ArrayList<>();
//         int commentCount = mComments.size();
// 
//         for (int i = 0; i < commentCount; i++) {
//             CommentViewModel comment = mComments.get(i);
// 
//             if (comment.isVisible()) {
//                 comments.add(comment);
//             }
//         }
// 
//         return comments;
//     }
// 
//     public void toggleCommentAtPosition(int position) {
//         CommentViewModel commentToToggle = getVisibleComments().get(position);
//         int commentCount = mComments.size();
// 
//         commentToToggle.toggleCollapsed();
// 
//         int toggledCommentDepth = commentToToggle.getDepth();
//         boolean collapsedState = commentToToggle.isCollapsed();
// 
//         position = mComments.indexOf(commentToToggle);
// 
//         for (int i = position + 1; i < commentCount; i++) {
//             CommentViewModel comment = mComments.get(i);
// 
//             if (comment.getDepth() > toggledCommentDepth) {
//                 comment.setVisible(!collapsedState);
//                 comment.setCollapsed(collapsedState);
//             } else {
//                 break;
//             }
//         }
//     }
// 
// }
// 
// Path: app/src/main/java/com/hexforhn/hex/adapter/helper/TextHelper.java
// public class TextHelper {
// 
//     public static CharSequence removeTrailingNewlinesFromText(CharSequence text) {
//         if (text == null || text.length() < 1) {
//             return "";
//         }
// 
//         if (text.charAt(text.length() - 1) == "\n".charAt(0)) {
//             return removeTrailingNewlinesFromText(text.subSequence(0, text.length() - 1));
//         }
// 
//         return text;
//     }
// }
// 
// Path: app/src/main/java/com/hexforhn/hex/listener/ClickListener.java
// public interface ClickListener {
// 
//     /**
//      * Called when the view is clicked.
//      *
//      * @param v view that is clicked
//      * @param position of the clicked item
//      * @param isLongClick true if long click, false otherwise
//      */
//     void onClick(View v, int position, boolean isLongClick);
// }
// 
// Path: app/src/main/java/com/hexforhn/hex/viewmodel/CommentViewModel.java
// public class CommentViewModel {
// 
//     private final int mCommentCount;
//     private final String mText;
//     private final String mUser;
//     private final int mDepth;
//     private final Date mDate;
//     private Boolean mCollapsed;
//     private boolean mVisible;
// 
//     public CommentViewModel(String user, String text, int depth, int commentCount, Date date) {
//         this.mUser = user;
//         this.mText = text;
//         this.mDepth = depth;
//         this.mCommentCount = commentCount;
//         this.mDate = date;
//         this.mVisible = true;
//         this.mCollapsed = false;
//     }
// 
//     public String getText() {
//         return this.mText;
//     }
// 
//     public String getUser() {
//         return this.mUser;
//     }
// 
//     public int getDepth() {
//         return this.mDepth;
//     }
// 
//     public int getCommentCount() {
//         return this.mCommentCount;
//     }
// 
//     public String getRelativeTime() {
//         return DateUtils.getRelativeTimeSpanString(mDate.getTime(), System.currentTimeMillis(), 0)
//                 .toString();
//     }
// 
//     public boolean isVisible() {
//         return this.mVisible;
//     }
// 
//     public boolean isCollapsed() {
//         return this.mCollapsed;
//     }
// 
//     public void setVisible(boolean visible) {
//         this.mVisible = visible;
//     }
// 
//     public void toggleCollapsed() {
//         this.mCollapsed = !this.mCollapsed;
//     }
// 
//     public void setCollapsed(boolean collapsed) {
//         this.mCollapsed = collapsed;
//     }
// 
// }


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hexforhn.hex.R;
import com.hexforhn.hex.adapter.helper.CommentListManager;
import com.hexforhn.hex.adapter.helper.TextHelper;
import com.hexforhn.hex.listener.ClickListener;
import com.hexforhn.hex.viewmodel.CommentViewModel;
import java.util.List;

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CommentViewModel comment = mCommentListManager.getVisibleComments().get(position);
        renderCommentIntoView(comment, holder.mView);
    }

    @Override
    public int getItemCount() {
        return mCommentListManager.getVisibleComments().size();
    }

    @Override
    public void onClick(View view, int position, boolean isLongClick) {
        mCommentListManager.toggleCommentAtPosition(position);
        notifyDataSetChanged();
    }

    private void renderCommentIntoView(CommentViewModel comment, View commentView) {
        TextView usernameView = (TextView) commentView.findViewById(R.id.username);
        TextView relativeTimeView = (TextView) commentView.findViewById(R.id.relativeTime);
        final TextView textView = (TextView) commentView.findViewById(R.id.text);
        LinearLayout indentView = (LinearLayout) commentView.findViewById(R.id.indent);
        View childCommentsHidden = commentView.findViewById(R.id.hiddenChildCommentsMarker);

        usernameView.setText(comment.getUser());
        relativeTimeView.setText(comment.getRelativeTime());
// Path: app/src/main/java/com/hexforhn/hex/adapter/helper/CommentListManager.java
// public class CommentListManager {
//     private final List<CommentViewModel> mComments;
// 
//     public CommentListManager(List<CommentViewModel> comments) {
//         mComments = comments;
//     }
// 
//     public List<CommentViewModel> getVisibleComments() {
//         List<CommentViewModel> comments = new ArrayList<>();
//         int commentCount = mComments.size();
// 
//         for (int i = 0; i < commentCount; i++) {
//             CommentViewModel comment = mComments.get(i);
// 
//             if (comment.isVisible()) {
//                 comments.add(comment);
//             }
//         }
// 
//         return comments;
//     }
// 
//     public void toggleCommentAtPosition(int position) {
//         CommentViewModel commentToToggle = getVisibleComments().get(position);
//         int commentCount = mComments.size();
// 
//         commentToToggle.toggleCollapsed();
// 
//         int toggledCommentDepth = commentToToggle.getDepth();
//         boolean collapsedState = commentToToggle.isCollapsed();
// 
//         position = mComments.indexOf(commentToToggle);
// 
//         for (int i = position + 1; i < commentCount; i++) {
//             CommentViewModel comment = mComments.get(i);
// 
//             if (comment.getDepth() > toggledCommentDepth) {
//                 comment.setVisible(!collapsedState);
//                 comment.setCollapsed(collapsedState);
//             } else {
//                 break;
//             }
//         }
//     }
// 
// }
// 
// Path: app/src/main/java/com/hexforhn/hex/adapter/helper/TextHelper.java
// public class TextHelper {
// 
//     public static CharSequence removeTrailingNewlinesFromText(CharSequence text) {
//         if (text == null || text.length() < 1) {
//             return "";
//         }
// 
//         if (text.charAt(text.length() - 1) == "\n".charAt(0)) {
//             return removeTrailingNewlinesFromText(text.subSequence(0, text.length() - 1));
//         }
// 
//         return text;
//     }
// }
// 
// Path: app/src/main/java/com/hexforhn/hex/listener/ClickListener.java
// public interface ClickListener {
// 
//     /**
//      * Called when the view is clicked.
//      *
//      * @param v view that is clicked
//      * @param position of the clicked item
//      * @param isLongClick true if long click, false otherwise
//      */
//     void onClick(View v, int position, boolean isLongClick);
// }
// 
// Path: app/src/main/java/com/hexforhn/hex/viewmodel/CommentViewModel.java
// public class CommentViewModel {
// 
//     private final int mCommentCount;
//     private final String mText;
//     private final String mUser;
//     private final int mDepth;
//     private final Date mDate;
//     private Boolean mCollapsed;
//     private boolean mVisible;
// 
//     public CommentViewModel(String user, String text, int depth, int commentCount, Date date) {
//         this.mUser = user;
//         this.mText = text;
//         this.mDepth = depth;
//         this.mCommentCount = commentCount;
//         this.mDate = date;
//         this.mVisible = true;
//         this.mCollapsed = false;
//     }
// 
//     public String getText() {
//         return this.mText;
//     }
// 
//     public String getUser() {
//         return this.mUser;
//     }
// 
//     public int getDepth() {
//         return this.mDepth;
//     }
// 
//     public int getCommentCount() {
//         return this.mCommentCount;
//     }
// 
//     public String getRelativeTime() {
//         return DateUtils.getRelativeTimeSpanString(mDate.getTime(), System.currentTimeMillis(), 0)
//                 .toString();
//     }
// 
//     public boolean isVisible() {
//         return this.mVisible;
//     }
// 
//     public boolean isCollapsed() {
//         return this.mCollapsed;
//     }
// 
//     public void setVisible(boolean visible) {
//         this.mVisible = visible;
//     }
// 
//     public void toggleCollapsed() {
//         this.mCollapsed = !this.mCollapsed;
//     }
// 
//     public void setCollapsed(boolean collapsed) {
//         this.mCollapsed = collapsed;
//     }
// 
// }


// Path: app/src/main/java/com/hexforhn/hex/adapter/CommentListAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hexforhn.hex.R;
import com.hexforhn.hex.adapter.helper.CommentListManager;
import com.hexforhn.hex.adapter.helper.TextHelper;
import com.hexforhn.hex.listener.ClickListener;
import com.hexforhn.hex.viewmodel.CommentViewModel;
import java.util.List;


        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CommentViewModel comment = mCommentListManager.getVisibleComments().get(position);
        renderCommentIntoView(comment, holder.mView);
    }

    @Override
    public int getItemCount() {
        return mCommentListManager.getVisibleComments().size();
    }

    @Override
    public void onClick(View view, int position, boolean isLongClick) {
        mCommentListManager.toggleCommentAtPosition(position);
        notifyDataSetChanged();
    }

    private void renderCommentIntoView(CommentViewModel comment, View commentView) {
        TextView usernameView = (TextView) commentView.findViewById(R.id.username);
        TextView relativeTimeView = (TextView) commentView.findViewById(R.id.relativeTime);
        final TextView textView = (TextView) commentView.findViewById(R.id.text);
        LinearLayout indentView = (LinearLayout) commentView.findViewById(R.id.indent);
        View childCommentsHidden = commentView.findViewById(R.id.hiddenChildCommentsMarker);

        usernameView.setText(comment.getUser());
        relativeTimeView.setText(comment.getRelativeTime());
        textView.setText(TextHelper.removeTrailingNewlinesFromText(Html.fromHtml(comment.getText())));