package com.casic.dcms.adapter;

import android.content.Context;
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.AlarmDataBean;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.util.List;

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

    private List<AlarmDataBean.DataBean.RowsBean> dataRows;
    private LayoutInflater layoutInflater;

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

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

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindView(dataRows.get(position));
        if (cancelClickListener != null) {
            holder.cancelAlarmButton.setChangeAlphaWhenPress(true);
            holder.cancelAlarmButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancelClickListener.onClick(position);
                }
            });
        }
    }

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

    private OnCancelClickListener cancelClickListener;

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

    public void setOnCancelClickListener(OnCancelClickListener onCancelClickListener) {
        this.cancelClickListener = onCancelClickListener;
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView alarmMessageView;
        private TextView wellCodeView;
        private TextView alarmPositionView;
        private TextView alarmTimeView;
        private QMUIRoundButton cancelAlarmButton;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            alarmMessageView = itemView.findViewById(R.id.alarmMessageView);
            wellCodeView = itemView.findViewById(R.id.wellCodeView);
            alarmPositionView = itemView.findViewById(R.id.alarmPositionView);
            alarmTimeView = itemView.findViewById(R.id.alarmTimeView);
            cancelAlarmButton = itemView.findViewById(R.id.cancelAlarmButton);
        }

        void bindView(AlarmDataBean.DataBean.RowsBean rowsBean) {
            alarmMessageView.setText(rowsBean.getAlarmMessage());
            wellCodeView.setText(rowsBean.getWellCode());
            alarmPositionView.setText(rowsBean.getPosition());
            alarmTimeView.setText(rowsBean.getAlarmTime());
        }
    }
}
