package com.ttstd.dialer.glide;

import android.content.Context;
import android.graphics.drawable.PictureDrawable;

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.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;
import com.caverock.androidsvg.SVG;
import com.ttstd.dialer.glide.svg.SvgDecoder;
import com.ttstd.dialer.glide.svg.SvgDrawableTranscoder;

import org.jetbrains.annotations.NotNull;

import java.io.InputStream;

@GlideModule
public class CustomGlideModule extends AppGlideModule {

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);
        //内存缓存
        int memoryCacheSizeBytes = 1024 * 1024 * 16; // 16mb
        builder.setMemoryCache(new LruResourceCache(memoryCacheSizeBytes));
        //Bitmap 池
        int bitmapPoolSizeBytes = 1024 * 1024 * 32; // 32mb
        builder.setBitmapPool(new LruBitmapPool(bitmapPoolSizeBytes));
        //磁盘缓存
        int diskCacheSizeBytes = 1024 * 1024 * 64; // 64MB
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, diskCacheSizeBytes));

    }

    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        registry.register(SVG.class, PictureDrawable.class, new SvgDrawableTranscoder())
                .append(InputStream.class, SVG.class, new SvgDecoder());
    }

    // Disable manifest parsing to avoid adding similar modules twice.
    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }
}
