package com.wd.workoffice.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.RequestOptions;
import com.wd.workoffice.app.WorkApp;

import java.security.MessageDigest;

import androidx.annotation.NonNull;

import static com.bumptech.glide.load.resource.bitmap.VideoDecoder.FRAME_OPTION;

public class GlideUtils {

    public static GlideRequests getRequest() {
        return GlideApp.with(WorkApp.getApp());
    }

    public static void circleImg(ImageView iv, String url) {
        RequestOptions mRequestOptions = RequestOptions.circleCropTransform()
                .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                .skipMemoryCache(true);//不做内存缓存

        GlideApp.with(WorkApp.getApp()).load(url)
//                .error(R.mipmap.info_head)
                .circleCrop().into(iv);
    }

//    public static void videoShow(Context context, ImageView imageView, String url) {
//        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//        Glide.with(context).setDefaultRequestOptions(new RequestOptions()
//                .frame(1000)//单位微秒
//                .centerCrop()
//                .error(R.mipmap.img_default)
//                .placeholder(R.mipmap.img_default))
//                .load(url)
//                .into(imageView);
//    }

    public static void videoShow(final Context context, ImageView imageView, String uri) {
        RequestOptions requestOptions = RequestOptions.frameOf(2000);
        requestOptions.set(FRAME_OPTION, MediaMetadataRetriever.OPTION_CLOSEST);
        requestOptions.transform(new BitmapTransformation() {
            @Override
            protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
                return toTransform;
            }

            @Override
            public void updateDiskCacheKey(MessageDigest messageDigest) {
                try {
                    messageDigest.update((context.getPackageName() + "RotateTransform").getBytes("utf-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        Glide.with(context).load(uri).apply(requestOptions).into(imageView);
    }

}
