package com.sipai.jspatrol.Alarm;

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

import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.Alarm;
import com.sipai.jspatrol.Fault.NewFaultActivity;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.utils.Global.GlobalVal;

import java.util.List;

/**
 * created by lvliming on 2020/11/9
 */
public class AlarmAdapter extends RecyclerView.Adapter<AlarmAdapter.ViewHolder> {
    private List<Alarm> list;
    private Context context;

    public AlarmAdapter(Context context,List<Alarm> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public AlarmAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alarm,parent,false);
        final AlarmAdapter.ViewHolder viewHolder = new AlarmAdapter.ViewHolder(view);
        viewHolder.msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                NewFaultActivity.alarmStart(context,list.get(position).getDescribe());
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(AlarmAdapter.ViewHolder holder, int position) {
        Alarm item = list.get(position);
        holder.time.setText(item.getAlarmtime());
        holder.content.setText(item.getDescribe());
        holder.tv_type.setText(item.getAlarmTypeName());
        holder.tv_lvl.setText(item.getPointName());
        if (item.getStatus().equals(GlobalVal.Status_N)){
            holder.tv_status.setText("未处理");
            holder.tv_status.setTextColor(context.getResources().getColor(R.color.error_red));
        }else if (item.getStatus().equals(GlobalVal.Status_C)){
            holder.tv_status.setText("已确认");
            holder.tv_status.setTextColor(context.getResources().getColor(R.color.error_yellow));
        }else if (item.getStatus().equals(GlobalVal.Status_Y)){
            holder.tv_status.setText("已恢复");
            holder.tv_status.setTextColor(context.getResources().getColor(R.color.little_blue));
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        View msg;
        TextView time,content,tv_type,tv_lvl,tv_status;
        public ViewHolder(View itemView) {
            super(itemView);
            msg = itemView;
            time = itemView.findViewById(R.id.moudle_item_alarm_tv_time);
            content = itemView.findViewById(R.id.moudle_item_alarm_tv_content);
            tv_type = itemView.findViewById(R.id.moudle_item_alarm_tv_alarmtype);
            tv_lvl = itemView.findViewById(R.id.moudle_item_alarm_tv_alarmlvl);
            tv_status = itemView.findViewById(R.id.moudle_item_alarm_tv_status);;
        }
    }
}
