package com.runing.utilslib.adapter;

import androidx.annotation.NonNull;
import androidx.collection.SparseArrayCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Simplify {@link RecyclerView} Adapter
 * Simplifies the creation of {@link RecyclerView.Adapter}
 * <pre>
 * Contains the following features:
 * 1. Set the click event for item.
 *  {@link BaseRvAdapter#setOnItemClickListener(OnItemClickListener)}
 *  {@link BaseRvAdapter#setOnItemLongClickListener(OnItemLongClickListener)}
 * 2. Support for adding multiple View types. {@link MultiType}
 * </pre>
 * Created by runing on 2016/10/21.
 */

public abstract class BaseRvAdapter extends RecyclerView.Adapter<BaseRvAdapter.ViewHolder> {

  private final List<?> mData;

  private OnItemClickListener mOnItemClickListener;
  private OnItemLongClickListener mOnItemLongClickListener;

  private int mItemId;
  private MultiType mMultiType;

  /**
   * Multiple item types are supported
   */
  public interface MultiType {
    /**
     * Returns the corresponding layout id based on position.
     * The layout id of the item will be viewType
     *
     * @param position position
     * @return The layout id of item
     */
    int getItemLayoutId(int position);
  }

  /**
   * @param mData     bean data
   * @param multiType MultiType callback
   */
  protected BaseRvAdapter(List<?> mData, @NonNull MultiType multiType) {
    this.mMultiType = multiType;
    this.mData = mData;
  }

  /**
   * @param mData   bean data
   * @param mItemId The layout id of the item
   */
  protected BaseRvAdapter(List<?> mData, int mItemId) {
    this.mItemId = mItemId;
    this.mData = mData;
  }

  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.mOnItemClickListener = onItemClickListener;
  }

  public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
    this.mOnItemLongClickListener = onItemLongClickListener;
  }

  @Override
  public int getItemViewType(int position) {
    if (mMultiType != null) {
      return mMultiType.getItemLayoutId(position);
    }
    return super.getItemViewType(position);
  }

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

  protected Object getItem(int position) {
    return mData.get(position);
  }

  @Override
  public final long getItemId(int position) {
    return position;
  }

  @Override
  @NonNull
  public final ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    final int layoutId;
    if (mMultiType == null) {
      layoutId = mItemId;
    } else {
      layoutId = viewType;
    }
    ViewHolder holder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
        layoutId, parent, false));
    setClickListener(holder);
    return holder;
  }

  private void setClickListener(final ViewHolder holder) {
    if (mOnItemClickListener != null) {
      holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          mOnItemClickListener.onItemClick(holder.itemView, holder.getAdapterPosition());
        }
      });
    }
    if (mOnItemLongClickListener != null) {
      holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
          mOnItemLongClickListener.onItemLongClick(holder.itemView,
              holder.getAdapterPosition());
          return true;
        }
      });
    }
  }

  @Override
  public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
    if (payloads.isEmpty()) {
      onBindViewHolder(holder, position);
    }
  }

  public interface OnItemClickListener {
    void onItemClick(View view, int position);
  }

  public interface OnItemLongClickListener {
    void onItemLongClick(View view, int position);
  }

  protected static final class ViewHolder extends RecyclerView.ViewHolder {

    private SparseArrayCompat<View> views = new SparseArrayCompat<>();

    private ViewHolder(View itemView) {
      super(itemView);
    }

    public <T extends View> T getView(int id) {
      View view = views.get(id);
      if (view == null) {
        view = itemView.findViewById(id);
        views.put(id, view);
      }
      @SuppressWarnings("unchecked")
      T t = (T)view;
      return t;
    }
  }

}

@@ -49,241 +49,277 @@
 roid
-x.annotation.NonNull;%0Aimport androidx.collection.SparseArrayCompat;%0Aimport androidx.recyclerview.widget.RecyclerView;%0Aimport android.view.LayoutInflater;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;%0A%0Aimport java.util.List
+.view.LayoutInflater;%0Aimport android.view.View;%0Aimport android.view.ViewGroup;%0Aimport android.widget.FrameLayout;%0A%0Aimport java.util.List;%0A%0Aimport androidx.annotation.NonNull;%0Aimport androidx.collection.SparseArrayCompat;%0Aimport androidx.recyclerview.widget.RecyclerView
 ;%0A%0A/
@@ -2742,16 +2742,20 @@
    %7D
+%0A   
  else %7B%0A
     
@@ -2746,24 +2746,24 @@
 %0A    else %7B%0A
-
       layout
@@ -3058,32 +3058,45 @@
 = null) %7B%0A      
+getClickView(
 holder.itemView.
@@ -3090,24 +3090,25 @@
 der.itemView
+)
 .setOnClickL
@@ -3362,24 +3362,37 @@
 ll) %7B%0A      
+getClickView(
 holder.itemV
@@ -3394,16 +3394,17 @@
 itemView
+)
 .setOnLo
@@ -3677,24 +3677,284 @@
 %0A    %7D%0A  %7D%0A%0A
+  private static View getClickView(View itemView) %7B%0A    if (itemView instanceof FrameLayout) %7B%0A      final ViewGroup view = (ViewGroup) itemView;%0A      if (view.getChildCount() != 0) %7B%0A        return view.getChildAt(0);%0A      %7D%0A    %7D%0A    return itemView;%0A  %7D%0A%0A
   @Override%0A
@@ -4779,16 +4779,16 @@
 ecked%22)%0A
-
       T 
@@ -4794,16 +4794,17 @@
  t = (T)
+ 
 view;%0A  

update BaseRvAdapter.java (update OnItemClickListener).