package com.anotherdev.android.robospice;

import com.anotherdev.android.robospice.request.Cacheable;
import com.octo.android.robospice.SpiceManager;
import com.octo.android.robospice.request.SpiceRequest;
import com.octo.android.robospice.request.listener.RequestListener;
import com.octo.android.robospice.retry.RetryPolicy;

import javax.annotation.Nullable;

public class RequestCreator {

    private final SpiceManager mManager;

    private Optional<String> mCacheKey = Optional.absent();
    private Optional<Long> mCacheExpiry = Optional.absent();
    private Optional<Boolean> mAcceptDirtyCache = Optional.absent();
    private Optional<RetryPolicy> mRetryPolicy = Optional.absent();


    RequestCreator(SpiceManager manager) {
        if (manager == null) {
            throw new IllegalArgumentException("SpiceManager must not be null");
        }
        mManager = manager;
    }

    public RequestCreator cache(@Nullable String key) {
        mCacheKey = Optional.from(key);
        return this;
    }

    public RequestCreator expiry(long durationInMillis) {
        mCacheExpiry = Optional.from(durationInMillis);
        return this;
    }

    public RequestCreator acceptDirtyCache(boolean enable) {
        mAcceptDirtyCache = Optional.from(enable);
        return this;
    }

    public RequestCreator retry(RetryPolicy retryPolicy) {
        mRetryPolicy = Optional.from(retryPolicy);
        return this;
    }

    public <T> RequestExecutor<T> notify(RequestListener<T> listener) {
        return new RequestExecutor<>(this, listener);
    }

    public <T> void execute(SpiceRequest<T> request) {
        execute(request, null);
    }

    <T> void execute(SpiceRequest<T> request, RequestListener<T> listener) {
        if (request instanceof Cacheable) {
            Cacheable cacheable = (Cacheable) request;
            Optional<String> cacheKey = Optional.from(cacheable.getCacheKey());

            final String key = mCacheKey.or(cacheKey).orNull();
            final long expiry = mCacheExpiry.or(cacheable.getCacheDurationInMillis());
            final boolean acceptDirtyCache = mAcceptDirtyCache.or(cacheable.isAcceptingDirtyCache());

            if (acceptDirtyCache) {
                mManager.getFromCacheAndLoadFromNetworkIfExpired(request, key, expiry, listener);
            } else {
                mManager.execute(request, key, expiry, listener);
            }
        } else {
            mManager.execute(request, listener);
        }
    }
}

@@ -139,24 +139,88 @@
 iceManager;%0A
+import com.octo.android.robospice.persistence.DurationInMillis;%0A
 import com.o
@@ -1813,184 +1813,664 @@
     
-if (request instanceof Cacheable) %7B%0A            Cacheable cacheable = (Cacheable) request;%0A            Optional%3CString%3E cacheKey = Optional.from(cacheable.getCacheKey());%0A%0A    
+Optional%3CString%3E requestKey = Optional.absent();%0A        Optional%3CLong%3E requestExpiry = Optional.absent();%0A        Optional%3CBoolean%3E requestAcceptDirty = Optional.absent();%0A%0A        if (request instanceof Cacheable) %7B%0A            Cacheable cacheable = (Cacheable) request;%0A            requestKey = Optional.from(cacheable.getCacheKey());%0A            requestExpiry = Optional.from(cacheable.getCacheDurationInMillis());%0A            requestAcceptDirty = Optional.from(cacheable.isAcceptingDirtyCache());%0A        %7D%0A%0A        if (mRetryPolicy.isPresent()) %7B%0A            // Override RetryPolicy%0A            request.setRetryPolicy(mRetryPolicy.get());%0A        %7D%0A%0A
     
@@ -2505,21 +2505,23 @@
 eKey.or(
-cache
+request
 Key).orN
@@ -2523,28 +2523,24 @@
 ).orNull();%0A
-    
         fina
@@ -2571,34 +2571,34 @@
 piry.or(
-cacheable.getCache
+requestExpiry).or(
 Duration
@@ -2597,34 +2597,47 @@
 DurationInMillis
-()
+.ALWAYS_EXPIRED
 );%0A            f
@@ -2627,28 +2627,24 @@
 D);%0A        
-    
 final boolea
@@ -2689,49 +2689,40 @@
 .or(
-cacheable.is
+request
 Accept
-ing
 Dirty
-Cache());%0A%0A    
+).or(false);%0A%0A
     
@@ -2745,28 +2745,24 @@
 rtyCache) %7B%0A
-    
             
@@ -2847,28 +2847,24 @@
 r);%0A        
-  
-  
 %7D else %7B%0A   
@@ -2852,36 +2852,32 @@
        %7D else %7B%0A
-    
             mMan
@@ -2926,88 +2926,8 @@
 r);%0A
-            %7D%0A        %7D else %7B%0A            mManager.execute(request, listener);%0A
     

fix local cache key for basic request