package com.xwkj.jcjcgz.mvp.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

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

public abstract class BaseRecycleAdapter<T> extends RecyclerView.Adapter<BaseRecycleAdapter.BaseViewHolder> {

    protected List<T> datas;

    public BaseRecycleAdapter(List<T> datas) {
        this.datas = datas == null ? new ArrayList<T>() : datas;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

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

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(), parent, false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseRecycleAdapter.BaseViewHolder holder, final int position) {

        bindData(holder, position);

    }


    /**
     * 刷新数据
     *
     * @param datas
     */
    public void refresh(List<T> datas) {
        this.datas.clear();
        addDatas(datas);
//        if(datas.isEmpty()) return;
//        this.datas = datas;
        notifyDataSetChanged();
    }


    /**
     * 批量添加数据
     *
     * @param datas
     */
    public void addDatas(List<T> datas) {
        if (null != datas) {
            this.datas.addAll(datas);
            notifyDataSetChanged();
        }
    }

    /**
     * 添加数据
     *
     * @param data
     */
    public void addData(T data) {
        this.datas.add(data);
        notifyDataSetChanged();
    }

    /**
     * 绑定数据
     *
     * @param holder   具体的viewHolder
     * @param position 对应的索引
     */
    protected abstract void bindData(BaseViewHolder holder, int position);


    @Override
    public int getItemCount() {

        return datas == null ? 0 : datas.size();
    }

    public interface OnRecyclerViewListener {
        void onItemClick(int position);

        boolean onItemLongClick(int position);
    }

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

        boolean onItemLongClick(View view, int position);
    }
    public interface OnRecyclerViewListenerV3 {
        void onItemClick(View view, int position);

        boolean onItemLongClick(View view, int position);
    }
    public interface OnRecyclerViewListenerV4 {
        void onItemClick(View view, int position);

        boolean onItemLongClick(View view, int position);
    }

    /**
     * 封装ViewHolder ,子类可以直接使用
     */
    public class BaseViewHolder extends RecyclerView.ViewHolder {


        private Map<Integer, View> mViewMap;

        public BaseViewHolder(View itemView) {
            super(itemView);
            mViewMap = new HashMap<>();
        }

        /**
         * 获取设置的view
         *
         * @param id
         * @return
         */
        public View getView(int id) {
            View view = mViewMap.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                mViewMap.put(id, view);
            }
            return view;
        }

        public void setOnItemClickListener(int id, final int position, final OnRecyclerViewListener listener) {
            getView(id).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position);
                    }
                }
            });
        }

        public void setOnItemClickListenerV2(int id, final int position, final OnRecyclerViewListenerV2 listener) {
            getView(id).setOnClickListener(v -> listener.onItemClick(v, position));
        }
        public void setOnItemClickListenerV3(int id, final int position, final OnRecyclerViewListenerV3 listener) {
            getView(id).setOnClickListener(v -> listener.onItemClick(v, position));
        }
        public void setOnItemClickListenerV4(int id, final int position, final OnRecyclerViewListenerV4 listener) {
            getView(id).setOnClickListener(v -> listener.onItemClick(v, position));
        }
        public void setOnItemLongClickListener(int id, final int position, final OnRecyclerViewListener listener) {
            getView(id).setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return listener.onItemLongClick(position);
                }
            });
        }

        public void setOnItemLongClickListenerV2(int id, final int position, final OnRecyclerViewListenerV2 listener) {
            getView(id).setOnLongClickListener(v -> listener.onItemLongClick(v, position));
        }
        public void setOnItemLongClickListenerV3(int id, final int position, final OnRecyclerViewListenerV3 listener) {
            getView(id).setOnLongClickListener(v -> listener.onItemLongClick(v, position));
        }
        public void setOnItemLongClickListenerV4(int id, final int position, final OnRecyclerViewListenerV4 listener) {
            getView(id).setOnLongClickListener(v -> listener.onItemLongClick(v, position));
        }
    }

    /**
     * 获取子item
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * 设置文本属性
     *
     * @param view
     * @param text
     */
    public void setItemText(View view, String text) {
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
        }
    }

    /**
     * 设置文本属性
     *
     * @param view
     * @param text
     */
    public void setItemTextAndColor(View view, String text, int color) {
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
            ((TextView) view).setTextColor(color);
        }
    }

    public void setItemTextColorAndHid(View view, String text, int color) {
        if (view instanceof TextView) {
            ((TextView) view).setText(text);
            ((TextView) view).setTextColor(color);
        }
    }

    /**
     * 设置图片属性
     *
     * @param view
     * @param resId
     */
    public void setItemImageView(View view, int resId) {
        if (view instanceof ImageView) {
            view.setBackgroundResource(resId);
        }
    }

    /**
     * 设置图片属性
     *
     * @param view
     * @param url
     */
    public void setItemImage(View view, String url) {
        if (view instanceof ImageView) {
            url = "http://192.168.20.228:7213/jlyqfk/" + url;
            /*((TextView) view).setBackgroundResource(resId);*/
            Picasso.with(view.getContext()).load(url).fit().into(((ImageView) view));
            //  ImagePicker.getInstance().getImageLoader().displayImage((Activity)view.getContext(), url, ((ImageView) view), 0, 0);
        }
    }

    public void setItemImageV2(View view, String base64) {
        if (view instanceof ImageView) {
            Glide.with(view.getContext())
                    .load(base64)
                    .into(((ImageView) view));
            //  ImagePicker.getInstance().getImageLoader().displayImage((Activity)view.getContext(), url, ((ImageView) view), 0, 0);
        }
    }

    public void setItemGirdView(View view, BaseRecycleAdapter adapter) {
        if (view instanceof RecyclerView) {
            ((RecyclerView) view).setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.HORIZONTAL, false));
            ((RecyclerView) view).setAdapter(adapter);
        }
    }
}
