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.AddPhotoBean;

import java.util.List;

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

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

    @Override
    public void bindHolder(AddPhotoHolder applyServicePhotoHolder, int i) {
        if (TextUtils.isEmpty(mData.get(i).getPath())) {
            applyServicePhotoHolder.mIvDelete.setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(R.drawable.ic_add_photo)
                    .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);
            }
            RequestOptions options = new RequestOptions()
                    .transform(new GlideRoundTransform(mContext, 5))
                    .error(R.drawable.ic_img_load_err)
                    .placeholder(R.drawable.ic_img_loading);
            Glide.with(mContext)
                    .load(mData.get(i).getPath())
                    .apply(options)
                    .into(applyServicePhotoHolder.mIvPhoto);
        }
    }

    private OnDeleteListener mDeleteListener;

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

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