/*
 * Copyright (C) 2013 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.content;

import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.net.Uri;
import android.util.Log;

import com.btmura.android.reddit.BuildConfig;
import com.btmura.android.reddit.accounts.AccountUtils;
import com.btmura.android.reddit.app.Filter;
import com.btmura.android.reddit.database.Subreddits;
import com.btmura.android.reddit.net.RedditApi;
import com.btmura.android.reddit.net.UriHelper;
import com.btmura.android.reddit.net.Urls;

import java.io.IOException;
import java.net.HttpURLConnection;

public class RandomSubredditLoader extends BaseAsyncTaskLoader<String> {

    private static final String TAG = "RandomSubredditLoader";

    private final String accountName;

    public RandomSubredditLoader(Context context, String accountName) {
        super(context);
        this.accountName = accountName;
    }

    @Override
    public String loadInBackground() {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "loadInBackground");
        }

        try {
            CharSequence url = Urls.subreddit(Subreddits.NAME_RANDOM, Filter.SUBREDDIT_HOT, Urls.TYPE_HTML);
            String cookie = AccountUtils.getCookie(getContext(), accountName);
            HttpURLConnection conn = RedditApi.connect(url, cookie, false);
            if (conn.getResponseCode() == 302) {
                return UriHelper.getSubreddit(Uri.parse(conn.getHeaderField("Location")));
            }
        } catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
        } catch (OperationCanceledException e) {
            Log.e(TAG, e.getMessage(), e);
        } catch (AuthenticatorException e) {
            Log.e(TAG, e.getMessage(), e);
        }
        return null;
    }
}

@@ -1303,18 +1303,16 @@
 ing%3E %7B%0A%0A
-  
   privat
@@ -1365,18 +1365,16 @@
 ader%22;%0A%0A
-  
   privat
@@ -1404,18 +1404,16 @@
 ame;%0A%0A  
- 
- 
 public R
@@ -1476,20 +1476,16 @@
 ) %7B%0A    
-   
- 
 super(co
@@ -1492,20 +1492,16 @@
 ntext);%0A
-    
     this
@@ -1528,25 +1528,21 @@
 ntName;%0A
-  
   %7D%0A%0A
-  
   @Overr
@@ -1545,18 +1545,16 @@
 verride%0A
-  
   public
@@ -1578,28 +1578,24 @@
 kground() %7B%0A
-    
     if (Buil
@@ -1613,30 +1613,24 @@
 UG) %7B%0A      
-   
-   
 Log.d(TAG, %22
@@ -1649,20 +1649,16 @@
 ound%22);%0A
-    
     %7D%0A%0A 
@@ -1664,24 +1664,14 @@
     
- 
-   
 try %7B%0A
-      
     
@@ -1729,16 +1729,26 @@
 _RANDOM,
+%0A         
  Filter.
@@ -1779,22 +1779,16 @@
 _HTML);%0A
-      
       St
@@ -1858,22 +1858,16 @@
 ;%0A      
-  
-    
 HttpURLC
@@ -1928,22 +1928,16 @@
 ;%0A      
-    
-  
 if (conn
@@ -1961,32 +1961,24 @@
 ) == 302) %7B%0A
-        
         retu
@@ -2003,16 +2003,29 @@
 breddit(
+%0A            
 Uri.pars
@@ -2067,28 +2067,18 @@
 ;%0A      
-      %7D%0A    
+%7D%0A
     %7D ca
@@ -2097,38 +2097,32 @@
 tion e) %7B%0A      
-  
-    
 Log.e(TAG, e.get
@@ -2128,36 +2128,32 @@
 tMessage(), e);%0A
-    
     %7D catch (Ope
@@ -2179,38 +2179,32 @@
 tion e) %7B%0A      
- 
-     
 Log.e(TAG, e.get
@@ -2222,20 +2222,16 @@
 e);%0A    
-   
- 
 %7D catch 
@@ -2259,22 +2259,16 @@
 on e) %7B%0A
-      
       Lo
@@ -2300,26 +2300,18 @@
 e);%0A    
-  
-  %7D%0A    
+%7D%0A
     retu
@@ -2319,16 +2319,14 @@
 n null;%0A
-  
   %7D%0A%7D%0A

Reformat RandomSubredditLoader