package com.qiniu.android.dns.http;

import com.qiniu.android.dns.Domain;
import com.qiniu.android.dns.IResolver;
import com.qiniu.android.dns.NetworkInfo;
import com.qiniu.android.dns.Record;
import com.qiniu.android.dns.util.DES;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class QiniuDns implements IResolver{
    private static final String ENDPOINT = "https://httpdns.qnydns.net:18443/";

    private static String mAccountId;
    private static String mEncryptKey;
    private static long mExpireTimeMs = 0;

    public QiniuDns(String accountId, String encryptKey, long expireTimeMs) {
        mAccountId = accountId;
        mEncryptKey = encryptKey;
        mExpireTimeMs = expireTimeMs;
    }

    @Override
    public Record[] resolve(Domain domain, NetworkInfo info) throws IOException {
        HttpsURLConnection connection = (HttpsURLConnection) new URL(ENDPOINT + mAccountId
                + "/d?dn=" + (mEncryptKey == null ? domain.domain
                    : DES.encrypt( domain.domain + "?e=" + Long.toString((System.currentTimeMillis() + mExpireTimeMs) / 1000), mEncryptKey))
                + "&ttl=1").openConnection();
        connection.setConnectTimeout(3000);
        connection.setReadTimeout(10000);
        if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        try {
            JSONObject response = new JSONArray(mEncryptKey == null ? sb.toString() : DES.decrypt(sb.toString(), mEncryptKey)).optJSONObject(0);
            JSONArray result = response.optJSONArray("A");
            if (result.length() <= 0) {
                return null;
            }
            int ttl = response.optInt("ttl");
            Record[] records = new Record[result.length()];
            for (int i = 0; i < records.length;++i) {
                records[i] = new Record(result.getString(i), Record.TYPE_A, ttl, System.currentTimeMillis() / 1000);
            }
            return records;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@@ -1206,16 +1206,40 @@
 n.domain
+%0A                       
  + %22?e=%22
@@ -1282,16 +1282,40 @@
 Millis()
+%0A                       
  + mExpi
@@ -1373,16 +1373,28 @@
 %22&ttl=1%22
+ + %22&echo=1%22
 ).openCo
@@ -1891,23 +1891,20 @@
 JSON
-Object response
+Array result
  = n
@@ -1910,21 +1910,22 @@
 new JSON
-Array
+Object
 (mEncryp
@@ -1952,16 +1952,36 @@
 String()
+%0A                   
  : DES.d
@@ -2019,66 +2019,50 @@
 ey))
-.optJSONObject(0);%0A            JSONArray result = response
+%0A                    .optJSONArray(%22data%22)
 .opt
@@ -2075,11 +2075,9 @@
 ray(
-%22A%22
+0
 );%0A 
@@ -2162,54 +2162,8 @@
   %7D%0A
-            int ttl = response.optInt(%22ttl%22);%0A
     
@@ -2268,24 +2268,83 @@
 ngth;++i) %7B%0A
+                JSONObject item = result.optJSONObject(i);%0A
             
@@ -2375,25 +2375,23 @@
 ord(
-result.ge
+item.op
 tString(
 i), 
@@ -2386,17 +2386,22 @@
 tString(
-i
+%22data%22
 ), Recor
@@ -2413,12 +2413,51 @@
 E_A,
- ttl
+%0A                        item.optInt(%22TTL%22)
 , Sy

Modify code due to server response