package com.maxiee.attitude.ui.adapter;

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.attitude.R;
import com.maxiee.attitude.common.TimeUtils;
import com.maxiee.attitude.common.tagview.Tag;
import com.maxiee.attitude.common.tagview.TagView;
import com.maxiee.attitude.database.api.GetLabelsByEventKeyApi;
import com.maxiee.attitude.database.api.ThoughtCountByEventApi;
import com.maxiee.attitude.model.Event;
import com.maxiee.attitude.ui.EventDetailActivity;

import org.json.JSONException;

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()));

        try {
            holder.tvThoughtCount.setText(
                    String.valueOf(
                            new ThoughtCountByEventApi(holder.mContext, event.getmId()).exec()
                    )
            );
        } catch (JSONException e) {
            e.printStackTrace();
        }

        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());
                context.startActivity(intent);
            }
        });
    }

    @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();
        }
    }
}

@@ -1721,26 +1721,8 @@
 );%0A%0A
-        try %7B%0A    
     
@@ -1772,20 +1772,16 @@
         
-    
 String.v
@@ -1788,20 +1788,16 @@
 alueOf(%0A
-    
         
@@ -1899,18 +1899,10 @@
     
-   
- 
 )%0A
-    
     
@@ -1911,87 +1911,8 @@
   );
-%0A        %7D catch (JSONException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A%0A  

use new Api.