package com.sipai.jspatrol.ProMonitoring;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.haozhang.lib.SlantedTextView;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
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 2017/10/24.
 */

public class MeasurePointsAdapter extends RecyclerView.Adapter<MeasurePointsAdapter.ViewHolder> {
    private Context context;
    private List<Measuringpoint> list;
    private onSwipeListener mOnSwipeListener;

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_measurepoint,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    public void setData(List<Measuringpoint> list){
        this.list = list;
    }

    @SuppressLint({"RecyclerView", "ResourceAsColor"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Measuringpoint item = list.get(position);
        Measuringpoint finalItem = item;
        holder.ll_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(view.getContext(), PointHistoryActivity.class);
                intent.putExtra("point", finalItem);
//                intent.putExtra("pointid", finalItem.getSid());
//                intent.putExtra("unitId", finalItem.getUnitId());
//                intent.putExtra("max", finalItem.getAlarmmax());
//                intent.putExtra("min", finalItem.getAlarmmin());
                view.getContext().startActivity(intent);
            }
        });
        holder.ll_all.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String ss = "aaa";
                return false;
            }
        });
        item = list.get(position);
        ((SwipeMenuLayout)holder.view).setIos(false).setLeftSwipe(true);
        holder.tv_point_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")){
            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("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 if (item.getName().contains("维护")||item.getName().contains("维修")||item.getName().contains("过载")||item.getName().contains("超时")||item.getName().contains("信号")||item.getName().contains("到位")||item.getName().contains("力矩")||item.getName().contains("报警")||item.getName().contains("次数")){
            holder.iv_logo.setImageResource(R.mipmap.iv_eqp);
        }
        else{
            holder.iv_logo.setImageResource(R.mipmap.iv_water);
        }


        if (item.getValuemeaning()==null||item.getValuemeaning().isEmpty()){
            if (item.getUnit()!=null&&!item.getUnit().isEmpty()&&!item.getUnit().equals("null")) {
                holder.tv_value.setText(item.getCvalue()+item.getUnit());
            }else {
                holder.tv_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.tv_value.setTextColor(R.color.gray04);
            }else {
                if (value <= min || value >= max) {
                    holder.tv_value.setTextColor(R.color.red);
                } else {
                    holder.tv_value.setTextColor(R.color.gray04);
                }
            }
        }
        if (item.getType().equals("DI")){
            holder.tv_value.setVisibility(View.GONE);
        }else if (item.getType().equals("AI")){
            holder.tv_value.setVisibility(View.VISIBLE);
        }
        String info = item.getName();
        if(item.getType().equals("DI")){
            int id= Utils.getlight4mp(info,item.getCvalue());
            holder.iv_value.setVisibility(View.VISIBLE);
            holder.iv_value.setImageResource(id);
        }else {
            holder.iv_value.setVisibility(View.GONE);
        }
        if (item.getUserid()!=null) {
            holder.btn_collecte.setVisibility(View.VISIBLE);
            if (!item.getUserid().isEmpty() && !item.getUserid().equals("null")) {
                holder.stv.setVisibility(View.VISIBLE);
                holder.btn_collecte.setText("取消收藏");
                holder.btn_collecte.setBackgroundResource(R.drawable.btn_error_press);
            } else {
                holder.stv.setVisibility(View.GONE);
                holder.btn_collecte.setText("收藏");
                holder.btn_collecte.setBackgroundResource(R.drawable.btn_press);
            }
        }else {
            holder.stv.setVisibility(View.GONE);
            holder.btn_collecte.setVisibility(View.GONE);
        }
        holder.btn_collecte.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null!=mOnSwipeListener){
                    mOnSwipeListener.onCollecte(position);
                    ((SwipeMenuLayout) holder.itemView).smoothClose();
                }
            }
        });
    }

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

    public interface onSwipeListener{
        void  onCollecte(int pos);
    }

    public void setOnCollecteListener(onSwipeListener mOnCollecteListener) {
        this.mOnSwipeListener = mOnCollecteListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        View view;
        LinearLayout ll_all;
        TextView tv_point_name,tv_value;
        ImageView iv_logo,iv_value;
        Button btn_collecte;
        SlantedTextView stv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            ll_all = view.findViewById(R.id.moudle_point_ll_all);
            tv_point_name = view.findViewById(R.id.point_name);
            tv_value = view.findViewById(R.id.point_value);
            iv_logo = view.findViewById(R.id.moudle_measurepoint_iv);
            iv_value = view.findViewById(R.id.point_btn);
            btn_collecte = view.findViewById(R.id.moudle_item_btn_collecte);
            stv = view.findViewById(R.id.moudle_measurepoint_st);
        }
    }
}

