package com.casic.dcms.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.casic.dcms.R;
import com.casic.dcms.bean.CaseHandledBean;
import com.casic.dcms.utils.StringHelper;

import java.util.List;

public class CaseHandledAdapter extends RecyclerView.Adapter<CaseHandledAdapter.ItemViewHolder> {

    private List<CaseHandledBean.DataBean.RowsBean> dataBeans;
    private LayoutInflater layoutInflater;

    public CaseHandledAdapter(Context context, List<CaseHandledBean.DataBean.RowsBean> dataRows) {
        this.dataBeans = dataRows;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public CaseHandledAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_case_recycleview, parent, false);
        return new CaseHandledAdapter.ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CaseHandledAdapter.ItemViewHolder holder, int position) {
        holder.bindView(dataBeans.get(position));
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
    }

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

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView leftTextView;
        private TextView caseTitleView;
        private TextView casePlaceView;
        private TextView caseTimeView;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            leftTextView = itemView.findViewById(R.id.leftTextView);
            caseTitleView = itemView.findViewById(R.id.caseTitleView);
            casePlaceView = itemView.findViewById(R.id.casePlaceView);
            caseTimeView = itemView.findViewById(R.id.caseTimeView);
        }

        @SuppressLint("SetTextI18n")
        void bindView(CaseHandledBean.DataBean.RowsBean rowsBean) {
            String eorcName = rowsBean.getEorcName();
            if (TextUtils.isEmpty(eorcName)) {
                eorcName = "未知";
            }
            leftTextView.setText(eorcName.substring(0, 1));
            caseTitleView.setText(StringHelper.filterString(rowsBean.getDescription()));
            casePlaceView.setText("发生地点：" + StringHelper.filterString(rowsBean.getFieldintro()));
            caseTimeView.setText("上报时间：" + rowsBean.getReportTime());
        }
    }

    private OnItemClickListener clickListener;

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

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