package org.peace.savingtracker.ui.history;

import android.content.Context;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.jakewharton.rxbinding.view.RxView;
import java.util.Collections;
import java.util.List;
import org.peace.savingtracker.R;
import org.peace.savingtracker.model.Expense;
import org.peace.savingtracker.model.ExpenseAPI;
import org.peace.savingtracker.model.ExpenseHelper;
import org.peace.savingtracker.ui.base.BaseActivity;
import org.peace.savingtracker.ui.widget.ProgressDialog;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by peacepassion on 15/11/10.
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryViewHolder> {

  private Context context;
  private ExpenseAPI expenseAPI;
  private List<Expense> expenses;
  private ProgressDialog progressDialog;

  public HistoryAdapter(Context context, ExpenseAPI expenseAPI) {
    this.context = context;
    this.expenseAPI = expenseAPI;
    progressDialog = new ProgressDialog(context);
    expenses = Collections.emptyList();
    loadData();
  }

  public void updateDataSet() {
    loadData();
  }

  @Override public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View root = inflater.inflate(R.layout.item_expense_history, parent, false);
    return new HistoryViewHolder(root);
  }

  @Override public void onBindViewHolder(HistoryViewHolder holder, int position) {
    Expense expense = expenses.get(position);
    holder.userName.setText(expense.getName());
    holder.date.setText(ExpenseHelper.getMonthDay(expense));
    holder.category.setText(expense.getCategory());
    holder.amount.setText(String.valueOf(expense.getValue()));
    RxView.clickEvents(holder.edit).subscribe(viewClickEvent -> {
      Snackbar.make(viewClickEvent.view(), "not ready yet", Snackbar.LENGTH_SHORT).show();
    });
    RxView.clickEvents(holder.delete).subscribe(viewClickEvent -> {
      expenseAPI.delete(expense)
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .compose(((BaseActivity) context).bindToLifecycle())
          .subscribe(new Subscriber<Void>() {
            @Override public void onStart() {
              progressDialog.show();
            }

            @Override public void onCompleted() {
              progressDialog.dismiss();
            }

            @Override public void onError(Throwable e) {
              progressDialog.dismiss();
              ((BaseActivity) context).popHint(e.getMessage());
            }

            @Override public void onNext(Void aVoid) {
              expenses.remove(expense);
              notifyDataSetChanged();
            }
          });
    });
  }

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

  private void loadData() {
    expenseAPI.queryAll()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(((BaseActivity) context).bindToLifecycle())
        .subscribe(new Subscriber<List<Expense>>() {
          @Override public void onStart() {
            progressDialog.show();
          }

          @Override public void onCompleted() {
            progressDialog.dismiss();
          }

          @Override public void onError(Throwable e) {
            progressDialog.dismiss();
            ((BaseActivity) context).popHint(e.getMessage());
          }

          @Override public void onNext(List<Expense> expenses) {
            HistoryAdapter.this.expenses = expenses;
            notifyDataSetChanged();
          }
        });
  }
}

@@ -257,16 +257,70 @@
 wGroup;%0A
+import com.afollestad.materialdialogs.MaterialDialog;%0A
 import c
@@ -2217,24 +2217,381 @@
 kEvent -%3E %7B%0A
+      alertDelete(expense);%0A    %7D);%0A  %7D%0A%0A  private void alertDelete(Expense expense) %7B%0A    new MaterialDialog.Builder(context).title(%22%E7%A1%AE%E5%AE%9A%E5%88%A0%E9%99%A4%22)%0A        .content(%22%E7%A1%AE%E5%AE%9A%E5%88%A0%E9%99%A4%E8%AF%A5%E7%AC%94%E8%B4%A6%E5%8D%95?%22)%0A        .positiveText(%22%E7%A1%AE%E5%AE%9A%22)%0A        .negativeText(%22%E5%8F%96%E6%B6%88%22)%0A        .callback(new MaterialDialog.ButtonCallback() %7B%0A          @Override public void onPositive(MaterialDialog dialog) %7B%0A      
       expens
@@ -2611,16 +2611,22 @@
 xpense)%0A
+      
         
@@ -2659,32 +2659,38 @@
 io())%0A          
+      
 .observeOn(Andro
@@ -2708,32 +2708,38 @@
 s.mainThread())%0A
+      
           .compo
@@ -2787,32 +2787,38 @@
 le())%0A          
+   
+   
 .subscribe(new S
@@ -2841,32 +2841,38 @@
 ) %7B%0A            
+     
+ 
 @Override public
@@ -2895,32 +2895,38 @@
 %7B%0A              
+      
 progressDialog.s
@@ -2936,35 +2936,47 @@
 ();%0A            
+ 
-%7D%0A%0A
+     %7D%0A%0A      
             @Ove
@@ -3015,32 +3015,38 @@
 %7B%0A              
+     
+ 
 progressDialog.d
@@ -3059,35 +3059,47 @@
 ();%0A            
-%7D%0A%0A
+      %7D%0A%0A      
             @Ove
@@ -3145,32 +3145,38 @@
 %7B%0A              
+   
+   
 progressDialog.d
@@ -3177,32 +3177,38 @@
 alog.dismiss();%0A
+      
               ((
@@ -3247,32 +3247,38 @@
 .getMessage());%0A
+      
             %7D%0A%0A 
@@ -3280,32 +3280,38 @@
  %7D%0A%0A            
+      
 @Override public
@@ -3347,24 +3347,30 @@
             
+      
 expenses.rem
@@ -3389,32 +3389,38 @@
 ;%0A              
+    
+  
 notifyDataSetCha
@@ -3431,32 +3431,38 @@
 ();%0A            
+      
 %7D%0A          %7D);%0A
@@ -3461,17 +3461,55 @@
     
+ 
-%7D);%0A    %7D
+     %7D);%0A          %7D%0A        %7D)%0A        .show(
 );%0A 

add delete alert dialog