package com.casic.dcms.adapter;

import android.annotation.SuppressLint;
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.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.casic.dcms.R;
import com.casic.dcms.model.PunishTimesBean;
import com.casic.dcms.model.PunishTypeBean;

import java.util.List;

@SuppressLint("SetTextI18n")
public class PunishTypeAdapter extends RecyclerView.Adapter<PunishTypeAdapter.ItemViewHolder> {

    private final Context context;
    private final List<PunishTypeBean.DataBean> dataRows;
    private final PunishTimesBean.DataBean dataBean;
    private final LayoutInflater layoutInflater;

    PunishTypeAdapter(Context context, List<PunishTypeBean.DataBean> dataRows, PunishTimesBean.DataBean dataBean) {
        this.context = context;
        this.dataRows = dataRows;
        this.dataBean = dataBean;
        this.layoutInflater = LayoutInflater.from(context);
    }

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

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        String typeName = dataRows.get(position).getPunishTypeName();
        int color, times;
        switch (typeName) {
            case "劝导":
                times = dataBean.getPersuade();
                color = ContextCompat.getColor(context, R.color.persuadeColor);
                break;
            case "警告":
                times = dataBean.getWarn();
                color = ContextCompat.getColor(context, R.color.warnColor);
                break;
            case "处罚":
                times = dataBean.getPunish();
                color = ContextCompat.getColor(context, R.color.punishColor);
                break;
            default:
                times = 0;
                color = ContextCompat.getColor(context, R.color.mainThemeColor);
                break;
        }
        if (times == 0) {
            holder.punishTypeView.setVisibility(View.GONE);
        } else {
            holder.punishTypeView.setText(typeName + " x " + times);
            holder.punishTypeView.setBackgroundColor(color);
        }
    }

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

    static class ItemViewHolder extends RecyclerView.ViewHolder {

        private final TextView punishTypeView;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            punishTypeView = itemView.findViewById(R.id.punishTypeView);
        }
    }
}
