package com.sxyz.rfid.ui.ui.main;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.sxyz.rfid.R;

import java.util.List;

public class ManageListAdapterRv extends RecyclerView.Adapter<ManageListAdapterRv.ViewHolder> {

    private List<com.sxyz.courtai.ui.pojo.TagManage> data;
    private Context context;
    private onItemSelectedListener onItemSelectedListener;
    private int currentItem = -1;
    private int temp = -1;

    public ManageListAdapterRv(List<com.sxyz.courtai.ui.pojo.TagManage> data, Context context) {
        this.data = data;
        this.context = context;
    }

    public List<com.sxyz.courtai.ui.pojo.TagManage> getData() {
        return data;
    }

    public void setData(List<com.sxyz.courtai.ui.pojo.TagManage> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public ManageListAdapterRv.onItemSelectedListener getOnItemSelectedListener() {
        return onItemSelectedListener;
    }

    public void setOnItemSelectedListener(ManageListAdapterRv.onItemSelectedListener onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(context).inflate(R.layout.tag_manage_item, parent, false);

        return new ViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.refreshView(position, data.get(position));
        holder.itemView.setSelected(holder.getLayoutPosition() == currentItem);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.itemView.setSelected(true);
                temp = currentItem;
                currentItem = holder.getLayoutPosition();
                notifyItemChanged(temp);
                onItemSelectedListener.onItemSelected(holder.itemView, position, data.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return null != data ? data.size() : 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private View mView;
        TextView manageEpcText;
        TextView manageDataText;

        public ViewHolder(View view) {
            super(view.getRootView());
            this.mView = view;
            manageEpcText = mView.findViewById(R.id.manage_epc_text);
            manageDataText = mView.findViewById(R.id.manage_data_text);
        }

        private void refreshView(int position, com.sxyz.courtai.ui.pojo.TagManage data) {
            Log.d("usdk", "refreshView: data = " + data + ", i = " + position);
            manageEpcText.setText(data.getEpc());
            manageDataText.setText(data.getData());
        }
    }

    interface onItemSelectedListener {

        void onItemSelected(View v, int position, com.sxyz.courtai.ui.pojo.TagManage data);

    }
}
