package com.base.locationsharewhite.location

import android.os.Build
import com.base.locationsharewhite.BuildConfig
import com.base.locationsharewhite.helper.AESHelper
import com.base.locationsharewhite.helper.ConfigHelper
import com.base.locationsharewhite.helper.ReportUtils.doPost
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.LogEx
import org.json.JSONObject

/**
 * 登录
 */
object LocationLoginUtils {

    var invitationCodeSp = ""
        get() {
            return AppPreferences.getInstance().getString("invitationCodeSp", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("invitationCodeSp", value, true)
        }
    var nickNameSp = ""
        get() {
            return AppPreferences.getInstance().getString("nickNameSp", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("nickNameSp", value, true)
        }

    private val TAG = "LocationLoginUtils"
    private const val DATA_KEY = "data"

    private val url by lazy {
        val pkg = ConfigHelper.packageName
        val url = StringBuilder(
            "${ConfigHelper.apiUrl}/dingwei/${pkg.filter { it.isLowerCase() }.substring(4, 9)}v"
        )
        url.append("?pkg=$pkg")
        url.toString()
    }

    fun login(name: String? = null) {
        Thread {
            val pkg = ConfigHelper.packageName

            val data = JSONObject()
            name?.let { data.put("nickName", it) }

            val bp = JSONObject()
//            .put("${pkg}_1", "")
                .put("${pkg}_5", Build.VERSION.SDK_INT)
                .put("${pkg}_8", BuildConfig.VERSION_NAME)
                .put("${pkg}_9", AppPreferences.getInstance().getString("uuid", ""))
                .put("${pkg}_10", AppPreferences.getInstance().getString("uuid", ""))//gid
                .put("${pkg}_13", "android")
                .put("${pkg}_14", BuildConfig.VERSION_CODE)
                .put("${pkg}_15", "google")
                .put("${pkg}_24", BuildConfig.BUILD_TYPE)
            LogEx.logDebug(TAG, "uuid=${AppPreferences.getInstance().getString("uuid", "")}")
            val body = JSONObject()
                .put("data", data)
                .put("bp", bp)
                .toString()

            val paramJson = AESHelper.encrypt(body)
            LogEx.logDebug(TAG, "url=$url")

            runCatching {
                val result = doPost(url, HashMap(), paramJson)
                LogEx.logDebug(TAG, "result=$result")
                val responseData = extractData(result)
                LogEx.logDebug(TAG, "responseData=$responseData")
                if (responseData != null) {
                    val decryptedData = AESHelper.decrypt(responseData)
                    LogEx.logDebug(TAG, "decryptedData=$decryptedData")
                    parseLoginData(decryptedData)
                }
            }
        }.start()

    }

    fun extractData(response: String): String? {
        val regex = Regex("\"$DATA_KEY\":\"(.*?)\"")
        val match = regex.find(response)
        return match?.groupValues?.get(1)
    }

    private fun parseLoginData(json: String) {
        val data: JSONObject = JSONObject(json)

        val invitationCode = data.getString("invitationCode")
        invitationCodeSp = invitationCode

        val nickName = data.getString("nickName")
        nickNameSp = nickName

        LogEx.logDebug(TAG, "invitationCode=$invitationCode nickName=$nickName")
    }
}