package com.sipai.jspatrol.Patrol.patrolInfoAdapter;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Patrol.PatrolEquDetailActivity;
import com.sipai.jspatrol.R;

import java.util.List;

/**
 * created by lvliming on 2019/6/17
 */
public class PatrolInfoEquAdapter extends RecyclerView.Adapter<PatrolInfoEquAdapter.ViewHolder> {
    private List<PatrolEqu> list;
    private Context context;
//    private DoubleManager keyDoubleManager = null;

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

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_patrol_info_equ,parent,false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = viewHolder.getAdapterPosition();
                PatrolEqu infoBean = list.get(position);
                if (infoBean.getMeasuringpoints()!=null&&infoBean.getMeasuringpoints().size()>0) {
                    if (viewHolder.rc_list.getVisibility() == View.VISIBLE) {
                        viewHolder.rc_list.setVisibility(View.GONE);
                    } else if (viewHolder.rc_list.getVisibility() == View.GONE) {
                        viewHolder.rc_list.setVisibility(View.VISIBLE);
                    }
                }else {
                    Toast.makeText(context, "该设备下暂未配置测量点", Toast.LENGTH_SHORT).show();
                }
            }
        });
        viewHolder.ll_item.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int position = viewHolder.getAdapterPosition();
                PatrolEqu infoBean = list.get(position);
                PatrolEquDetailActivity.actionStart(context, infoBean.getEquipmentId(), infoBean.getPatrolPointId(), infoBean.getPatrolRecordId(),infoBean.getType());
                return false;
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PatrolEqu item = list.get(position);
        holder.tv_title.setText(item.getEquipmentName());
        if (item.getManufacturers()!=null&&!item.getManufacturers().isEmpty()) {
            holder.tv_equipmentmanufacturer.setText(item.getManufacturers());
        }
        holder.tv_cardId.setText("设备号："+item.getEquipmentCardId());
        if (item.getMeasuringpoints()!=null&&item.getMeasuringpoints().size()>0){
            PatrolInfoEquMeasurePointAdapter measurePointsAdapter = new PatrolInfoEquMeasurePointAdapter(item.getMeasuringpoints());
            holder.rc_list.setLayoutManager(new LinearLayoutManager(context));
            holder.rc_list.setAdapter(measurePointsAdapter);
            holder.tv_count.setText(String.valueOf(item.getMeasuringpoints().size()));
        }else {
            holder.tv_count.setText("0");
        }
        if (item.getStatus().equals("3")){
            holder.tv_title.setTextColor(context.getResources().getColor(R.color.white));
            holder.tv_equipmentmanufacturer.setTextColor(context.getResources().getColor(R.color.white));
            holder.tv_cardId.setTextColor(context.getResources().getColor(R.color.white));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                holder.ll_all.setBackground(context.getDrawable(R.drawable.bg_layout_shape_com));
            }else {
                holder.ll_all.setBackground(ResourcesCompat.getDrawable(context.getResources(),R.drawable.bg_layout_shape_com,null));
            }
        }else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                holder.ll_all.setBackground(context.getDrawable(R.drawable.bg_layout_shape_factory));
            }else {
                holder.ll_all.setBackground(ResourcesCompat.getDrawable(context.getResources(),R.drawable.bg_layout_shape_factory,null));
            }
        }
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_title,tv_count;
        LinearLayout ll_item,ll_all;
        RecyclerView rc_list;
        TextView tv_equipmentmanufacturer,tv_cardId;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_count = itemView.findViewById(R.id.moudle_item_patrol_info_equ_pointcount);
            tv_title = itemView.findViewById(R.id.moudle_item_patrol_info_equ_title);
//            tv_majorparameter = itemView.findViewById(R.id.moudle_item_patrol_info_equ_majorparameter);
//            tv_type = itemView.findViewById(R.id.moudle_item_patrol_info_equ_type);
            tv_cardId = itemView.findViewById(R.id.moudle_item_patrol_info_equ_cardid);
            tv_equipmentmanufacturer = itemView.findViewById(R.id.moudle_item_patrol_info_equ_equipmentmanufacturer);
            ll_item = itemView.findViewById(R.id.moudle_item_patrol_info_equ_ll_title);
            rc_list = itemView.findViewById(R.id.moudle_item_patrol_info_equ_rc_grid);
            ll_all = itemView.findViewById(R.id.moudle_item_patrol_info_ll);

        }
    }
}
