package com.uiui.zyappstore.image;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.uiui.zyappstore.R;


// ImageLoader 加载图片 清除缓存
public class ImageLoader {
    // 优先级，设置图片加载的顺序 Priority.LOW Priority.NORMAL Priority.HIGH  Priority.IMMEDIATE
    public static Priority p1 = Priority.LOW;
    public static Priority p2 = Priority.NORMAL;
    public static Priority p3 = Priority.HIGH;
    public static Priority p4 = Priority.IMMEDIATE;
    private static volatile ImageLoader imageLoader;

    RequestOptions options = new RequestOptions()
            .centerCrop()
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .placeholder(R.mipmap.ic_launcher)
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .priority(ImageLoader.p4);


    RequestOptions options1 = new RequestOptions()
            .centerCrop()
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .placeholder(R.mipmap.ic_launcher)
            .fitCenter()
            .error(R.mipmap.ic_launcher)
            .priority(ImageLoader.p3);


    RequestOptions options2 = new RequestOptions()
            .centerCrop()
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .fitCenter()
//            .transform(new GlideCircleTransformation())
            .transform(new RoundedCorners(30))
            .priority(ImageLoader.p3);

    private ImageLoader() {

    }

    public static ImageLoader getDefault() {
        if (imageLoader == null) {
            synchronized (ImageLoader.class) {
                if (imageLoader == null) {
                    imageLoader = new ImageLoader();
                }
            }
        }
        return imageLoader;
    }

    // ImageView 加载gif图片
    public void loadGif(Context context, Object drawable, ImageView view) {
        if (context != null && !((Activity) context).isFinishing()) {
            Glide.with(context)
                    .load(drawable)
                    .apply(options)
                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(view);
        }
    }

    // ImageView 加载本地图片
    public void load(Context context, Object drawable, ImageView view) {
        if (context != null && !((Activity) context).isFinishing()) {
            Glide.with(context).load(drawable)
                    .apply(options)
                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(view);
        }
    }

    // View 加载背景图片
    public void loadBackground(Context context, int drawable, final View view) {
        if (context != null && !((Activity) context).isFinishing()) {
            Glide.with(context)
                    .load(drawable)
                    .apply(options)
                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                            view.setBackground(resource);
                        }
                    });
        }
    }

    // ImageView 加载网络图片
    public void loadUrl(Context context, String url, ImageView view) {
        if (context != null && !((Activity) context).isFinishing()) {
            Glide.with(context).load(url)
                    .apply(options1)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(view);
        }
    }

    // ImageView 加载本地圆形图片
    public void loadLocalRound(Context context, int drawable, ImageView view) {
        if (context != null && !((Activity) context).isFinishing()) {
            Glide.with(context)
                    .load(drawable)
                    .apply(options)
                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(view);
        }
    }

    // ImageView 加载本地圆形图片
    public void loadLocalRound(Context context, Drawable drawable, ImageView view) {
        if (context != null && !((Activity) context).isFinishing()) {
            Glide.with(context)
                    .load(drawable)
//                    .transform(new GlideCircleWithBorder(context))
//                    .apply(options)
//                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(view);
        }
    }

    // ImageView 加载网络圆形图片
    public void loadUrlRound(Context context, String url, ImageView view) {
        if (context != null && !((Activity) context).isFinishing()) {
            Glide.with(context).load(url)
//                    .transform(new GlideCircleWithBorder(context))
                    .apply(options2)
                    .transition(new DrawableTransitionOptions().dontTransition())
                    .into(view);
        }
    }
}
