package com.unis.sxzcl.widget.banner;

import android.view.View;

import com.bumptech.glide.Glide;
import com.unis.sxzcl.R;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.api.widget.Widget;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;


/**
 * 包名: com.bskapp.bskapp.widget
 * 类名: BannerUtils
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/8/22 21:12
 * 更新人:
 * 更新时间: 2020/8/22 21:12
 * 更新说明:
 * 版本号: 1.0
 */
public class BannerUtils {
    public Banner mBanner;
    private ArrayList<String> list = new ArrayList<>();

    public BannerUtils(Banner banner) {
        mBanner = banner;
    }

    public void setImages(List<String> imageUrls) {
        for (Object s : imageUrls) {
            if (s instanceof String)
                list.add((String) s);
        }
        mBanner.setAdapter(new BannerImageAdapter<String>(imageUrls) {
            @Override
            public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                Glide.with(holder.imageView)
                        .load(data)
                        .placeholder(R.drawable.ic_img_loading)
                        .error(R.drawable.no_banner)
                        .into(holder.imageView);
                holder.imageView.setTag(position);
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showBigImg(list, (Integer) v.getTag());
                    }
                });
            }
        }).setIndicator(new CircleIndicator(mBanner.getContext()));
    }

    public void setImagesResId(List<Integer> imageUrls) {
        mBanner.setAdapter(new BannerImageAdapter<Integer>(imageUrls) {
            @Override
            public void onBindView(BannerImageHolder holder, Integer resId, int position, int size) {
                Glide.with(holder.imageView)
                        .load(resId)
                        .centerCrop()
                        .placeholder(R.drawable.ic_img_loading)
                        .error(R.drawable.no_banner)
                        .into(holder.imageView);
                holder.imageView.setTag(position);
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }
        }).setIndicator(new CircleIndicator(mBanner.getContext()));
    }

    private void showBigImg(ArrayList<String> list, int position) {
        Widget widget = Widget.newLightBuilder(mBanner.getContext())
                .title("查看图片").build();
        Album.gallery(mBanner.getContext())
                .checkedList(list)
                .currentPosition(position)
                .widget(widget)
                .start();
    }

}
