package com.naz.sdkdemo.weight;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.val;


/**
 * @Description:
 * @Date: 2020/8/21 下午10:00
 */
public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewAdapter.BaseViewHolder>{
    private final List<T> dataList = new ArrayList<>();
    private OnItemClickListener itemClickListener;
    private Map<Integer, View> itemViewMap = new HashMap<>();

    protected Context context;

    public BaseRecyclerViewAdapter(Context context){
        this.context = context;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        int layoutId = getItemLayoutId(viewType);
        View rootView = null;
        if (layoutId != 0) {
            rootView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        }else {
            rootView = getItemView(viewType);
        }
        BaseViewHolder holder = new BaseViewHolder(rootView);
        setupViewHolder(holder, viewType);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != itemClickListener){
                    itemClickListener.onItemClick(position);
                }
            }
        });
        itemViewMap.put(position, holder.itemView);
        bindViewHolder(holder, getItemDataByPosition(position), position, getItemViewType(position));
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

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

    public List<T> getDataList() {
        return dataList;
    }

    public T getItemDataByPosition(int position) {
        if (position < 0 || position > dataList.size() - 1) {
            return null;
        }
        return dataList.get(position);
    }


    protected abstract int getItemLayoutId(int viewType);

    protected View getItemView(int viewType){
        return null;
    }

    public abstract void bindViewHolder(BaseViewHolder holder, T data, int position, int viewType);

    public void setupViewHolder(BaseViewHolder holder, int viewType){

    }

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

    public void startActivity(Intent intent){
        if (null != context){
            context.startActivity(intent);
        }
    }

    public void startActivityForResult(Intent intent, int requestCode){
        if (context instanceof Activity){
            ((Activity)context).startActivityForResult(intent, requestCode);
        }
    }

    public View findItemView(int position) {
        return itemViewMap.get(position);
    }

    public int getColor(int resId) {
        return context.getResources().getColor(resId);
    }

    public void refreshData(List<T> list) {
        int size = dataList.size();
        dataList.clear();
        itemViewMap.clear();
//        notifyItemRangeRemoved(0, size);
        if (null == list || list.isEmpty()) {
            return;
        }
        dataList.addAll(list);
//        size = dataList.size();
//        notifyItemRangeInserted(0, size);
        notifyDataSetChanged();
    }

    public void addData(List<T> list) {
        if (null == list || list.isEmpty()) {
            return;
        }
        int oldSize = dataList.size();
        dataList.addAll(list);
        int newSize = dataList.size();
        notifyItemRangeInserted(oldSize, newSize - oldSize);
    }

    public void addData(List<T> list, int position) {
        if (null == list || list.isEmpty()) {
            return;
        }
        int finalPosition = position;
        if (position < 0) {
            finalPosition = 0;
        } else if (position > dataList.size() - 1) {
            finalPosition = dataList.size() - 1;
        }
        int oldSize = dataList.size();
        dataList.addAll(finalPosition, list);
        int newSize = dataList.size();
        notifyItemRangeInserted(oldSize, newSize - oldSize);
        notifyItemRangeChanged(finalPosition, newSize - finalPosition - 1);
    }

    public void removeData(int position) {
        if (position < 0 || position > dataList.size() - 1) {
            return;
        }
        itemViewMap.remove(position);
        dataList.remove(position);
        int size = dataList.size();
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, size - position);
    }

    public void removeAllData() {
        dataList.clear();
        itemViewMap.clear();
        notifyDataSetChanged();
    }

    protected static class BaseViewHolder extends RecyclerView.ViewHolder{

        public BaseViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        public <T extends View> T findView(int viewId){
            View child = itemView.findViewById(viewId);
            return (T) child;
        }

        public void setText(int viewId, int resId){
            View child = itemView.findViewById(viewId);
            if (child instanceof TextView){
                ((TextView) child).setText(resId);
            }
        }

        public void setText(int viewId, String text){
            View child = itemView.findViewById(viewId);
            if (child instanceof TextView){
                ((TextView) child).setText(text);
            }
        }

        public void setText(int viewId, CharSequence text){
            View child = itemView.findViewById(viewId);
            if (child instanceof TextView){
                ((TextView) child).setText(text);
            }
        }

        public void setImage(int viewId, int resId){
            View child = itemView.findViewById(viewId);
            if (child instanceof ImageView){
                ((ImageView) child).setImageResource(resId);
            }
        }

        public void setImage(int viewId, Bitmap bitmap){
            View child = itemView.findViewById(viewId);
            if (child instanceof ImageView){
                ((ImageView) child).setImageBitmap(bitmap);
            }
        }

        public void setOnClickListener(int viewId, View.OnClickListener listener) {
            View child = itemView.findViewById(viewId);
            if (child != null){
                child.setOnClickListener(listener);
            }
        }
    }
}
