package com.sybotan.android.demo.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;


import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.sybotan.android.demo.retrofit.API;
import com.sybotan.android.demo.retrofit.Constant;
import com.sybotan.android.demo.tools.CommonUtils;
import com.sybotan.android.demo.tools.GlideApp;
import com.sybotan.android.demo.tools.PhotoViewAttacher;
import com.sybotan.android.demo.tools.ToastUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * viewpager展示多张图片通用适配器
 * Created by wangmaobo on 2018/8/15.
 */
public class ViewPagerImageAdapter extends PagerAdapter {
    private ArrayList<String> mImages = new ArrayList<>();
    private ArrayList<HashMap<String, Object>> mBitmaps = new ArrayList<>();
    private Context mContext;
    private DismissListener mListener;
    private boolean isFile;

    public ViewPagerImageAdapter(Context context, ArrayList<String> list, boolean isFile) {
        mContext = context;
        mImages = list;
        this.isFile = isFile;
    }

    public ViewPagerImageAdapter(Context context, ArrayList<HashMap<String, Object>> list) {
        mContext = context;
        mBitmaps = list;
    }

    public ViewPagerImageAdapter setListener(DismissListener listener) {
        mListener = listener;
        return this;
    }

    @Override
    public int getCount() {
        return mImages.size() > mBitmaps.size() ? mImages.size() : mBitmaps.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());

        PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
        attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float v, float v1) {
                if (null != mListener) {
                    mListener.dismissM();
                }
            }

            @Override
            public void onOutsidePhotoTap() {
                if (null != mListener) {
                    mListener.dismissM();
                }
            }
        });
        if (isFile) {
            loadImage(mContext, mImages.get(position), photoView, isFile);
        } else if (!CommonUtils.IsNull(mBitmaps)) {
            if (null != mBitmaps.get(position).get("Image_Bit") && mBitmaps.get(position).get("Image_Bit") instanceof Bitmap) {
                photoView.setImageBitmap((Bitmap) mBitmaps.get(position).get("Image_Bit"));
            }
        } else {
            String loadUrl = mImages.get(position);
            if (TextUtils.isEmpty(loadUrl)) {
                ToastUtils.showMyToast("图片已损坏");
                return photoView;
            }
            if (!loadUrl.startsWith("http")) {
                loadUrl = Constant.ServerImgIPDown + mImages.get(position);
            }
            loadImage(mContext, loadUrl, photoView, isFile);
        }
        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return photoView;
    }

    public static void loadImage(Context context, String path, ImageView imageView, boolean isFile) {
        if (isFile) {
            GlideApp.with(context)
                    .load(new File(path))
                    .into(imageView);
        } else {
            if (path.contains("key=null") || path.endsWith("key=")) {
                return;
            }
            GlideApp.with(context)
                    .load(path)
//                    .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
//                    .signature(new ObjectKey(String.valueOf(System.currentTimeMillis())))
                    .into(imageView);
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    public interface DismissListener {
        void dismissM();
    }
}
