package com.tofly.ghrq_route.module.emergencyHandle.ui.adapter;

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

import androidx.recyclerview.widget.RecyclerView;

import com.gaoyuan.weixinrecord.example.Record;
import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.emergencyHandle.domain.ScheduleExecuteBean;
import com.tofly.ghrq_route.module.emergencyHandle.ui.EmergencyAssignHandleDetailBottomDialog;
import com.tofly.ghrq_route.common.util.file.FileViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/1/10.
 */

public class EmergencyAssignHandleAdapter extends ARecyclerBaseAdapter<EmergencyAssignHandleAdapter.ViewHolder, ScheduleExecuteBean> {
    private Context context;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private EmergencyHandleData mHandleData;

    private EmergencyAssignHandleDetailBottomDialog handleDetailBottomDialog;

    public EmergencyAssignHandleAdapter(Context context, EmergencyHandleData handleData, List<ScheduleExecuteBean> executeBeans) {
        super(context, executeBeans);
        this.context = context;
        mHandleData = handleData;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_emerge_assign_handle,
                parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(position);
    }

    protected class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_check_detail)
        LinearLayout layoutCheckDetail;
        @BindView(R.id.text_reporter)
        TextView textReporter;
        @BindView(R.id.text_report_time)
        TextView textReportTime;
        @BindView(R.id.text_is_maintenance)
        TextView textIsMaintenance;
        @BindView(R.id.text_is_audit)
        TextView textIsAudit;
        @BindView(R.id.text_over_time)
        TextView textOverTime;
        @BindView(R.id.layout_over_time)
        LinearLayout layoutOverTime;
        @BindView(R.id.text_handle_note)
        TextView textHandleNote;
        @BindView(R.id.layout_handle_note)
        LinearLayout layoutHandleNote;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

            if (null != onItemClickListener) {
                itemView.setOnClickListener(v -> onItemClickListener.onItemClick(itemView, getLayoutPosition()));
            }
        }

        public void setData(final int position) {
            ScheduleExecuteBean executeBean = getItem(position);
            if (executeBean != null) {
                /*上报人*/
                String dispatchUserName = mHandleData.getDispatchUserName();
                textReporter.setText(dispatchUserName);

                /*上报时间*/
                String completeTime = executeBean.getCompleteTime();
                textReportTime.setText(completeTime);

                /*是否抢修*/
                int isMaintenance = executeBean.getIsMaintenance();
                textIsMaintenance.setText(isMaintenance == 0 ? "不抢修" : "需要抢修");

                /*是否审核*/
                int auditFlag = executeBean.getAuditFlag();
                textIsAudit.setText(auditFlag == 0 ? "不审核" : "需要审核");

                /*到位时间*/
                String overTime = executeBean.getOverTime();
                if (overTime != null && !overTime.isEmpty()) {
                    layoutOverTime.setVisibility(View.VISIBLE);
                    textOverTime.setText(overTime);
                }else {
                    layoutOverTime.setVisibility(View.GONE);
                }

                /*处置情况*/
                String emergencyHandling = executeBean.getEmergencyHandling();
                if (emergencyHandling != null && !emergencyHandling.isEmpty()) {
                    layoutHandleNote.setVisibility(View.VISIBLE);
                    textHandleNote.setText(emergencyHandling);
                }else {
                    layoutHandleNote.setVisibility(View.GONE);
                }

                layoutCheckDetail.setOnClickListener(view -> {
                    showHandleDetailDialog(executeBean);
                });
            }
        }

        private void showHandleDetailDialog(ScheduleExecuteBean executeBean) {
            if (handleDetailBottomDialog == null) {
                handleDetailBottomDialog = new EmergencyAssignHandleDetailBottomDialog(mContext, mHandleData, executeBean);
            }else {
                handleDetailBottomDialog.setExecuteBean(executeBean);
            }
            if (!handleDetailBottomDialog.isShowing()) {
                handleDetailBottomDialog.show();
            }
        }
    }
}
