/*
 * Copyright (C) 2015 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.net;

import com.btmura.android.reddit.app.Filter;
import com.btmura.android.reddit.database.Subreddits;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/** Static methods that create Reddit API URLs. */
public class ApiUrls {

    private static final String BASE_URL = "https://oauth.reddit.com";

    public static CharSequence subreddit(String subreddit, int filter, String more) {
        StringBuilder b = new StringBuilder(BASE_URL);

        if (!Subreddits.isFrontPage(subreddit)) {
            b.append("/r/").append(encode(subreddit));
        }

        if (!Subreddits.isRandom(subreddit)) {
            switch (filter) {
                case Filter.SUBREDDIT_CONTROVERSIAL:
                    b.append("/controversial");
                    break;

                case Filter.SUBREDDIT_HOT:
                    b.append("/hot");
                    break;

                case Filter.SUBREDDIT_NEW:
                    b.append("/new");
                    break;

                case Filter.SUBREDDIT_RISING:
                    b.append("/rising");
                    break;

                case Filter.SUBREDDIT_TOP:
                    b.append("/top");
                    break;
            }
        }

        if (more != null) {
            b.append("?count=25&after=").append(encode(more));
        }

        return b;
    }

    public static String encode(String param) {
        try {
            return URLEncoder.encode(param, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}

@@ -887,18 +887,16 @@
 Urls %7B%0A%0A
-  
   privat
@@ -957,18 +957,16 @@
 .com%22;%0A%0A
-  
   public
@@ -996,16 +996,23 @@
 breddit(
+%0A      
 String s
@@ -1020,16 +1020,22 @@
 breddit,
+%0A     
  int fil
@@ -1038,16 +1038,22 @@
  filter,
+%0A     
  String 
@@ -1064,20 +1064,16 @@
 ) %7B%0A    
- 
-   
 StringBu
@@ -1112,20 +1112,16 @@
 _URL);%0A%0A
-    
     if (
@@ -1156,38 +1156,32 @@
 eddit)) %7B%0A      
- 
-     
 b.append(%22/r/%22).
@@ -1207,35 +1207,27 @@
 ddit));%0A    
-   
- 
 %7D%0A%0A
-    
     if (!Sub
@@ -1263,22 +1263,16 @@
 %7B%0A      
-   
-   
 switch (
@@ -1277,32 +1277,24 @@
  (filter) %7B%0A
-        
         case
@@ -1332,34 +1332,24 @@
 :%0A          
-    
-      
 b.append(%22/c
@@ -1360,34 +1360,24 @@
 oversial%22);%0A
-          
           br
@@ -1378,32 +1378,24 @@
     break;%0A%0A
-        
         case
@@ -1423,34 +1423,24 @@
 :%0A          
-       
-   
 b.append(%22/h
@@ -1441,34 +1441,24 @@
 nd(%22/hot%22);%0A
-          
           br
@@ -1467,32 +1467,24 @@
 k;%0A%0A        
-   
-     
 case Filter.
@@ -1498,26 +1498,16 @@
 IT_NEW:%0A
-          
         
@@ -1522,34 +1522,24 @@
 nd(%22/new%22);%0A
-          
           br
@@ -1548,32 +1548,24 @@
 k;%0A%0A        
-      
-  
 case Filter.
@@ -1582,26 +1582,16 @@
 RISING:%0A
-          
         
@@ -1623,34 +1623,24 @@
         
-   
-       
 break;%0A%0A
         
@@ -1631,24 +1631,16 @@
 break;%0A%0A
-        
         
@@ -1676,26 +1676,16 @@
         
- 
-         
 b.append
@@ -1704,26 +1704,16 @@
         
-      
-    
 break;%0A 
@@ -1721,35 +1721,21 @@
     
-    
-  %7D%0A  
+%7D%0A
     
-  
 %7D%0A%0A    
-  
-  
 if (
@@ -1746,30 +1746,24 @@
  != null) %7B%0A
-      
       b.appe
@@ -1815,23 +1815,15 @@
     
-    %7D%0A%0A   
+%7D%0A%0A
     
- 
 retu
@@ -1828,25 +1828,21 @@
 turn b;%0A
-  
   %7D%0A%0A
-  
   public
@@ -1887,24 +1887,14 @@
     
-   
- 
 try %7B%0A
-      
     
@@ -1941,20 +1941,16 @@
 %22);%0A    
- 
-   
 %7D catch 
@@ -1990,22 +1990,16 @@
 %7B%0A      
-     
- 
 throw ne
@@ -2025,22 +2025,16 @@
 e);%0A    
-    %7D%0A  
+%7D%0A
   %7D%0A%7D%0A

Reformat ApiUrls class