/*
 * 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.provider;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.util.JsonReader;
import android.util.Log;

import com.btmura.android.reddit.BuildConfig;
import com.btmura.android.reddit.database.SubredditSearches;
import com.btmura.android.reddit.net.RedditApi;
import com.btmura.android.reddit.net.Urls;
import com.btmura.android.reddit.util.JsonParser;

class SubredditSearchListing extends JsonParser {

    public static final String TAG = "SubredditSearchListing";

    public final ArrayList<ContentValues> values = new ArrayList<ContentValues>(25);

    private final String accountName;
    private final String sessionId;
    private final long sessionTimestamp;

    public static SubredditSearchListing get(Context context, String accountName, String sessionId,
            long sessionTimestamp, String query, String cookie) throws IOException {
        URL url = Urls.subredditSearchUrl(query, null);
        HttpURLConnection conn = RedditApi.connect(url, cookie, false);
        InputStream input = new BufferedInputStream(conn.getInputStream());
        try {
            JsonReader reader = new JsonReader(new InputStreamReader(input));
            SubredditSearchListing listing = new SubredditSearchListing(accountName, sessionId,
                    sessionTimestamp);
            listing.parseListingObject(reader);
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "url: " + url + " values: " + listing.values.size());
            }
            return listing;
        } finally {
            input.close();
            conn.disconnect();
        }
    }

    private SubredditSearchListing(String accountName, String sessionId, long sessionTimestamp) {
        this.accountName = accountName;
        this.sessionId = sessionId;
        this.sessionTimestamp = sessionTimestamp;
    }

    @Override
    public void onEntityStart(int index) {
        ContentValues v = new ContentValues(6);
        v.put(SubredditSearches.COLUMN_ACCOUNT, accountName);
        v.put(SubredditSearches.COLUMN_SESSION_ID, sessionId);
        v.put(SubredditSearches.COLUMN_SESSION_TIMESTAMP, sessionTimestamp);
        values.add(v);
    }

    @Override
    public void onDisplayName(JsonReader reader, int index) throws IOException {
        values.get(index).put(SubredditSearches.COLUMN_NAME, readTrimmedString(reader, ""));
    }

    @Override
    public void onOver18(JsonReader reader, int index) throws IOException {
        values.get(index).put(SubredditSearches.COLUMN_OVER_18, reader.nextBoolean());
    }

    @Override
    public void onSubscribers(JsonReader reader, int index) throws IOException {
        values.get(index).put(SubredditSearches.COLUMN_SUBSCRIBERS, reader.nextInt());
    }

    @Override
    public void onEntityEnd(int index) {
        super.onEntityEnd(index);
        Log.d(TAG, "v: " + values.get(index));
    }
}

@@ -960,79 +960,8 @@
 er;%0A
-import android.util.Log;%0A%0Aimport com.btmura.android.reddit.BuildConfig;
 %0Aimp
@@ -2145,140 +2145,8 @@
 r);%0A
-            if (BuildConfig.DEBUG) %7B%0A                Log.d(TAG, %22url: %22 + url + %22 values: %22 + listing.values.size());%0A            %7D%0A
     
@@ -3402,150 +3402,7 @@
    %7D
-%0A%0A    @Override%0A    public void onEntityEnd(int index) %7B%0A        super.onEntityEnd(index);%0A        Log.d(TAG, %22v: %22 + values.get(index));%0A    %7D
 %0A%7D%0A

Remove unnecessary logging messages