/*
 * Copyright 2014 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
 *
 * 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 org.traccar.geocode;

import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.traccar.helper.Log;
import org.w3c.dom.Document;

public class NominatimReverseGeocoder extends JsonReverseGeocoder {

    public NominatimReverseGeocoder() {
        this("http://nominatim.openstreetmap.org/reverse");
    }
    
    public NominatimReverseGeocoder(String url) {
        super(url + "?format=json&lat=%f&lon=%f&zoom=18&addressdetails=1");
    }

    @Override
    protected Address parseAddress(JsonObject json) {
        Address address = new Address();
        JsonObject result = json.getJsonObject("address");

        if (result.containsKey("house_number")) {
            address.setHouse(result.getString("house_number"));
        }
        if (result.containsKey("road")) {
            address.setStreet(result.getString("road"));
        }
        if (result.containsKey("village")) {
            address.setSettlement(result.getString("village"));
        }
        if (result.containsKey("city")) {
            address.setSettlement(result.getString("city"));
        }
        if (result.containsKey("state_district")) {
            address.setDistrict(result.getString("state_district"));
        }
        if (result.containsKey("state")) {
            address.setState(result.getString("state"));
        }
        if (result.containsKey("country_code")) {
            address.setCountry(result.getString("country_code").toUpperCase());
        }
        if (result.containsKey("postcode")) {
            address.setPostcode(result.getString("postcode"));
        }

        return address;
    }

}

@@ -1370,49 +1370,68 @@
     
-Address address = new A
+JsonObject result = json.getJsonObject(%22a
 ddress
-(
+%22
 );%0A
+%0A
         
 Json
@@ -1430,60 +1430,80 @@
     
-JsonObject 
+if (
 result 
+!
 = 
-json.getJsonObject(%22a
+null) %7B%0A            Address address = new A
 ddress
-%22
+(
 );%0A%0A
+    
     
@@ -1544,24 +1544,28 @@
 number%22)) %7B%0A
+    
             
@@ -1616,34 +1616,42 @@
 ber%22));%0A        
-%7D%0A
+    %7D%0A    
         if (resu
@@ -1680,32 +1680,36 @@
 ) %7B%0A            
+    
 address.setStree
@@ -1733,24 +1733,28 @@
 g(%22road%22));%0A
+    
         %7D%0A  
@@ -1751,32 +1751,36 @@
       %7D%0A        
+    
 if (result.conta
@@ -1796,24 +1796,28 @@
 illage%22)) %7B%0A
+    
             
@@ -1872,34 +1872,41 @@
 ));%0A        
-%7D%0A
+    %7D%0A   
         
 if (result.c
@@ -1885,32 +1885,33 @@
    %7D%0A           
+ 
 if (result.conta
@@ -1924,24 +1924,28 @@
 (%22city%22)) %7B%0A
+    
             
@@ -1993,34 +1993,42 @@
 ity%22));%0A        
+   
+ 
 %7D%0A
+    
         if (resu
@@ -2067,32 +2067,36 @@
 ) %7B%0A            
+   
+ 
 address.setDistr
@@ -2140,34 +2140,41 @@
 ));%0A        
-%7D%0A
+    %7D%0A   
         
 if (result.c
@@ -2153,32 +2153,33 @@
    %7D%0A           
+ 
 if (result.conta
@@ -2193,24 +2193,28 @@
 %22state%22)) %7B%0A
+    
             
@@ -2258,34 +2258,42 @@
 ate%22));%0A        
+  
-%7D%0A
+  %7D%0A    
         if (resu
@@ -2318,32 +2318,36 @@
 untry_code%22)) %7B%0A
+    
             addr
@@ -2418,26 +2418,32 @@
         
-%7D%0A
+    %7D%0A  
         
 if (resu
@@ -2426,32 +2426,34 @@
     %7D%0A          
+  
 if (result.conta
@@ -2466,32 +2466,36 @@
 (%22postcode%22)) %7B%0A
+    
             addr
@@ -2545,27 +2545,35 @@
 ));%0A        
+  
+  
 %7D%0A%0A
+    
         retu
@@ -2584,16 +2584,48 @@
 ddress;%0A
+        %7D%0A%0A        return null;%0A
     %7D%0A%0A%7D

Handle no result from Nominatim