package com.maxiee.heartbeat.ui.adapter;

import android.content.Context;
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.model.Thoughts;
import com.maxiee.heartbeat.ui.dialog.EditThoughtDialog;


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

    private Thoughts mThoughtList;

    public ThoughtTimeaxisAdapter(Thoughts thoughtList) {
        mThoughtList = thoughtList;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        String order;
        switch (position) {
            case 0:
                order = holder.mContext.getString(R.string.firtime);
                break;
            case 1:
                order = holder.mContext.getString(R.string.sectime);
                break;
            default:
                order = String.valueOf(position + 1) + ".";
                break;
        }
        holder.tvOrder.setText(order);

        long time = mThoughtList.get(position).getTimeStamp();
        holder.tvThought.setText(mThoughtList.get(position).getThought());
        holder.tvTime.setText(TimeUtils.parseTime(holder.mContext, time));

        holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                EditThoughtDialog dialog = new EditThoughtDialog(
                        holder.mContext,
                        mThoughtList.get(position).getKey(),
                        mThoughtList.get(position).getThought()
                );
                dialog.setOnAddFinishedListener(new EditThoughtDialog.OnAddFinishedListener() {
                    @Override
                    public void update(String newThought) {
                        mThoughtList.get(position).setThought(newThought);
                        holder.tvThought.setText(newThought);
                    }

                    @Override
                    public void remove() {
                        mThoughtList.remove(position);
                        notifyDataSetChanged();
                    }
                });
                dialog.show();
                return true;
            }
        });
        Log.d("增删测试",
                String.valueOf(mThoughtList.get(position).getKey()) + ": " +
                mThoughtList.get(position).getThought()
        );
    }

    @Override
    public int getItemCount() {
        return mThoughtList.length();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvOrder, tvThought, tvTime;
        public final View mView;
        public Context mContext;

        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            mContext = itemView.getContext();
            tvOrder = (TextView) itemView.findViewById(R.id.tv_order);
            tvThought = (TextView) itemView.findViewById(R.id.tv_thought);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
        }
    }
}

@@ -67,16 +67,103 @@
 ontext;%0A
+import android.content.SharedPreferences;%0Aimport android.preference.PreferenceManager;%0A
 import a
@@ -197,24 +197,24 @@
 cyclerView;%0A
-
 import andro
@@ -1190,16 +1190,66 @@
  order;%0A
+        if (holder.sortingType.equals(%220%22)) %7B%0A    
         
@@ -1284,16 +1284,20 @@
     
+    
 case 0:%0A
     
@@ -1292,16 +1292,444 @@
 case 0:%0A
+                    order = holder.mContext.getString(R.string.firtime);%0A                    break;%0A                case 1:%0A                    order = holder.mContext.getString(R.string.sectime);%0A                    break;%0A                default:%0A                    order = String.valueOf(position + 1) + %22.%22;%0A                    break;%0A            %7D%0A        %7D else %7B%0A            if (position == mThoughtList.length() - 1) %7B%0A
         
@@ -1805,38 +1805,59 @@
     
-    break;%0A            case 1:
+%7D else if (position == mThoughtList.length() - 2) %7B
 %0A   
@@ -1938,39 +1938,16 @@
     
-    break;%0A            default:
+%7D else %7B
 %0A   
@@ -1978,32 +1978,56 @@
 ing.valueOf(
+mThoughtList.length() - 
 position
  + 1) + %22.%22;
@@ -2006,36 +2006,32 @@
 gth() - position
- + 1
 ) + %22.%22;%0A       
@@ -2031,34 +2031,25 @@
             
-  
-  break;
+%7D
 %0A        %7D%0A 
@@ -3794,16 +3794,51 @@
 Context;
+%0A        public String sortingType;
 %0A%0A      
@@ -4117,32 +4117,32 @@
 id.tv_thought);%0A
-
             tvTi
@@ -4190,24 +4190,182 @@
 d.tv_time);%0A
+            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);%0A            sortingType = sp.getString(%22time_axis_sorting%22, %220%22);%0A
         %7D%0A  

read SP to set index by time_axis_sorting order.