package com.base.browserwhite.help

import android.util.Log
import android.widget.Toast
import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.LogEx
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL

object SearchTipUtils {

    private val TAG = "SearchTipUtils"

    fun getSearchTip(q: String): String? {
        val url = "https://www.google.com/complete/search?q=$q&cp=3&client=gws-wiz&xssi=t&hl=en"

        try {
            val conn: HttpURLConnection = URL(url).openConnection() as HttpURLConnection
            conn.setRequestMethod("GET")
            conn.connectTimeout = 5000
            if (200 == conn.getResponseCode()) {
                val json = BufferedReader(InputStreamReader(conn.inputStream)).readLine()
                LogEx.logDebug(TAG, json)
                AppPreferences.getInstance().put(TAG,json)
                return json
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return null
    }
}