package com.zzzmode.appopsx.ui.main.usagestats;

import android.content.Intent;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.zzzmode.appopsx.R;
import com.zzzmode.appopsx.common.OtherOp;
import com.zzzmode.appopsx.ui.core.Helper;
import com.zzzmode.appopsx.ui.core.LocalImageLoader;
import com.zzzmode.appopsx.ui.model.AppInfo;
import com.zzzmode.appopsx.ui.model.OpEntryInfo;
import com.zzzmode.appopsx.ui.permission.AppPermissionActivity;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zl on 2017/8/16.
 */
 class UsageStatsAdapter extends RecyclerView.Adapter<UsageStatsAdapter.ViewHolder> implements
    OnClickListener,OnCheckedChangeListener{

  private static final String TAG = "UsageStatsAdapter";

  private List<Pair<AppInfo, OpEntryInfo>> mDatas=new ArrayList<>();

  void showItems(List<Pair<AppInfo, OpEntryInfo>> value){
    mDatas.clear();
    if(value != null) {
      mDatas.addAll(value);
    }
    notifyDataSetChanged();
  }

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

  @Override
  public void onBindViewHolder(ViewHolder holder, int position) {

    Pair<AppInfo, OpEntryInfo> pair = mDatas.get(position);

    LocalImageLoader.load(holder.imgIcon, pair.first);

    holder.tvName.setText(pair.first.appName);
    holder.imgPerm.setImageResource(pair.second.icon);

    long time = pair.second.opEntry.getTime();
    if (time > 0) {
      holder.tvLastTime.setText(DateUtils
          .getRelativeTimeSpanString(time, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
              DateUtils.FORMAT_ABBREV_TIME));
    } else {
      holder.tvLastTime.setText(R.string.never_used);
    }
    if(OtherOp.isSupportCount()){
      holder.tvPermName.setText(holder.tvPermName.getResources().getString(R.string.perms_count,pair.second.opPermsLab,pair.second.opEntry.getAllowedCount()));
    }else {
      holder.tvPermName.setText(pair.second.opPermsLab);
    }
    holder.itemView.setTag(holder);
    holder.itemView.setOnClickListener(this);

    holder.switchCompat.setTag(holder);

    holder.switchCompat.setOnCheckedChangeListener(null);
    holder.switchCompat.setChecked(pair.second.isAllowed());
    holder.switchCompat.setOnCheckedChangeListener(this);

  }

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

  @Override
  public void onClick(View view) {
    Object tag = view.getTag();
    if(tag instanceof ViewHolder){
      ViewHolder holder = ((ViewHolder) tag);
      Pair<AppInfo, OpEntryInfo> pair = mDatas.get(holder.getAdapterPosition());

      Intent intent = new Intent(view.getContext(), AppPermissionActivity.class);
      intent.putExtra(AppPermissionActivity.EXTRA_APP,pair.first);
      view.getContext().startActivity(intent);
    }
  }

  @Override
  public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    Object tag = compoundButton.getTag();
    if(tag instanceof ViewHolder){
      ViewHolder holder = ((ViewHolder) tag);
      Pair<AppInfo, OpEntryInfo> pair = mDatas.get(holder.getAdapterPosition());

      Helper.setMode(compoundButton.getContext(),pair.first.packageName,pair.second,b)
          .subscribe();

    }
  }

  static class ViewHolder extends RecyclerView.ViewHolder{

    ImageView imgPerm;
    ImageView imgIcon;
    TextView tvName;
    TextView tvLastTime;
    TextView tvPermName;
    SwitchCompat switchCompat;


    ViewHolder(View itemView) {
      super(itemView);
      imgIcon = (ImageView) itemView.findViewById(R.id.app_icon);
      tvName = (TextView) itemView.findViewById(R.id.app_name);
      switchCompat = (SwitchCompat) itemView.findViewById(R.id.switch_compat);
      tvLastTime = (TextView) itemView.findViewById(R.id.perm_last_time);
      tvPermName = (TextView)itemView.findViewById(R.id.perm_name);
      imgPerm = (ImageView) itemView.findViewById(R.id.img_group);
    }
  }
}

@@ -869,16 +869,148 @@
 tivity;%0A
+import io.reactivex.Scheduler;%0Aimport io.reactivex.android.schedulers.AndroidSchedulers;%0Aimport io.reactivex.schedulers.Schedulers;%0A
 import j
@@ -3850,16 +3850,109 @@
 cond,b)%0A
+          .observeOn(Schedulers.io())%0A          .subscribeOn(AndroidSchedulers.mainThread())%0A
         

fix NetworkOnMainThreadException