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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.tenlionsoft.baselib.R;
import com.tenlionsoft.baselib.core.beans.BaseImageBean;
import com.tenlionsoft.baselib.utils.ConvertUtils;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * 作者: adam
 * 日期: 2020/5/14 - 5:34 PM
 * 邮箱: itgaojian@163.com
 * 描述:
 */
public class BaseBannerImageAdapter extends BannerAdapter<BaseImageBean, BannerHolder> {

    public BaseBannerImageAdapter(List<BaseImageBean> datas) {
        super(datas);
    }

    @Override
    public BannerHolder onCreateHolder(ViewGroup parent, int viewType) {
//        ImageView imageView = new ImageView(parent.getContext());
//        //注意，必须设置为match_parent，这个是viewpager2强制要求的
//        imageView.setLayoutParams(new ViewGroup.LayoutParams(
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT));
//        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner_layout, parent, false);
        return new BannerHolder(itemView);
    }

    @Override
    public void onBindView(BannerHolder holder, BaseImageBean data, int position, int size) {
        RoundedCornersTransform roundedCornersTransform = new RoundedCornersTransform(holder.itemView.getContext(), ConvertUtils.dp2px(2));
        roundedCornersTransform.setNeedCorner(false, false, false, false);
        RequestOptions options = new RequestOptions()
                .override(500, 600)
                .error(R.drawable.ic_img_default_banner)
                .placeholder(R.drawable.ic_img_default_banner)
                .transform(roundedCornersTransform)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(holder.itemView.getContext())
                .asBitmap()
                .load(data.getImgUrl())
                .apply(options)
                .into(holder.mIvContent);
    }
}
