/**
 * Copyright 2013 Andrew Yates <andrew@ir.cs.georgetown.edu>
 *
 * Based on hbc-example, which is  
 * Copyright 2013 Twitter, Inc.
 * 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 edu.georgetown.cs.ir.hbc;

import com.google.common.collect.Lists;
import com.twitter.hbc.ClientBuilder;
import com.twitter.hbc.core.Client;
import com.twitter.hbc.core.Constants;
import com.twitter.hbc.core.endpoint.Location;
import com.twitter.hbc.core.endpoint.StatusesFilterEndpoint;
import com.twitter.hbc.core.processor.StringDelimitedProcessor;
import com.twitter.hbc.httpclient.auth.Authentication;
import com.twitter.hbc.httpclient.auth.OAuth1;

import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class NorthAmericaFilter {

    public void oauth(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
        BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
        StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();

        // add bounding box for North America (erring on the side of including extra regions)
        // SW 14.12, -172.07
        // NE 71.92, -49.73
        ArrayList<Location> locations = new ArrayList<Location>();
        locations.add(new Location(new Location.Coordinate(14.12, 172.07),
                                   new Location.Coordinate(71.92, -49.73)));
        endpoint.locations(locations);

        Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);

        // Create a new BasicClient. By default gzip is enabled.
        Client client = new ClientBuilder()
            .hosts(Constants.STREAM_HOST)
            .endpoint(endpoint)
            .authentication(auth)
            .processor(new StringDelimitedProcessor(queue))
            .build();

        // Establish a connection
        client.connect();

        // Do whatever needs to be done with messages
        for (int msgRead = 0; msgRead < 1000; msgRead++) {
            String msg = queue.take();
            System.out.println(msg);
        }

        client.stop();

    }

    public static void main(String[] args) {
        if (args.length != 4) {
            System.err.println("usage: <consumer key> <consumer secret> <token> <secret");
            System.exit(1);
        }

        NorthAmericaFilter naf = new NorthAmericaFilter();

        try {
            naf.oauth(args[0], args[1], args[2], args[3]);
        } catch (InterruptedException e) {
            System.err.println(e);
            System.exit(2);
        }
    }
}

@@ -1610,20 +1610,18 @@
 for 
-North Americ
+US + Canad
 a (e
@@ -1670,65 +1670,8 @@
 ns)%0A
-        // SW 14.12, -172.07%0A        // NE 71.92, -49.73%0A
     
@@ -1772,57 +1772,84 @@
 ion(
-new Location.Coordinate(14.12, 172.07),%0A         
+%0A                          new Location.Coordinate(-166.992188, 24.846565),%0A
     
@@ -1898,21 +1898,29 @@
 ate(
-71.92, -49.73
+-53.789063, 70.020587
 )));

fix coord ordering