package com.morihacky.android.rxjava.pagination;

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.TextView;
import com.morihacky.android.rxjava.R;
import com.morihacky.android.rxjava.rxbus.RxBus;
import java.util.ArrayList;
import java.util.List;

class PaginationAutoAdapter
      extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int ITEM_LOG = 0;
    private static final int ITEM_BTN = 1;

    private final List<String> _items = new ArrayList<>();
    private final RxBus _bus;

    PaginationAutoAdapter(RxBus bus) {
        _bus = bus;
    }

    void addItems(List<String> items) {
        _items.addAll(items);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == _items.size()) {
            return ITEM_BTN;
        }

        return ITEM_LOG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ITEM_BTN:
                return ItemBtnViewHolder.create(parent);
            default:
                return ItemLogViewHolder.create(parent);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case ITEM_LOG:
                ((ItemLogViewHolder) holder).bindContent(_items.get(position));
                return;
            case ITEM_BTN:
                ((ItemBtnViewHolder) holder).bindContent(_bus);
        }
    }

    @Override
    public int getItemCount() {
        return _items.size() + 1; // add 1 for paging button
    }

    private static class ItemLogViewHolder extends RecyclerView.ViewHolder {
        ItemLogViewHolder(View itemView) {
            super(itemView);
        }

        static ItemLogViewHolder create(ViewGroup parent) {
            return new ItemLogViewHolder(LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.item_log, parent, false));
        }

        void bindContent(String content) {
            ((TextView) itemView).setText(content);
        }
    }

    static class ItemBtnViewHolder extends RecyclerView.ViewHolder {
        ItemBtnViewHolder(View itemView) {
            super(itemView);
        }

        static ItemBtnViewHolder create(ViewGroup parent) {
            return new ItemBtnViewHolder(LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.item_btn, parent, false));
        }

        void bindContent(RxBus bus) {
            ((Button) itemView).setText(R.string.btn_demo_pagination_more);
            itemView.setOnClickListener(v -> bus.send(new ItemBtnViewHolder.PageEvent()));
        }

        static class PageEvent {
        }
    }
}

@@ -187,38 +187,8 @@
 up;%0A
-import android.widget.Button;%0A
 impo
@@ -492,51 +492,8 @@
 = 0;
-%0A    private static final int ITEM_BTN = 1;
 %0A%0A  
@@ -654,498 +654,106 @@
     
-void addItems(List%3CString%3E items) %7B%0A        _items.addAll(items);%0A    %7D%0A%0A    @Override%0A    public int getItemViewType(int position) %7B%0A        if (position == _items.size()) %7B%0A            return ITEM_BTN;%0A        %7D%0A%0A        return ITEM_LOG;%0A    %7D%0A%0A    @Override%0A    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) %7B%0A        switch (viewType) %7B%0A            case ITEM_BTN:%0A                return ItemBtnViewHolder.create(parent);%0A            default:%0A        
+@Override%0A    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) %7B%0A
     
@@ -793,34 +793,24 @@
 te(parent);%0A
-        %7D%0A
     %7D%0A%0A    @
@@ -903,88 +903,8 @@
 ) %7B%0A
-        switch (getItemViewType(position)) %7B%0A            case ITEM_LOG:%0A        
     
@@ -979,128 +979,96 @@
     
+%7D%0A%0A
     
-        return;%0A            case ITEM_BTN:%0A                ((
+@Override%0A    public int get
 Item
-Btn
 View
-Holder) holder).bindContent(_bus);%0A        %7D
+Type(int position) %7B%0A        return ITEM_LOG;
 %0A   
@@ -1149,164 +1149,53 @@
 ze()
- + 1; // add 1 for paging button%0A    %7D%0A%0A    private static class ItemLogViewHolder extends RecyclerView.ViewHolder %7B%0A        ItemLogViewHolder(View
+;%0A    %7D%0A%0A    void addItems(List%3CString%3E
  item
-View
+s
 ) %7B%0A
@@ -1206,249 +1206,30 @@
     
-    super(itemView);%0A        %7D%0A%0A        static ItemLogViewHolder create(ViewGroup parent) %7B%0A            return new ItemLogViewHolder(LayoutInflater.from(parent.getContext())%0A                  .inflate(R.layout.item_log, parent, false)
+_items.addAll(items
 );%0A
-    
     
@@ -1239,119 +1239,15 @@
     
-    void bindContent(String content) %7B%0A            ((TextView) itemView).setText(content);%0A        %7D%0A    %7D%0A%0A   
+private
  sta
@@ -1256,27 +1256,27 @@
 c class Item
-Btn
+Log
 ViewHolder e
@@ -1273,16 +1273,26 @@
 ewHolder
+%0A         
  extends
@@ -1326,27 +1326,27 @@
         Item
-Btn
+Log
 ViewHolder(V
@@ -1416,27 +1416,27 @@
  static Item
-Btn
+Log
 ViewHolder c
@@ -1480,35 +1480,35 @@
  return new Item
-Btn
+Log
 ViewHolder(Layou
@@ -1543,16 +1543,45 @@
 text())%0A
+                             
         
@@ -1613,19 +1613,19 @@
 ut.item_
-btn
+log
 , parent
@@ -1671,25 +1671,30 @@
 Content(
-RxBus bus
+String content
 ) %7B%0A    
@@ -1707,14 +1707,16 @@
   ((
-Button
+TextView
 ) it
@@ -1735,178 +1735,17 @@
 ext(
-R.string.btn_demo_pagination_more);%0A            itemView.setOnClickListener(v -%3E bus.send(new ItemBtnViewHolder.PageEvent()));%0A        %7D%0A%0A        static class PageEvent %7B
+content);
 %0A   

remove moar btn