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

import java.util.List;

import com.btmura.android.reddit.widget.ThingBundle;

import android.content.UriMatcher;
import android.net.Uri;
import android.os.Bundle;

class UriHelper {

    // TODO: Add missing support for /r/pics/top or /u/btmura/overview.

    // URI constants and matcher for handling intents with data.
    private static final String AUTHORITY = "www.reddit.com";
    private static final String AUTHORITY2 = "reddit.com";
    private static final UriMatcher MATCHER = new UriMatcher(0);
    private static final int MATCH_SUBREDDIT = 1;
    private static final int MATCH_COMMENTS = 2;
    private static final int MATCH_USER = 3;
    static {
        // http://www.reddit.com/r/rbb
        MATCHER.addURI(AUTHORITY, "r/*", MATCH_SUBREDDIT);
        MATCHER.addURI(AUTHORITY2, "r/*", MATCH_SUBREDDIT);

        // http://www.reddit.com/r/rbb/comments/12zl0q/
        MATCHER.addURI(AUTHORITY, "r/*/comments/*", MATCH_COMMENTS);
        MATCHER.addURI(AUTHORITY2, "r/*/comments/*", MATCH_COMMENTS);

        // http://www.reddit.com/r/rbb/comments/12zl0q/test_1
        MATCHER.addURI(AUTHORITY, "r/*/comments/*/*", MATCH_COMMENTS);
        MATCHER.addURI(AUTHORITY2, "r/*/comments/*/*", MATCH_COMMENTS);

        // http://www.reddit.com/u/btmura
        MATCHER.addURI(AUTHORITY, "u/*", MATCH_USER);
        MATCHER.addURI(AUTHORITY2, "u/*", MATCH_USER);
    }

    public static String getSubreddit(Uri data) {
        switch (MATCHER.match(data)) {
            case MATCH_SUBREDDIT:
                return data.getLastPathSegment();

            case MATCH_COMMENTS:
                return data.getPathSegments().get(1);

            default:
                return null;
        }
    }

    public static Bundle getThingBundle(Uri data) {
        switch (MATCHER.match(data)) {
            case MATCH_COMMENTS:
                List<String> segments = data.getPathSegments();
                Bundle b = new Bundle(2);
                ThingBundle.putSubreddit(b, segments.get(1));
                ThingBundle.putThingId(b, segments.get(3));
                return b;

            default:
                return null;
        }
    }

    public static String getUser(Uri data) {
        switch (MATCHER.match(data)) {
            case MATCH_USER:
                return data.getLastPathSegment();

            default:
                return null;
        }
    }
}

@@ -898,73 +898,8 @@
 w.%0A%0A
-    // URI constants and matcher for handling intents with data.%0A
     
@@ -917,32 +917,34 @@
 tic final String
+%5B%5D
  AUTHORITY = %22ww
@@ -940,11 +940,27 @@
 ORIT
-Y =
+IES = %7B%0A           
  %22ww
@@ -976,54 +976,21 @@
 com%22
-;
+,
 %0A    
-private static final String AUTHORITY2 =
+       
  %22re
@@ -998,17 +998,25 @@
 dit.com%22
-;
+,%0A    %7D;%0A
 %0A    pri
@@ -1242,98 +1242,102 @@
     
-// http://www.reddit.com/r/rbb%0A        MATCHER.addURI(AUTHORITY, %22r/*%22, MATCH_SUBREDDIT);%0A
+for (int i = 0; i %3C AUTHORITIES.length; i++) %7B%0A            // http://www.reddit.com/r/rbb%0A    
     
@@ -1359,26 +1359,30 @@
 URI(AUTHORIT
-Y2
+IES%5Bi%5D
 , %22r/*%22, MAT
@@ -1393,24 +1393,28 @@
 UBREDDIT);%0A%0A
+    
         // h
@@ -1465,73 +1465,8 @@
     
-    MATCHER.addURI(AUTHORITY, %22r/*/comments/*%22, MATCH_COMMENTS);%0A
     
@@ -1484,34 +1484,38 @@
 .addURI(AUTHORIT
-Y2
+IES%5Bi%5D
 , %22r/*/comments/
@@ -1528,32 +1528,36 @@
 TCH_COMMENTS);%0A%0A
+    
         // http:
@@ -1610,75 +1610,8 @@
     
-    MATCHER.addURI(AUTHORITY, %22r/*/comments/*/*%22, MATCH_COMMENTS);%0A
     
@@ -1637,18 +1637,22 @@
 AUTHORIT
-Y2
+IES%5Bi%5D
 , %22r/*/c
@@ -1683,32 +1683,36 @@
 ENTS);%0A%0A        
+    
 // http://www.re
@@ -1729,32 +1729,36 @@
 /btmura%0A        
+ 
+   
 MATCHER.addURI(A
@@ -1764,17 +1764,22 @@
 AUTHORIT
-Y
+IES%5Bi%5D
 , %22u/*%22,
@@ -1804,54 +1804,9 @@
     
-MATCHER.addURI(AUTHORITY2, %22u/*%22, MATCH_USER);
+%7D
 %0A   

Use a for loop for authority possibilites