/**
 *
 */
package com.stanfy.enroscar.images.cache;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;

import android.content.Context;
import android.os.Environment;

import com.stanfy.enroscar.beans.Bean;
import com.stanfy.enroscar.beans.BeansContainer;
import com.stanfy.enroscar.beans.EnroscarBean;
import com.stanfy.enroscar.images.ImagesManager;
import com.stanfy.enroscar.net.cache.BaseFileResponseCache;
import com.stanfy.enroscar.net.cache.CacheEntry;
import com.stanfy.enroscar.net.cache.CacheTimeRule;
import com.stanfy.enroscar.sdkdep.SdkDepUtils;
import com.stanfy.enroscar.sdkdep.SdkDependentUtils;

/**
 * File-based cache used by images manager.
 * @author Roman Mazur (Stanfy - http://stanfy.com)
 */
@EnroscarBean(value = ImagesManager.CACHE_BEAN_NAME, contextDependent = true)
public class ImageFileCache extends BaseFileResponseCache implements Bean {

  /** Bigger images cache (10M). */
  public static final long MAX_SIZE = 10 * 1024 * 1024;

  /** Application context. */
  private final Context context;
  
  public ImageFileCache(final Context context) {
    this.context = context;
  }
  
  @Override
  public void onInitializationFinished(final BeansContainer beansContainer) {
    final String eState = Environment.getExternalStorageState();
    SdkDependentUtils sdkUtils = SdkDepUtils.get(context);
    final File baseDir = Environment.MEDIA_MOUNTED.equals(eState)
        ? sdkUtils.getExternalCacheDir(context)
        : context.getCacheDir();
    setWorkingDirectory(new File(baseDir, "images"));
    setMaxSize(MAX_SIZE);
    super.onInitializationFinished(beansContainer);
  }
  
  @Override
  protected CacheEntry createCacheEntry() { return new ImageCacheEntry(); }

  /** Image cache entry. */
  public static class ImageCacheEntry extends CacheEntry {
    /** Image type identifier. */
    int imageType = -1;

    @Override
    protected void writeMetaData(final Writer writer) throws IOException {
      writeInt(writer, imageType);
    }

    @Override
    protected void readMetaData(final InputStream in) throws IOException {
      imageType = readInt(in);
    }

  }

  /** Cache rule. */
  public static class ImageTypeBasedCacheRule extends CacheTimeRule {
    /** Image type identifier. */
    final int imageType;

    public ImageTypeBasedCacheRule(final int imageType, final long time) {
      super(time);
      this.imageType = imageType;
    }

    @Override
    public boolean matches(final CacheEntry cacheEntry) {
      return cacheEntry instanceof ImageCacheEntry && imageType == ((ImageCacheEntry)cacheEntry).imageType;
    }

    @Override
    protected String matcherToString() {
      return "imageType=" + imageType;
    }

  }

  /** 'Until' cache rule. */
  public static class ImageTypeBasedUntilCacheRule extends ImageTypeBasedCacheRule {

    public ImageTypeBasedUntilCacheRule(final int imageType, final long time) {
      super(imageType, time);
    }

    @Override
    public boolean isActual(final long createTime) { return isUntilActual(createTime, getTime()); }

  }

}

@@ -210,16 +210,41 @@
 ronment;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -1271,24 +1271,67 @@
 ontainer) %7B%0A
+    if (getWorkingDirectory() == null) %7B%0A  
     final St
@@ -1387,16 +1387,18 @@
 ();%0A    
+  
 SdkDepen
@@ -1448,21 +1448,17 @@
 t);%0A    
-final
+ 
  File ba
@@ -1514,16 +1514,18 @@
         
+  
 ? sdkUti
@@ -1556,16 +1556,18 @@
 ontext)%0A
+  
         
@@ -1587,24 +1587,174 @@
 CacheDir();%0A
+      if (baseDir == null) %7B%0A        baseDir = context.getCacheDir();%0A        Log.w(TAG, %22Could not locate cache on the external storage%22);%0A      %7D%0A  
     setWorki
@@ -1795,16 +1795,53 @@
 ges%22));%0A
+    %7D%0A    if (getMaxSize() == 0) %7B%0A  
     setM
@@ -1858,16 +1858,22 @@
 _SIZE);%0A
+    %7D%0A
     supe

change images file cache directory selection