package com.sipai.jspatrol.Fault.adapter;

import android.content.Context;
import android.view.View;

import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.sipai.jspatrol.Bean.Fault;
import com.sipai.jspatrol.Fault.NewFaultDetailActivity;
import com.sipai.jspatrol.R;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.List;

/**
 * Created by lvliming on 2019/2/27.
 */

public class NewFaultAdapter extends SuperBaseAdapter<Fault> {
    private List<Fault> data;
    private Context context;
    private onSwipeListener mOnSwipeListener;

    public NewFaultAdapter(Context context, List<Fault> data) {
        super(context, data);
        this.data = data;
        this.context = context;
    }

    @Override
    protected void convert(final BaseViewHolder holder, Fault item, final int position) {
        item = data.get(position);
        ((SwipeMenuLayout)holder.itemView).setIos(false).setLeftSwipe(true);
        final Fault finalItem = item;
        holder.setOnClickListener(R.id.moudle_item_new_fault_ll_all, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NewFaultDetailActivity.actionStart(context,finalItem,"no");
            }
        });
        holder.setText(R.id.moudle_item_new_fault_tv_area,item.getProcessSection().getName());
        holder.setText(R.id.moudle_item_new_fault_tv_content,item.getAbnormityDescription());
        if (item.getRemark()!=null&&item.getRemark().length()>0) {
            holder.setText(R.id.moudle_item_new_fault_tv_remark,item.getRemark());
        }else {
            holder.setVisible(R.id.moudle_item_new_fault_tv_remark,false);
        }
        if (item.getEquipmentNames()!=null){
            holder.setText(R.id.moudle_item_new_fault_tv_equ,item.getEquipmentNames());
        }
        if (item.getInsdt().length()>16) {
            holder.setText(R.id.moudle_item_new_fault_tv_date,item.getInsdt().substring(5, 16));
        }else {
            holder.setText(R.id.moudle_item_new_fault_tv_date,"");
        }
        holder.setText(R.id.moudle_item_new_fault_tv_man,item.getInsuser());
        holder.setOnClickListener(R.id.moudle_item_new_fault_btn_make_fault, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null!=mOnSwipeListener){
                    mOnSwipeListener.onFault(position);
                    ((SwipeMenuLayout) holder.itemView).smoothClose();
                }
            }
        });
        holder.setOnClickListener(R.id.moudle_item_new_fault_btn_delete, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null!=mOnSwipeListener){
                    mOnSwipeListener.onDel(position);
                    ((SwipeMenuLayout) holder.itemView).smoothClose();
                }
            }
        });
    }

    /**
     * 和Activity通信的接口
     */
    public interface onSwipeListener {
        void onFault(int pos);

        void onDel(int pos);
    }

    public onSwipeListener getOnDelListener() {
        return mOnSwipeListener;
    }

    public void setOnDelListener(onSwipeListener mOnDelListener) {
        this.mOnSwipeListener = mOnDelListener;
    }

    @Override
    protected int getItemViewLayoutId(int position, Fault item) {
        return R.layout.item_new_fault;
    }
}
