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

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
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.trouble.domain.AuditFileBean;
import com.tofly.ghrq_route.module.trouble.domain.AuditSchemeBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
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 java.util.Locale;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;

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

public class TroubleSchemeVerifyAdapter extends ARecyclerBaseAdapter<TroubleSchemeVerifyAdapter.ViewHolder, AuditSchemeBean> {
    private Context context;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private TroubleBean mTroubleBean;

    public TroubleSchemeVerifyAdapter(Context context, TroubleBean troubleBean, List<AuditSchemeBean> featureList) {
        super(context, featureList);
        this.context = context;
        mTroubleBean = troubleBean;
        fileViewUtil = new FileViewUtil(context);
        mRecords = new ArrayList<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_trouble_scheme_verify, 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_scheme_title)
        TextView textSchemeTitle;
        @BindView(R.id.text_duty_name)
        TextView textDutyName;
        @BindView(R.id.text_executor_name)
        TextView textExecutorName;
        @BindView(R.id.text_assists_name)
        TextView textAssistsName;
        @BindView(R.id.text_scheme_time)
        TextView textSchemeTime;
        @BindView(R.id.layout_scheme_note)
        LinearLayout layoutSchemeNote;
        @BindView(R.id.text_scheme_note)
        TextView textSchemeNote;
        @BindView(R.id.layout_scheme_module)
        LinearLayout layoutSchemeModule;
        @BindView(R.id.btn_verify)
        Button btnVerify;
        @BindView(R.id.layout_action)
        LinearLayout layoutAction;
        @BindView(R.id.text_verify_user)
        TextView textVerifyUser;
        @BindView(R.id.text_check_time)
        TextView textCheckTime;
        @BindView(R.id.text_verify_state)
        TextView textVerifyState;
        @BindView(R.id.layout_verify_note)
        LinearLayout layoutVerifyNote;
        @BindView(R.id.text_verify_note)
        TextView textVerifyNote;
        @BindView(R.id.layout_check_report_module)
        LinearLayout layoutCheckReportModule;
        @BindArray(R.array.trouble_scheme_files)
        String[] schemeFiles;

        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) {
            AuditSchemeBean schemeBean = getItem(position);
            if (schemeBean != null) {
                textSchemeTitle.setText("方案" + (position+1));

                String dutyUserName = mTroubleBean.getDutyUserName();
                if (dutyUserName == null || dutyUserName.isEmpty()) {
                    dutyUserName = mTroubleBean.getUndertakerUserName();
                }
                if (dutyUserName != null) {
                    textDutyName.setText(dutyUserName);
                }

                String dutyCheckTime = schemeBean.getDutyCheckTime();
                if (dutyCheckTime != null)
                    textSchemeTime.setText(dutyCheckTime);

                textExecutorName.setText(schemeBean.getHandleUserName());
                String coProcessor = schemeBean.getCoprocessorName();
                if (coProcessor != null && !coProcessor.isEmpty()) {
                    textAssistsName.setText(String.format(Locale.CHINA, "(%s)", coProcessor));
                }

                layoutSchemeNote.setVisibility(View.GONE);
                String dutyPlanNote = schemeBean.getDutyPlanNote();
                if (dutyPlanNote != null && !dutyPlanNote.isEmpty()) {
                    layoutSchemeNote.setVisibility(View.VISIBLE);
                    textSchemeNote.setText(dutyPlanNote);
                }

                /*String schemeId = schemeBean.getId();
                String fileNote = schemeFiles[0];
                List<AuditFileBean> rectifyFile = schemeBean.getRectifyFile();               ///整改方案
                for (AuditFileBean auditFileBean : rectifyFile) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, schemeAdditionGallery);
                }
                fileNote = schemeFiles[1];
                List<AuditFileBean> protectNoticeFile = schemeBean.getProtectNoticeFile();   ///管道保护书
                for (AuditFileBean auditFileBean : protectNoticeFile) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, schemeAdditionGallery);
                }
                fileNote = schemeFiles[2];
                List<AuditFileBean> rectifyNoticeFile = schemeBean.getRectifyNoticeFile();   ///整改通知书
                for (AuditFileBean auditFileBean : rectifyNoticeFile) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, schemeAdditionGallery);
                }
                fileNote = schemeFiles[3];
                List<AuditFileBean> rectifyFinalFile = schemeBean.getRectifyFinalFile();    ///整改决算书
                for (AuditFileBean auditFileBean : rectifyFinalFile) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, schemeAdditionGallery);
                }

                fileNote = "附件";
                List<AuditFileBean> schemeFiles = schemeBean.getSchemeFiles();    ///附件
                for (AuditFileBean auditFileBean : schemeFiles) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, schemeAdditionGallery);
                }
                List<AuditFileBean> schemeVoice = schemeBean.getSchemeVoice();    ///音频
                for (AuditFileBean auditFileBean : schemeVoice) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, schemeAdditionGallery);
                }*/

                layoutAction.setVisibility(View.GONE);
                String auditMindName = schemeBean.getAuditMindName();
                if (auditMindName == null) {
                    layoutCheckReportModule.setVisibility(View.GONE);
                } else {
                    layoutCheckReportModule.setVisibility(View.VISIBLE);

                    textVerifyUser.setText(schemeBean.getAuditorSecretaryName());
                    String auditTime = schemeBean.getAuditTime();
                    if (auditTime != null)
                        textCheckTime.setText(auditTime);

                    layoutVerifyNote.setVisibility(View.GONE);
                    String description = schemeBean.getAuditDescription();
                    if (description != null && !description.isEmpty()) {
                        layoutVerifyNote.setVisibility(View.VISIBLE);
                        textVerifyNote.setText(description);
                    }

                    textVerifyState.setText(auditMindName);

                    /*String fileNote = "附件";
                    List<AuditFileBean> auditSchemeFiles = schemeBean.getAuditSchemeFiles();    ///附件
                    for (AuditFileBean auditFileBean : auditSchemeFiles) {
                        getSchemeFiles(schemeId, auditFileBean, fileNote, verifyAdditionGallery);
                    }
                    List<AuditFileBean> auditSchemeVoice = schemeBean.getAuditSchemeVoice();    ///音频
                    for (AuditFileBean auditFileBean : auditSchemeVoice) {
                        getSchemeFiles(schemeId, auditFileBean, fileNote, verifyAdditionGallery);
                    }*/
                }
            }
        }


        private void getSchemeFiles(String schemeId, AuditFileBean fileBean, String fileNote, final ViewGroup rootView) {
            if (fileBean != null) {
                String fileName = fileBean.getFileName();
                String filePath = fileBean.getPath();
                FileViewUtil.GetViewCallback getViewCallback = (isSuccess, view) -> {
                    if (isSuccess && view != null) {
                        rootView.addView(view);
                    }
                };
                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);
                }
            }
        }
    }
}
