package com.tenlionsoft.baselib.core.widget.base;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.beans.AddFileBean;

import java.util.List;

/**
 * 作者 : Adam on 2018/11/10.
 * 邮箱 : itgaojian@163.com
 * 描述 : 添加图片的适配器
 */
public class AddFileAdapter extends BaseRecyclerAdapter<AddFileBean, AddFileHolder> {
    public AddFileAdapter(Context ctx, List<AddFileBean> list) {
        super(ctx, list);
    }

    @Override
    public AddFileHolder createHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext)
            .inflate(R.layout.item_apply_service_photo, parent, false);
        return new AddFileHolder(itemView);
    }

    @Override
    public void bindHolder(AddFileHolder applyServicePhotoHolder, int i) {
        if (TextUtils.isEmpty(mData.get(i).getPath())) {
            applyServicePhotoHolder.mIvDelete.setVisibility(View.GONE);
            Glide.with(mContext)
                .load(R.drawable.ic_file_add)
                .into(applyServicePhotoHolder.mIvPhoto);
        } else {
            if (mDeleteListener != null) {
                applyServicePhotoHolder.mIvDelete.setVisibility(View.VISIBLE);
                applyServicePhotoHolder.mIvDelete.setOnClickListener(v -> mDeleteListener.onDelete(mData.get(i), i));
            } else {
                applyServicePhotoHolder.mIvDelete.setVisibility(View.GONE);
            }
            applyServicePhotoHolder.mTvFileName.setText(mData.get(i).getFileName());
            RequestOptions options = new RequestOptions()
                .transform(new GlideRoundTransform(mContext, 5))
                .error(R.drawable.ic_img_load_err)
                .placeholder(R.drawable.ic_img_loading);
            if (mData.get(i).getFileType() == 1) {
                //文档
                Glide.with(mContext)
                    .load(R.drawable.ic_file_word)
                    .apply(options)
                    .into(applyServicePhotoHolder.mIvPhoto);
            } else if (mData.get(i).getFileType() == 2) {
                //图片
                Glide.with(mContext)
                    .load(mData.get(i).getPath())
                    .apply(options)
                    .into(applyServicePhotoHolder.mIvPhoto);
            } else if (mData.get(i).getFileType() == 3) {
                //视频
                Glide.with(mContext)
                    .load(R.drawable.ic_file_video)
                    .apply(options)
                    .into(applyServicePhotoHolder.mIvPhoto);
            } else if (mData.get(i).getFileType() == 4) {
                //音频
                Glide.with(mContext)
                    .load(R.drawable.ic_file_audio)
                    .apply(options)
                    .into(applyServicePhotoHolder.mIvPhoto);
            }
        }
    }

    private OnDeleteListener mDeleteListener;

    public void addOnDeleteListener(OnDeleteListener listener) {
        this.mDeleteListener = listener;
    }

    public interface OnDeleteListener {
        void onDelete(AddFileBean bean, int i);
    }
}
