package com.sipai.jspatrol.Patrol.patrolInfoAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.sipai.jspatrol.Bean.Measuringpoint;
import com.sipai.jspatrol.R;
import com.sipai.jspatrol.utils.Utils;

import java.util.List;

/**
 * created by lvliming on 2019/6/17
 */
public class PatrolInfoMeasurePointAdapter extends RecyclerView.Adapter<PatrolInfoMeasurePointAdapter.ViewHolder> {
    private List<Measuringpoint> list;
    private String planid;

    public PatrolInfoMeasurePointAdapter(List<Measuringpoint> list, String planid) {
        this.list = list;
        this.planid = planid;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_measurepoint,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.point.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int positon = viewHolder.getAdapterPosition();
//                Intent intent=new Intent();
//                intent.putExtra("planid", planid);
//                intent.putExtra("info", list.get(positon).getName()+"当前值为："+list.get(positon).getCvalue()+list.get(positon).getUnit());
//                intent.setClass(v.getContext(), EquFault_NewActivity.class);
//                v.getContext().startActivity(intent);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Measuringpoint item = list.get(position);
        holder.name.setText(item.getName());
        if (item.getName().contains("运行")){
            holder.iv_logo.setImageResource(R.mipmap.iv_eqp);
        }
        else if (item.getName().contains("故障")){
            holder.iv_logo.setImageResource(R.mipmap.iv_eqp);
        }
        else if (item.getName().contains("功率")||item.getName().toLowerCase().contains("p")&&!item.getName().toLowerCase().contains("ph")&&!item.getName().toLowerCase().contains("tp")&&!item.getName().toLowerCase().contains("输出")){
            holder.iv_logo.setImageResource(R.mipmap.iv_p);
        }
        else if (item.getName().toLowerCase().contains("ph")){
            holder.iv_logo.setImageResource(R.mipmap.iv_ph);
        }
        else if (item.getName().contains("氨氮")){
            holder.iv_logo.setImageResource(R.mipmap.iv_nh3);
        }
        else if (item.getName().contains("温度")){
            holder.iv_logo.setImageResource(R.mipmap.iv_temp_r);
        }
        else if (item.getName().toLowerCase().contains("cod")){
            holder.iv_logo.setImageResource(R.mipmap.iv_cod);
        }
        else if (item.getName().contains("流量")){
            holder.iv_logo.setImageResource(R.mipmap.iv_waterflow);
        }
        else if (item.getName().contains("风量")){
            holder.iv_logo.setImageResource(R.mipmap.iv_airflow);
        }
        else if (item.getName().contains("电")||item.getName().toLowerCase().contains("ia")||item.getName().toLowerCase().contains("uan")){
            holder.iv_logo.setImageResource(R.mipmap.iv_ele);
        }
        else if (item.getName().contains("液位")){
            holder.iv_logo.setImageResource(R.mipmap.iv_waterliqu);
        }
        else if (item.getName().toLowerCase().contains("ss")&&!item.getName().toLowerCase().contains("mlss")){
            holder.iv_logo.setImageResource(R.mipmap.iv_ss);
        }
        else if (item.getName().toLowerCase().contains("mlss")){
            holder.iv_logo.setImageResource(R.mipmap.iv_mlss);
        }
        else if (item.getName().toLowerCase().contains("nh3n")){
            holder.iv_logo.setImageResource(R.mipmap.iv_nh3n);
        }
        else if (item.getName().toLowerCase().contains("pa")||item.getName().contains("压力")){
            holder.iv_logo.setImageResource(R.mipmap.iv_pa);
        }
        else if (item.getName().toLowerCase().contains("hz")||item.getName().contains("频率")){
            holder.iv_logo.setImageResource(R.mipmap.iv_hz);
        }
        else if (item.getName().toLowerCase().contains("do1")){
            holder.iv_logo.setImageResource(R.mipmap.iv_do);
        }
        else if (item.getName().toLowerCase().contains("do2")){
            holder.iv_logo.setImageResource(R.mipmap.iv_do);
        }
        else if (item.getName().toLowerCase().contains("输出")){
            holder.iv_logo.setImageResource(R.mipmap.iv_eqp);
        }
        else if (item.getName().toLowerCase().contains("tn")||item.getName().contains("总氮")){
            holder.iv_logo.setImageResource(R.mipmap.iv_tn);
        }
        else if (item.getName().toLowerCase().contains("tp")||item.getName().contains("总磷")){
            holder.iv_logo.setImageResource(R.mipmap.iv_tp);
        }
        else{
            holder.iv_logo.setImageResource(R.mipmap.iv_water);
        }

        if (item.getUnit()!=null&&!item.getUnit().isEmpty()&&!item.getUnit().equals("null")) {
            holder.value.setText(item.getCvalue() + item.getUnit());
        }else {
            holder.value.setText(item.getCvalue());
        }
        if (item.getAlarmmax()!=null&&!item.getAlarmmax().isEmpty()&&item.getAlarmmin()!=null&&!item.getAlarmmin().isEmpty()) {
            float max = Float.parseFloat(item.getAlarmmax());
            float min = Float.parseFloat(item.getAlarmmin());
            float value = Float.parseFloat(item.getCvalue());
            if (max==min){
                holder.value.setTextColor(holder.point.getContext().getResources().getColor(R.color.gray04));
            }else {
                if (value < min || value > max) {
                    holder.value.setTextColor(holder.point.getContext().getResources().getColor(R.color.red));
                } else {
                    holder.value.setTextColor(holder.point.getContext().getResources().getColor(R.color.gray04));
                }
            }
        }
        if (item.getType().equals("DI")){
            holder.value.setVisibility(View.GONE);
        }else if (item.getType().equals("AI")){
            holder.value.setVisibility(View.VISIBLE);
        }
        String info = item.getName();
        if(item.getType().equals("DI")){
            int id= Utils.getlight4mp(info,item.getCvalue());
            holder.iv_btn.setVisibility(View.VISIBLE);
            holder.iv_btn.setImageResource(id);
        }else {
            holder.iv_btn.setVisibility(View.GONE);
        }
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
        View point;
        TextView name,value;
        ImageView iv_btn,iv_logo;
        public ViewHolder(View itemView) {
            super(itemView);
            point = itemView;
            name = (TextView) itemView.findViewById(R.id.point_name);
            value = (TextView) itemView.findViewById(R.id.point_value);
            iv_btn = (ImageView) itemView.findViewById(R.id.point_btn);
            iv_logo = (ImageView) itemView.findViewById(R.id.moudle_measurepoint_iv);
        }
    }
}
