package ca.ualberta.cmput301f17t08.habitrabbit;

/**
 * Created by yuxuanzhao on 2017-11-07.
 */


import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class feedAdapter extends RecyclerView.Adapter<feedAdapter.ViewHolder> {
    private  ArrayList<HabitEvent> habitEvents;
    public String username;

    // Provide a direct reference to each of the views within a data item
    // Used to cache the views within the item layout for fast access

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView feedName;
        public TextView numLike;
        public TextView feedComment;
        public TextView userNameView;
        public Button likeButton;
        public TextView feedDate;
        public ImageView imagePreview;


        public ViewHolder(View feedView) {
            // Stores the itemView in a public final member variable that can be used
            // to access the context from any ViewHolder instance.
            super(feedView);
            feedName = feedView.findViewById(R.id.feed_name);
            numLike = feedView.findViewById(R.id.num_like);
            likeButton = feedView.findViewById(R.id.like_button);
            feedComment = feedView.findViewById(R.id.comment);
            userNameView = feedView.findViewById(R.id.feed_username);
            feedDate = feedView.findViewById(R.id.feed_time);
            imagePreview = feedView.findViewById(R.id.feed_image);




        }
    }
    public feedAdapter(String username,ArrayList<HabitEvent> habitEvents) {
        this.habitEvents = habitEvents;
        this.username =  username;
    }
    @Override
    public feedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View feedView = inflater.inflate(R.layout.feed_post, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(feedView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(feedAdapter.ViewHolder viewHolder, final int position) {
        viewHolder.feedName.setText(habitEvents.get(position).getHabit().getName());
        viewHolder.feedComment.setText(habitEvents.get(position).getComment());
        viewHolder.feedDate.setText(habitEvents.get(position).getDateCompleted().toString());
        viewHolder.numLike.setText(Integer.toString(habitEvents.get(position).getLikeCount())+" likes");
        viewHolder.userNameView.setText(username);

        Bitmap userImage = habitEvents.get(position).getPicture();
        if (userImage != null){
            viewHolder.imagePreview.setImageBitmap(userImage);
        }

        viewHolder.likeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                habitEvents.get(position).like(LoginManager.getInstance().getCurrentUser().getUsername());
            }
        });


    }
    @Override
    public int getItemCount() {
        return 1;
    }




}
@@ -91,17 +91,16 @@
 7.%0A */%0A%0A
-%0A
 import a
@@ -813,24 +813,47 @@
 ew feedName;
+ //the name of the feed
 %0A        pub
@@ -873,16 +873,44 @@
 numLike;
+ // the num of likes in feed
 %0A       
@@ -938,16 +938,38 @@
 Comment;
+//comment on each feed
 %0A       
@@ -998,16 +998,59 @@
 ameView;
+ // the username of user who posts the feed
 %0A       
@@ -1075,16 +1075,35 @@
 eButton;
+ // the like button
 %0A       
@@ -1128,16 +1128,48 @@
 eedDate;
+// the date that feed is created
 %0A       
@@ -1199,16 +1199,51 @@
 Preview;
+// the image attached with the feed
 %0A%0A%0A     
@@ -1912,20 +1912,16 @@
 image);%0A
-%0A%0A%0A%0A
         
@@ -2047,43 +2047,129 @@
 nts;
-%0A        this.username =  username;
+ //get the habitsEvents list passed in%0A        this.username =  username;//get the username passed in from activity class
 %0A   
@@ -2318,16 +2318,33 @@
 ntext();
+//get the context
 %0A       
@@ -2399,16 +2399,48 @@
 ontext);
+//initialize the layout inflater
 %0A%0A      
@@ -2780,24 +2780,96 @@
 position) %7B%0A
+        //change the text and appearance of each elements on the layout%0A
         view
@@ -3267,24 +3267,89 @@
 (username);%0A
+        //get the image the user uploaded, set the image if exist
 %0A        Bit
@@ -3505,24 +3505,68 @@
 ;%0A        %7D%0A
+        //functionality of likes to the post
 %0A        vie
@@ -3832,18 +3832,16 @@
     %7D);%0A
-%0A%0A
     %7D%0A  
@@ -3908,13 +3908,9 @@
 ;%0A    %7D%0A
-%0A%0A%0A%0A
 %7D

add the comment and fix the code style of feedAdapter