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.HandleSchemeBean;
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 TroubleHandleAdapter extends ARecyclerBaseAdapter<TroubleHandleAdapter.ViewHolder, HandleSchemeBean> {
    private Context context;

    private FileViewUtil fileViewUtil;

    private List<Record> mRecords;

    private TroubleBean mTroubleBean;

    public TroubleHandleAdapter(Context context, TroubleBean troubleBean, List<HandleSchemeBean> 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_handle_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_duty_name)
        TextView textDutyName;
        @BindView(R.id.text_scheme_time)
        TextView textSchemeTime;
        @BindView(R.id.text_end_task)
        TextView textEndTask;
        @BindView(R.id.layout_scheme_note)
        LinearLayout layoutSchemeNote;
        @BindView(R.id.text_scheme_note)
        TextView textSchemeNote;
        @BindView(R.id.scheme_addition_gallery)
        LinearLayout fileAdditionGallery;
        @BindView(R.id.layout_scheme_file_addition_area)
        LinearLayout layoutFileAdditionArea;

        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) {
            HandleSchemeBean schemeBean = getItem(position);
            if (schemeBean != null) {
                textCount.setText(position + 1 + "");
                textDutyName.setText(schemeBean.getCreateUserName());
                String handleTime = schemeBean.getHandleTime();
                if (handleTime != null)
                    textSchemeTime.setText(handleTime);
                int isEnd = schemeBean.getIsEnd();
                textEndTask.setText(isEnd == 1 ? "是" : "否");

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

                String fileNote = "附件";
                String schemeId = schemeBean.getId();
                List<AuditFileBean> auditSchemeFiles = schemeBean.getHandlePhotos();    ///附件
                for (AuditFileBean auditFileBean : auditSchemeFiles) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, fileAdditionGallery);
                }
                List<AuditFileBean> auditSchemeVoice = schemeBean.getHandleAudios();    ///音频
                for (AuditFileBean auditFileBean : auditSchemeVoice) {
                    getSchemeFiles(schemeId, auditFileBean, fileNote, fileAdditionGallery);
                }
            }
        }
    }

    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);
            }
        }
    }
}
