package com.jeromyang.transmssion;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jeromyang.transmssion.event.OnlineEvent;
import com.jeromyang.transmssion.model.OnlineModel;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Jeromeyang on 2017/1/15.
 */

public class OnlineRecyclerAdapter extends RecyclerView.Adapter<OnlineRecyclerAdapter.ViewHolder>{

    private final List<OnlineModel> models = new ArrayList<>();


    public void setDataList(final List<OnlineModel> modelss, final int position){


        Observable.just(models)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<OnlineModel>>() {
                    @Override
                    public void call(List<OnlineModel> onlineModels) {
                        synchronized (models){
                            models.clear();
                        if (models.size() == modelss.size()){

                            models.addAll(modelss);
                            notifyItemChanged(position);
                        }else if (models.size() > modelss.size()){ //删除
                            notifyItemRemoved(position);
                            models.addAll(modelss);

                        }else if (models.size() < modelss.size()){
                            models.addAll(modelss);

                            notifyItemInserted(0);
                        }
                        notifyDataSetChanged();
                            EventBus.getDefault().post(new OnlineEvent(1,models.size(),0));
                    }}
                });

    }



    @Override
    public OnlineRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_online_list, parent, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.name.setText(models.get(position).getName());
        holder.ip_name.setText(Packet.intToIp(models.get(position).getSourceIp()));
    }



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

    public void addItem(OnlineModel onlineModel){
        models.add(0,onlineModel);
        Observable.just(onlineModel)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<OnlineModel>() {
                    @Override
                    public void call(OnlineModel onlineModel) {
                        notifyItemChanged(0,onlineModel);
                    }
                });

    }

    public void remove(int position){
        models.remove(position);

        Observable.just(position)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        notifyItemRemoved(integer);
                    }
                });

    }


    public class ViewHolder extends RecyclerView.ViewHolder{

        public TextView name;
        public TextView ip_name;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            ip_name = (TextView) itemView.findViewById(R.id.ip_name);

        }
    }
}

@@ -646,16 +646,17 @@
 wHolder%3E
+ 
 %7B%0A%0A    p
@@ -796,16 +796,17 @@
 osition)
+ 
 %7B%0A%0A%0A    
@@ -1100,16 +1100,17 @@
 (models)
+ 
 %7B%0A      
@@ -1135,87 +1135,99 @@
     
+if (
 models.
-clear();%0A                        if (models.size() == modelss.size())%7B%0A%0A
+size() == modelss.size()) %7B%0A                                models.clear();%0A    
     
@@ -1294,32 +1294,36 @@
                 
+    
 notifyItemChange
@@ -1351,33 +1351,38 @@
                 
+ 
-%7D
+   %7D 
 else if (models.
@@ -1388,17 +1388,17 @@
 .size() 
-%3E
+%3C
  modelss
@@ -1409,14 +1409,10 @@
 e())
+ 
 %7B
- //%E5%88%A0%E9%99%A4
 %0A   
@@ -1440,37 +1440,32 @@
     
-notifyItemRemoved(position);%0A
+    models.clear();%0A    
     
@@ -1504,33 +1504,89 @@
 ddAll(modelss);%0A
-%0A
+                                notifyItemInserted(0);%0A  
                 
@@ -1593,17 +1593,20 @@
         
-%7D
+  %7D 
 else if 
@@ -1612,33 +1612,33 @@
  (models.size() 
-%3C
+%3E
  modelss.size())
@@ -1637,17 +1637,23 @@
 .size())
-%7B
+ %7B //%E5%88%A0%E9%99%A4
 %0A       
@@ -1677,33 +1677,41 @@
     
-models.addAll(modelss
+    notifyItemRemoved(position
 );%0A
-%0A
     
@@ -1734,36 +1734,33 @@
         
-notifyItemInserted(0
+    models.clear(
 );%0A     
@@ -1770,33 +1770,63 @@
                 
-%7D
+        models.addAll(modelss);
 %0A               
@@ -1838,31 +1838,13 @@
     
-notifyDataSetChanged();
+    %7D
 %0A   
@@ -1913,16 +1913,17 @@
 Event(1,
+ 
 models.s
@@ -1928,16 +1928,17 @@
 .size(),
+ 
 0));%0A   
@@ -1954,17 +1954,42 @@
         
-%7D
+    %7D%0A                    
 %7D%0A      
@@ -2003,33 +2003,32 @@
    %7D);%0A%0A    %7D%0A%0A%0A
-%0A
     @Override%0A  
@@ -2509,17 +2509,16 @@
     %7D%0A%0A%0A
-%0A
     @Ove
@@ -2640,16 +2640,17 @@
 neModel)
+ 
 %7B%0A      
@@ -2660,24 +2660,25 @@
 odels.add(0,
+ 
 onlineModel)
@@ -2969,16 +2969,17 @@
 anged(0,
+ 
 onlineMo
@@ -3070,16 +3070,17 @@
 osition)
+ 
 %7B%0A      
@@ -3500,16 +3500,17 @@
 ewHolder
+ 
 %7B%0A%0A     

fix bug