import java.io.InputStream ;
import java.io.InputStreamReader ;
import java.io.IOException ;
import java.lang.System ;
import java.net.URL ;
import java.net.HttpURLConnection ;
import org.json.simple.JSONArray ;
import org.json.simple.JSONObject ;
import org.json.simple.JSONValue ;

/**
 * A tutorial example of using the json-simple library to parse JSON resources
 * on the Doxpop Court API.  For more information on json-simple see
 * http://code.google.com/p/json-simple. This code was tested with json-simple
 * 1.1.
 */
public class JSONClient {

  private static String contextUrl = "https://demo-api.doxpop.com" ;
  // userPass is base64-encoded user:password
  private static String userPass = "ZGVtbzpkZW1v" ; 

  public static InputStream getPage( URL url ) throws IOException
  {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection() ;
    conn.setRequestProperty("Authorization", "Basic " + userPass) ;
    conn.connect() ;
    return (InputStream) conn.getContent() ;
  }

  public static void main( String[] args ) throws Exception
  {
    URL context = new URL( contextUrl ) ;
    URL queryUrl = new URL( context, "/actors_cases.json?fullname=rifkind,+david" ) ;

    InputStream ioStream = getPage( queryUrl ) ;
    InputStreamReader reader = new InputStreamReader( ioStream ) ;
    Object obj = JSONValue.parse( reader ) ;
    JSONArray array = (JSONArray) obj ;
    for ( int i=0; i < array.size(); i++ ) 
    {
      JSONObject resource = (JSONObject) array.get(i) ;
      System.out.println( resource.get( "assigned_case_role" ) ) ;

      // Some actor information
      JSONObject actor = (JSONObject) resource.get( "actor" ) ;
      System.out.println( actor.get( "actor_full_name" ) ) ;
      System.out.println( actor.get( "actor_person_date_of_birth" ) ) ;

      // Some address information
      JSONArray addresses = (JSONArray) actor.get( "addresses" ) ;
      for ( int j=0; j < addresses.size(); j++ )
      {
        JSONObject address = (JSONObject) addresses.get(j) ;
        System.out.println( address.get( "address_line1" ) ) ;
        System.out.println( address.get( "address_city" ) ) ;
        System.out.println( address.get( "address_state_province_code" ) ) ;
        System.out.println( address.get( "address_postal_code" ) ) ;
      }

      // Some case information
      JSONObject caseObj = (JSONObject) resource.get( "case" ) ;
      System.out.println( caseObj.get( "case_caption" ) ) ;
      System.out.println( caseObj.get( "case_global_disposition_code" ) ) ;
      System.out.println( caseObj.get( "case_disposition_date" ) ) ;

      // Extract and use the charges URI.
      String chargesUri = (String) caseObj.get( "charges_uri" ) ;
      System.out.println( chargesUri ) ;
      URL chargesUrl = new URL( context, chargesUri + ".json" ) ;
      InputStream chargesStream = getPage( chargesUrl ) ;
      InputStreamReader chargesReader = new InputStreamReader( chargesStream ) ;
      JSONArray chargesArray = (JSONArray) JSONValue.parse( chargesReader ) ;
      for ( int k=0; k < chargesArray.size(); k++ )
      {
        JSONObject chargeObj = (JSONObject) chargesArray.get(k) ;
        System.out.println( chargeObj.get( "charge_desc" ) ) ;
      }
    }
  }
}
