package com.sjkj.appthreefloor_tsgz.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sjkj.appthreefloor_tsgz.R;
import com.sjkj.appthreefloor_tsgz.bean.ResourcesListBean;
import com.sjkj.base_lib.utils.ToastUtils;

import java.util.List;


public class RecycleviewLeftAdapter extends RecyclerView.Adapter<RecycleviewLeftAdapter.MyViewHolder> implements View.OnClickListener {

    private List<ResourcesListBean> mDatas;
    private Context mContext;
    private LayoutInflater inflater;
    public void refreshList(List<ResourcesListBean> list) {
        this.mDatas = list;
        notifyDataSetChanged();
//        if (this.mDatas == null || this.mDatas.size() <= 0) {
//            ToastUtils.show("暂无更多数据");
//        }
    }
    private int selectedPosition = 0;

    private OnItemClickListener mOnItemClickListener = null;
    private View viewClassify;

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


    public RecycleviewLeftAdapter(Context context, List<ResourcesListBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_classify_rv_adapter, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(mDatas.get(position).getName());
        //选中和没选中时，设置不同的颜色
        if (position == selectedPosition) {
            holder.tv.setBackgroundColor(Color.WHITE);
            holder.tv.setTextColor(Color.BLACK);

        } else {
            holder.tv.setBackgroundColor(mContext.getResources().getColor(R.color.recyclerview_left_select));
            holder.tv.setTextColor(mContext.getResources().getColor(R.color.recyclerview_lefttext_select));

        }
        holder.itemView.setTag(position);
    }


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

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

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

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.classsify_tv);
        }
    }

}
