package com.alexstyl.specialdates.images;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.ImageView;

import com.alexstyl.specialdates.BuildConfig;
import com.alexstyl.specialdates.addevent.ui.AvatarCameraButtonView;
import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.utils.L;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class ImageLoader {

    private static final boolean DEBUG = BuildConfig.DEBUG;
    private static final long FB_LIFE_TIME = DateUtils.DAY_IN_MILLIS * 3;

    public static void init(Context context) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .threadPoolSize(10)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .diskCache(new LimitedAgeDiskCache(StorageUtils.getCacheDirectory(context), FB_LIFE_TIME))
                .imageDecoder(new NutraBaseImageDecoder(BuildConfig.DEBUG))
                .imageDownloader(new ImageDownloader(context));

        L.writeLogs(DEBUG);
        // Initialize ImageLoader with configuration.
        com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config.build());
    }

    private final DisplayImageOptions displayImageOptions;
    private final com.nostra13.universalimageloader.core.ImageLoader loader;

    ImageLoader(DisplayImageOptions circleImageOptions) {
        this.displayImageOptions = circleImageOptions;
        loader = UniversalImageLoader.newInstance();
    }

    public static ImageLoader createWidgetThumbnailLoader() {
        DisplayImageOptions options = UniversalImageLoader.buildWidgetImageOptions();
        return new ImageLoader(options);
    }

    public static ImageLoader createCircleThumbnailLoader(Resources resources) {
        DisplayImageOptions options = UniversalImageLoader.buildCircleImageOptions(resources);
        return new ImageLoader(options);
    }

    public static ImageLoader createSquareThumbnailLoader(Resources resources) {
        DisplayImageOptions options = UniversalImageLoader.buildSquareImageOptions(resources);
        return new ImageLoader(options);
    }

    public void displayThumbnail(Uri imagePath, ImageView imageView) {
        loader.displayImage(imagePath.toString(), imageView, displayImageOptions);
    }

    public void displayThumbnail(Uri imagePath, ImageAware imageView) {
        loader.displayImage(imagePath.toString(), imageView, displayImageOptions);
    }

    public void loadImage(Uri imagePath, AvatarCameraButtonView avatarView, final OnImageLoadedCallback callback) {
        loader.loadImage(imagePath.toString(), new ImageSize(avatarView.getWidth(), avatarView.getHeight()), new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                callback.onImageLoaded(loadedImage);
            }
        });
    }

    public Bitmap loadBitmap(Uri imagePath, int width, int height) {
        ImageSize imageSize = new ImageSize(width, height);
        return loader.loadImageSync(imagePath.toString(), imageSize);
    }

    public void loadBitmapAsync(Uri imagePath, int size, SimpleImageLoadingListener listener) {
        ImageSize imageSize = new ImageSize(size, size);
        loader.loadImage(imagePath.toString(), imageSize, displayImageOptions, listener);
    }

    public void resume() {
        loader.resume();
    }

    public void pause() {
        loader.pause();
    }
}

@@ -166,46 +166,8 @@
 ri;%0A
-import android.text.format.DateUtils;%0A
 impo
@@ -184,24 +184,24 @@
 .view.View;%0A
+
 import andro
@@ -341,167 +341,8 @@
 ew;%0A
-import com.nostra13.universalimageloader.cache.disc.impl.LimitedAgeDiskCache;%0Aimport com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;%0A
 impo
@@ -819,69 +819,8 @@
 s.L;
-%0Aimport com.nostra13.universalimageloader.utils.StorageUtils;
 %0A%0Apu
@@ -908,82 +908,8 @@
 BUG;
-%0A    private static final long FB_LIFE_TIME = DateUtils.DAY_IN_MILLIS * 3;
 %0A%0A  
@@ -1164,232 +1164,53 @@
    .
-diskCacheFileNameGenerator(new Md5FileNameGenerator())%0A                .tasksProcessingOrder(QueueProcessingType.LIFO)%0A                .diskCache(new LimitedAgeDiskCache(StorageUtils.getCacheDirectory(context), FB_LIFE_TIME)
+tasksProcessingOrder(QueueProcessingType.LIFO
 )%0A  
@@ -3840,10 +3840,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Remove disk cache