package com.ycgis.macall.personalcenter.p.app;

import android.content.Context;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.Registry;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.AppGlideModule;
import com.bumptech.glide.request.RequestOptions;
import com.rs.macall.androidx.basemodel.request.OkHttpUtils;
import com.ycgis.macall.personalcenter.R;
import com.ycgis.macall.personalcenter.p.request.interceptors.HttpLoggingInterceptor;
import com.ycgis.macall.personalcenter.util.FileUtils;

import org.jetbrains.annotations.NotNull;

import java.io.InputStream;

/**
 * @Author macall
 * @Create 2019/12/18 0018
 * @Describe
 */
@GlideModule
public class MyAppGlideModule extends AppGlideModule {

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(OkHttpUtils.getClient(30,40,60,new HttpLoggingInterceptor()));
        registry.replace(GlideUrl.class, InputStream.class, factory);
    }

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //        设置缓存大小为20mb
        int memoryCacheSizeBytes = 1024 * 1024 * 50; // 50mb
        //        设置内存缓存大小
        builder.setMemoryCache(new LruResourceCache(1024 * 1024*20));
        //        根据SD卡是否可用选择是在内部缓存还是SD卡缓存
        builder.setDiskCache(new DiskLruCacheFactory(FileUtils.IMAGE_DIR,"cache_glide",memoryCacheSizeBytes));
        builder.setDefaultRequestOptions(new Glide.RequestOptionsFactory() {
            @NonNull
            @NotNull
            @Override
            public RequestOptions build() {
                return new RequestOptions()
                        .placeholder(R.drawable.icon_not_data)
                        .error(R.drawable.ic_load_imag_eerror)
                        .centerCrop()
                        .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                        .format(DecodeFormat.DEFAULT)
                        .encodeQuality(90);
            }
        });
    }

    //    针对V4用户可以提升速度
    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }
}

