package com.tofly.ghrq_route.common.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.common.util.file.FileViewUtil;
import com.tofly.ghrq_route.module.buildManage.buildFilesInfo;
import com.tofly.ghrq_route.common.util.DownLoadUtil;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BuildappendAdapter extends BaseAdapter {

    private Context context;
    private List<buildFilesInfo> data;
    private DownLoadUtil downLoadUtil;

    private FileViewUtil fileViewUtil;

    public BuildappendAdapter(Context context, List<buildFilesInfo> data) {
        this.context = context;
        this.data = data;
        downLoadUtil = new DownLoadUtil(context);
        fileViewUtil = new FileViewUtil(context);
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"ViewHolder", "InflateParams"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_build_appendix, null);
        BuildAppendViewHolder viewHolder = new BuildAppendViewHolder(convertView);
        convertView.setTag(viewHolder);
        final buildFilesInfo news = data.get(position);
        if (!TextUtils.isEmpty(news.getName())) {
            viewHolder.text_file_name.setText(news.getName());
        }
        if (!TextUtils.isEmpty(news.getPath())) {
            String schemeId = news.getId();
            File file = new File(news.getPath());
            String type = file.getName().split("\\.")[1];
            String url = NetRequestURL.newApi_dyrq.getFormattedPicture(file.getPath());
            if (type.equals("jpg") || type.equals("png") || type.equals("jpeg")) {
                viewHolder.fl_image.setVisibility(View.VISIBLE);
                viewHolder.audioLayout.setVisibility(View.GONE);
                if (!file.exists()) {
                    downLoadUtil.getPhotoView(url, null, viewHolder.imageFileIcon, viewHolder.progressBar);
                } else {
                    downLoadUtil.getPhotoView(null, file.getPath(), viewHolder.imageFileIcon, viewHolder.progressBar);
                }
            } else if (type.equals("mp4")) {
                fileViewUtil.getVideoFileView(schemeId, file.getPath(), file.getName(), null, (isSuccess, view) -> {
                    if (isSuccess && view != null) {
                        viewHolder.fl_image.setVisibility(View.VISIBLE);
                        viewHolder.audioLayout.setVisibility(View.GONE);
                        viewHolder.fl_image.addView(view);
                    }
                });
            } else if (type.equals("amr")) {
                viewHolder.audioLayout.setVisibility(View.VISIBLE);
                viewHolder.fl_image.setVisibility(View.VISIBLE);
                if (!file.exists()) {
                    if (!TextUtils.isEmpty(news.getId())) {
                        downLoadUtil.getAudioView(url, null, news.getId(), viewHolder.audioLayout, viewHolder.progressBar);
                    }
                } else {
                    downLoadUtil.getAudioView(null, file.getPath(), news.getId(), viewHolder.audioLayout, viewHolder.progressBar);
                }
            } else if (type.equals("doc") || type.equals("docx") || type.equals("xls") || type.equals("xlsx") || type.equals("pdf")
                    || type.equals("txt") || type.equals("ppt") || type.equals("pptx")) {
                viewHolder.audioLayout.setVisibility(View.GONE);
                viewHolder.fl_image.setVisibility(View.GONE);
                if (!file.exists()) {
                    downLoadUtil.getFile(viewHolder.text_file_name, file.getName(), url, null, viewHolder.progressBar);
                } else {
                    downLoadUtil.getFile(viewHolder.text_file_name, file.getName(), null, file.getPath(), viewHolder.progressBar);
                }
            } else {
                ToastUtil.showToast(context, "无法识别的格式");
            }
        }
        return convertView;
    }

    public static class BuildAppendViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_file_icon)
        ImageView imageFileIcon;
        @BindView(R.id.text_file_name)
        TextView text_file_name;
        @BindView(R.id.fl_image)
        FrameLayout fl_image;
        @BindView(R.id.progress_image)
        ProgressBar progressBar;
        @BindView(R.id.layout_audio_signal)
        LinearLayout audioLayout;
        View itemView;

        public BuildAppendViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }
}