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.DoingFaultDetailActivity;
import com.sipai.jspatrol.Fault.FaultFlowChartActivity;
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/3/19.
 */

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

    public DoingFaultAdapter(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_doing_fault_ll_all, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (finalItem.getTodoTask()!=null) {
                    if (finalItem.getTodoTask().contains("维修确认")) {
//                        Toast.makeText(context, "工单验收完善中，敬请期待...", Toast.LENGTH_SHORT).show();
                        FaultFlowChartActivity.actionStart(context, finalItem);
                    } else {
//                        if (finalItem.getTodoTask().contains("维修确认")||finalItem.getTodoTask().contains("班长")){
////                            Toast.makeText(context, "工单审核中...", Toast.LENGTH_SHORT).show();
//                            FaultFlowChartActivity.actionStart(context, finalItem);
//                        }else {
                            DoingFaultDetailActivity.actionStart(context, finalItem);
//                        }
                    }
                }
            }
        });
        if (item.getProcessSection().getName()!=null&&!item.getProcessSection().getName().equals("")){
            holder.setVisible(R.id.moudle_item_doing_fault_tv_area,true);
            holder.setText(R.id.moudle_item_doing_fault_tv_area,item.getProcessSection().getName());
        }else {
            holder.setVisible(R.id.moudle_item_doing_fault_tv_area,false);
        }
        if (item.getProblemcontent()!=null&&!item.getProblemcontent().isEmpty()) {
            holder.setText(R.id.moudle_item_doing_fault_tv_content, item.getProblemcontent());
        }else {
            holder.setText(R.id.moudle_item_doing_fault_tv_content, "");
        }
        if (item.getProblemTypeNames()!=null&&item.getProblemTypeNames().length()>0) {
            holder.setText(R.id.moudle_item_doing_fault_tv_remark,item.getProblemTypeNames());
        }else {
            holder.setVisible(R.id.moudle_item_doing_fault_tv_remark,false);
        }
        if (item.getEquipmentCard()!=null&&!item.getEquipmentCard().getEquipmentname().equals("")){
            holder.setVisible(R.id.moudle_item_doing_fault_tv_equ,true);
            holder.setText(R.id.moudle_item_doing_fault_tv_equ,item.getEquipmentCard().getEquipmentname());
        }else {
            holder.setVisible(R.id.moudle_item_doing_fault_tv_equ,false);
        }
        if (item.getInsdt()!=null&&item.getInsdt().length()>16) {
            holder.setText(R.id.moudle_item_doing_fault_tv_date,item.getInsdt().substring(5, 16));
        }else {
            holder.setText(R.id.moudle_item_doing_fault_tv_date,"");
        }
        if (item.getInsuser()!=null&&!item.getInsuser().isEmpty()) {
            holder.setText(R.id.moudle_item_doing_fault_tv_man, item.getInsuser());
        }else {
            holder.setText(R.id.moudle_item_doing_fault_tv_man, "");
        }
        if (item.getTodoTask()!=null) {
            if (item.getTodoTask().equals("待签收")) {
                holder.setText(R.id.moudle_item_doing_fault_tv_todotask, item.getTodoTask());
                holder.setTextColor(R.id.moudle_item_doing_fault_tv_todotask, context.getResources().getColor(R.color.error_red));
//                holder.setVisible(R.id.moudle_item_doing_fault_btn_make_fault, false);
            } else {
                holder.setText(R.id.moudle_item_doing_fault_tv_todotask, item.getTodoTask());
            }
        }
//        holder.setOnClickListener(R.id.moudle_item_doing_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_doing_fault_btn_delete, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if (null!=mOnSwipeListener){
//                    mOnSwipeListener.onDel(position);
//                    ((SwipeMenuLayout) holder.itemView).smoothClose();
//                }
//            }
//        });
    }

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

    /**
     * 和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;
    }
}
