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.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.CoordinateSchemeBean;
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 butterknife.BindView;
import butterknife.ButterKnife;

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

public class TroubleCoordinateAdapter extends ARecyclerBaseAdapter<TroubleCoordinateAdapter.ViewHolder, CoordinateSchemeBean> {
    private Context context;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private TroubleBean mTroubleBean;

    public TroubleCoordinateAdapter(Context context, TroubleBean troubleBean, List<CoordinateSchemeBean> coordinateList) {
        super(context, coordinateList);
        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_coordiante_detail, 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_count)
        TextView textCount;
        @BindView(R.id.text_verify_user)
        TextView textVerifyUser;
        @BindView(R.id.text_check_time)
        TextView textCheckTime;
        @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;
        @BindView(R.id.verify_addition_gallery)
        LinearLayout verifyAdditionGallery;
        @BindView(R.id.layout_verify_addition_area)
        LinearLayout layoutVerifyAdditionArea;

        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) {
            CoordinateSchemeBean schemeBean = getItem(position);
            if (schemeBean != null) {
                textCount.setText(position + 1  + "");
                textVerifyUser.setText(schemeBean.getCreateUserName());
                String checkTime = schemeBean.getCheckTime();
                if (checkTime != null)
                    textCheckTime.setText(checkTime);

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

                String fileNote = "附件";
                String schemeId = schemeBean.getId();
                List<AuditFileBean> auditSchemeFiles = schemeBean.getCoordinatePhotos();    ///附件
                for (AuditFileBean auditFileBean : auditSchemeFiles) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, verifyAdditionGallery);
                }
                List<AuditFileBean> auditSchemeVoice = schemeBean.getCoordinateAudios();    ///音频
                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);
            }
        }
    }
}
