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.MaintainExecuteBean;
import com.tofly.ghrq_route.module.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.common.util.file.FileUtil;
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 EmergencyRepairHandleAdapter extends ARecyclerBaseAdapter<EmergencyRepairHandleAdapter.ViewHolder, MaintainExecuteBean> {
    private Context context;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private EmergencyHandleData mHandleData;

    public EmergencyRepairHandleAdapter(Context context, EmergencyHandleData handleData, List<MaintainExecuteBean> 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_repair_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.text_reporter)
        TextView textReporter;
        @BindView(R.id.text_report_time)
        TextView textReportTime;
        @BindView(R.id.text_repair_note)
        TextView textRepairNote;
        @BindView(R.id.layout_repair_note)
        LinearLayout layoutRepairNote;
        @BindView(R.id.addition_gallery)
        LinearLayout additionGallery;
        @BindView(R.id.layout_addition_area)
        LinearLayout layoutAdditionArea;

        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) {
            MaintainExecuteBean executeBean = getItem(position);
            if (executeBean != null) {
                /*上报人*/
                String responseUser = mHandleData.getResponsibleUserName();
                textReporter.setText(responseUser);

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

                /*抢修派工描述*/
                String maintenanceNotes = executeBean.getMaintenanceNotes();
                if (maintenanceNotes != null && !maintenanceNotes.isEmpty()) {
                    layoutRepairNote.setVisibility(View.VISIBLE);
                    textRepairNote.setText(maintenanceNotes);
                }else {
                    layoutRepairNote.setVisibility(View.GONE);
                }

                /*附件*/
                String emergencyId = mHandleData.getId() + "";
                String fileNote = "附件";

                additionGallery.removeAllViews();
                List<AuditFileBean>  maintenanceFiles = executeBean.getMaintenanceFileList();  ///附件
                int fileCount = 0;
                if (maintenanceFiles != null) {
                    fileCount = maintenanceFiles.size();
                    for (AuditFileBean auditFileBean : maintenanceFiles) {
                        getSchemeFiles(emergencyId, auditFileBean, fileNote, additionGallery);
                    }
                }

                List<AuditFileBean> maintenanceVoices = executeBean.getMaintenanceVoiceList();  ///附件
                if (maintenanceVoices != null) {
                    fileCount = fileCount + maintenanceVoices.size();
                    for (AuditFileBean auditFileBean : maintenanceVoices) {
                        getSchemeFiles(emergencyId, auditFileBean, fileNote, additionGallery);
                    }
                }

                if (fileCount > 0) {
                    layoutAdditionArea.setVisibility(View.VISIBLE);
                } else {
                    layoutAdditionArea.setVisibility(View.GONE);
                }
            }
        }

        private void getSchemeFiles(String schemeId, AuditFileBean fileBean, String fileNote, final ViewGroup rootView) {
            if (fileBean != null) {
                FileViewUtil.GetViewCallback getViewCallback = (isSuccess, view) -> {
                    if (isSuccess && view != null) {
                        rootView.addView(view);
                    }
                };

                String fileName = fileBean.getFileName();
                String filePath = fileBean.getPath();
                if (FileUtil.isFileTypeImage(fileName)) {
                    fileViewUtil.getImageFileView(filePath, getViewCallback);
                } else if (FileUtil.isFileTypeAudio(fileName)) {
                    fileViewUtil.getAudioFileView(schemeId, filePath, 1, fileNote, mRecords, null, getViewCallback);
                } else {
                    fileViewUtil.getFileView(schemeId, filePath, fileName, fileNote, getViewCallback);
                }
            }
        }
    }
}
