package com.zhaohaoting.framework.imagepicker;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.zhaohaoting.framework.R;
import com.zhaohaoting.framework.imagepicker.loader.ImageLoader;

import java.io.File;

/**
 * 该项目时间紧急只能采用glide + fresco方案
 * 后续如果维护将统一图片加载框架
 */
public class ZImageLoader implements ImageLoader {

    @Override
    public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {
        if (activity == null || activity.isFinishing()) return;
        Glide.with(activity).asBitmap().listener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                resource.getWidth();
                return false;
            }
        })                         //配置上下文
                .load(Uri.fromFile(new File(path)))      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                .apply(new RequestOptions().error(R.mipmap.default_img_failed).placeholder(R.mipmap.default_place_holder).diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate())           //设置错误图片
                .into(imageView);
    }

    @Override
    public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {
        if (activity == null || activity.isFinishing()) return;

        if (path.startsWith("http")) {
            Glide.with(activity)                             //配置上下文
                    .load(path)      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate())           //设置错误图片
                    .into(imageView);
        } else {
            Glide.with(activity)                             //配置上下文
                    .load(Uri.fromFile(new File(path)))      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                    .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate())           //设置错误图片
                    .into(imageView);
        }
    }

    @Override
    public void displayImageUrl(Activity activity, String url, ImageView imageView, int width, int height) {
        if (activity == null || activity.isFinishing()) return;

        Glide.with(activity)                             //配置上下文
                .load(url)      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate())          //设置错误图片
                .into(imageView);
    }

    @Override
    public void clearMemoryCache() {
    }
}
