package com.maxiee.heartbeat.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.maxiee.heartbeat.R;
import com.maxiee.heartbeat.common.TimeUtils;
import com.maxiee.heartbeat.database.api.ThoughtCountByEventApi;
import com.maxiee.heartbeat.model.Event;
import com.maxiee.heartbeat.ui.EventDetailActivity;

import java.util.ArrayList;

/**
 * Created by maxiee on 15-6-12.
 */
public class EventListAdapter extends RecyclerView.Adapter<EventListAdapter.ViewHolder> {

    private static final String TAG = EventListAdapter.class.getSimpleName();

    private ArrayList<Event> mEventList;

    public EventListAdapter(ArrayList<Event> mEventList) {
        this.mEventList = mEventList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_event_card, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        Event event = mEventList.get(position);

        holder.tvEvent.setText(event.getmEvent());
        holder.tvTime.setText(
                TimeUtils.parseTime(
                        holder.mView.getContext(),
                        event.getTimestamp()));

        holder.tvThoughtCount.setText(
                String.valueOf(
                        new ThoughtCountByEventApi(holder.mContext, event.getmId()).exec()
                )
        );

        Log.d(TAG, "事件列表项目");
        Log.d(TAG, "编号:" + String.valueOf(event.getmId()));
        Log.d(TAG, "名称:" + event.getmEvent());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent intent = new Intent(context, EventDetailActivity.class);
                intent.putExtra(
                        EventDetailActivity.EXTRA_NAME,
                        mEventList.get(position).getmId());
                ((Activity) context).startActivityForResult(intent, EventDetailActivity.EVENT_DETAIL);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvEvent, tvTime,tvThoughtCount;
        public final View mView;
        public Context mContext;

        public ViewHolder(View itemView) {
            super(itemView);
            this.mView = itemView;
            this.tvEvent = (TextView) itemView.findViewById(R.id.tv_event);
            this.tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            this.tvThoughtCount = (TextView) itemView.findViewById(R.id.tv_thought_count);
            mContext = itemView.getContext();
        }
    }
}

@@ -178,33 +178,8 @@
 ew;%0A
-import android.util.Log;%0A
 impo
@@ -293,18 +293,84 @@
 get.
-TextView;%0A
+ImageView;%0Aimport android.widget.TextView;%0A%0Aimport com.bumptech.glide.Glide;
 %0Aimp
@@ -443,16 +443,80 @@
 eUtils;%0A
+import com.maxiee.heartbeat.database.api.GetImageByEventKeyApi;%0A
 import c
@@ -1044,32 +1044,132 @@
 entList;%0A    %7D%0A%0A
+    public void setData(ArrayList%3CEvent%3E mEventList) %7B%0A        this.mEventList = mEventList;%0A    %7D%0A%0A
     @Override%0A  
@@ -1951,24 +1951,26 @@
         );%0A%0A
+//
         Log.
@@ -1987,16 +1987,18 @@
 %E5%88%97%E8%A1%A8%E9%A1%B9%E7%9B%AE%22);%0A
+//
         
@@ -2045,24 +2045,26 @@
 getmId()));%0A
+//
         Log.
@@ -2639,16 +2639,447 @@
      %7D);
+%0A%0A        String imagePath = new GetImageByEventKeyApi(holder.mContext, event.getmId()).exec();%0A%0A        if (imagePath != null) %7B%0A            holder.mCoverImage.setVisibility(View.VISIBLE);%0A            Glide.with(holder.mContext)%0A                    .load(imagePath)%0A                    .centerCrop()%0A                    .into(holder.mCoverImage);%0A        %7D else %7B%0A            holder.mCoverImage.setVisibility(View.GONE);%0A        %7D
 %0A    %7D%0A%0A
@@ -3348,16 +3348,54 @@
 Context;
+%0A        public ImageView mCoverImage;
 %0A%0A      
@@ -3732,24 +3732,108 @@
 ght_count);%0A
+            this.mCoverImage = (ImageView) itemView.findViewById(R.id.image_cover);%0A
             

add ImageView.