package com.example.vastlib.entity.request

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import android.telephony.TelephonyManager
import com.example.vastlib.BuildConfig
import com.example.vastlib.utils.AndroidIdManager
import com.example.vastlib.utils.LogUtils
import com.example.vastlib.utils.NetworkManager
import com.example.vastlib.utils.notificationListenerEnable
import kotlinx.coroutines.*

class BaseRequestBuilder(private val context: Context) {
    private val networkOperator: String
    private val simOperator: String
    private var versionCode: Int = 0
    private val affId: Int
    private val sdkVer: Int
    private val subId: String
    private val packageName: String
    private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
    private val userId: String
        get() {
            return AndroidIdManager.id ?: Settings.Secure.getString(AndroidIdManager.context.contentResolver, Settings.Secure.ANDROID_ID)
        }

    private val appVer: Int
    val telcoCode: String
        get() {
            return if (networkOperator.isNotBlank()) simOperator
            else networkOperator
        }

    private val netType: Int
        get() {
            return NetworkManager.sdkNetworkType(context)
        }

    private val recvFlag: Boolean
    get() = context.notificationListenerEnable()

    private val countryCode: String
        get() {
            return context.resources.configuration.locale.country
        }

    init {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
        networkOperator = tm?.networkOperator ?: ""
        simOperator = tm?.simOperator ?: ""

        try {
            versionCode = context.packageManager.getPackageInfo(
                context.packageName, PackageManager.GET_META_DATA
            ).runCatching {
                this.versionCode
            }.getOrDefault(0)

        } catch (e: Exception) {
            LogUtils.error(throwable = e)
        }
        affId = BuildConfig.aff_id
        sdkVer = BuildConfig.sdk_version
        subId = context.packageName
        packageName = context.packageName
        appVer = versionCode
    }

    val taskRequest: BaseRequest = GetTaskRequest(
        userId = userId,
        pkg = packageName,
        affId = affId,
        subId = subId,
        androidVer = Build.VERSION.SDK_INT,
        sdkVer = sdkVer,
        appVer = appVer,
        countryCode = countryCode,
        telcoCode = telcoCode,
        netType = netType,
        recvFlag = recvFlag
    )

    val genReportRequest: ReportTaskRequest = taskRequest.run {
        ReportTaskRequest(
            userId = userId,
            pkg = pkg,
            affId = affId,
            subId = subId,
            androidVer = androidVer,
            sdkVer = sdkVer,
            appVer = appVer,
            countryCode = countryCode,
            telcoCode = telcoCode,
            netType = netType,
            recvFlag = recvFlag
        )
    }
}