package com.pushtorefresh.storio.sample.db.resolvers;

import android.database.Cursor;
import android.support.annotation.NonNull;

import com.pushtorefresh.storio.sample.db.entities.Tweet;
import com.pushtorefresh.storio.sample.db.entities.User;
import com.pushtorefresh.storio.sample.db.entities.UserWithTweets;
import com.pushtorefresh.storio.sample.db.tables.TweetsTable;
import com.pushtorefresh.storio.sqlite.StorIOSQLite;
import com.pushtorefresh.storio.sqlite.operations.get.GetResolver;
import com.pushtorefresh.storio.sqlite.queries.Query;
import com.pushtorefresh.storio.sqlite.queries.RawQuery;

import java.util.List;

public final class UserWithTweetsGetResolver extends GetResolver<UserWithTweets> {

    // We can even reuse existing get resolvers for our needs
    // But, you can always write custom code, of course.
    @NonNull
    private final GetResolver<User> userGetResolver;

    // Sorry for this hack :(
    // We will pass you an instance of StorIO
    // into the mapFromCursor() in v2.0.0.
    //
    // At the moment, you can save this instance in performGet() and then null it at the end
    @NonNull
    private final ThreadLocal<StorIOSQLite> storIOSQLiteFromPerformGet = new ThreadLocal<StorIOSQLite>();

    public UserWithTweetsGetResolver(@NonNull GetResolver<User> userGetResolver) {
        this.userGetResolver = userGetResolver;
    }

    @NonNull
    @Override
    public UserWithTweets mapFromCursor(@NonNull Cursor cursor) {
        final StorIOSQLite storIOSQLite = storIOSQLiteFromPerformGet.get();

        // BTW, you don't need a transaction here
        // StorIO will wrap mapFromCursor() into the transaction if needed
        try {
            // Or you can manually parse cursor (it will be sliiightly faster)
            final User user = userGetResolver.mapFromCursor(cursor);

            // Yep, you can reuse StorIO here!
            // Or, you can do manual low level requests here
            // BTW, if you profiled your app and found that such queries are not very fast
            // You can always add some optimized version for particular queries to improve the performance
            final List<Tweet> tweetsOfTheUser = storIOSQLite
                    .get()
                    .listOfObjects(Tweet.class)
                    .withQuery(Query.builder()
                            .table(TweetsTable.TABLE)
                            .where(TweetsTable.COLUMN_AUTHOR + "=?")
                            .whereArgs(user.nick())
                            .build())
                    .prepare()
                    .executeAsBlocking();

            return new UserWithTweets(user, tweetsOfTheUser);
        } finally {
            // Releasing StorIOSQLite reference
            storIOSQLiteFromPerformGet.set(null);
        }
    }

    @NonNull
    @Override
    public Cursor performGet(@NonNull StorIOSQLite storIOSQLite, @NonNull RawQuery rawQuery) {
        storIOSQLiteFromPerformGet.set(storIOSQLite);
        return storIOSQLite.internal().rawQuery(rawQuery);
    }

    @NonNull
    @Override
    public Cursor performGet(@NonNull StorIOSQLite storIOSQLite, @NonNull Query query) {
        storIOSQLiteFromPerformGet.set(storIOSQLite);
        return storIOSQLite.internal().query(query);
    }
}

@@ -1545,151 +1545,8 @@
 );%0A%0A
-        // BTW, you don't need a transaction here%0A        // StorIO will wrap mapFromCursor() into the transaction if needed%0A        try %7B%0A    
     
@@ -1616,20 +1616,16 @@
 faster)%0A
-    
         
@@ -1686,28 +1686,24 @@
 );%0A%0A        
-    
 // Yep, you 
@@ -1725,20 +1725,16 @@
 O here!%0A
-    
         
@@ -1790,20 +1790,16 @@
         
-   
- 
 // BTW, 
@@ -1869,20 +1869,16 @@
 ry fast%0A
-    
         
@@ -1976,28 +1976,24 @@
 nce%0A        
-    
 final List%3CT
@@ -2049,23 +2049,15 @@
     
- 
-   
 .get()%0A
-    
     
@@ -2096,20 +2096,16 @@
 .class)%0A
-    
         
@@ -2163,20 +2163,16 @@
         
-    
 .table(T
@@ -2189,20 +2189,16 @@
 .TABLE)%0A
-    
         
@@ -2274,28 +2274,24 @@
             
-  
-  
 .whereArgs(u
@@ -2298,28 +2298,24 @@
 ser.nick())%0A
-    
             
@@ -2352,20 +2352,16 @@
         
-    
 .prepare
@@ -2379,20 +2379,16 @@
         
- 
-   
 .execute
@@ -2398,28 +2398,24 @@
 locking();%0A%0A
-    
         retu
@@ -2464,136 +2464,8 @@
 r);%0A
-        %7D finally %7B%0A            // Releasing StorIOSQLite reference%0A            storIOSQLiteFromPerformGet.set(null);%0A        %7D%0A
     

Remove nulling ThreadLocal since it's already WeakReference