package com.adealink.weparty.stat

import android.content.Context
import com.adealink.frame.locale.country.getCountryCode
import com.adealink.frame.locale.language.languageManager
import com.adealink.frame.statistics.IStatConfig
import com.adealink.frame.statistics.ReportType
import com.adealink.frame.statistics.StatClientError
import com.adealink.frame.util.safeToLong
import com.adealink.weparty.App
import com.adealink.weparty.channel.getChannel
import com.adealink.weparty.module.profile.ProfileModule
import okhttp3.OkHttpClient

class StatConfig : IStatConfig {

//    private val statHttpService by lazy {
//        App.instance.networkService.getReportHttpService(ServerStatHttpService::class.java)
//    }

    override val ctx: Context
        get() = App.instance
    override val app: String
        get() = "0"
    override val uid: Long
        // TODO:
        get() = ProfileModule.getMyUid().safeToLong()
    override val deviceId: String
        get() = App.instance.deviceIdService.getLocalDeviceId()
    override val gender: Int?
        get() = ProfileModule.getMyUserInfo()?.gender
    override val region: String
        get() = ""
//        get() = ProfileModule.getMyUserInfo()?.region ?: ""
    override val countryCode: String
        get() = getCountryCode()
    override val languageCode: String
        get() = languageManager?.getLanguageCode() ?: ""
    override val appChannel: String
        get() = getChannel()
    override val organicInstall: Boolean
        get() = false
//        get() = !AttributionModule.isInorganicInstall()
    override val roomId: Long?
        get() = null
//        get() = RoomModule.getJoinedRoomId()
    override val roomType: Int?
        get() = null
//        get() = RoomModule.getRoomType()?.type
    override val defaultReportChannels = emptyList<ReportType>()
        //listOf(ReportType.WENEXT, ReportType.FIREBASE)
    override val reportUrl: String = "https://api-log-upload.wenext.technology/api/log"
    override val statHtpClient: OkHttpClient
        get() = App.instance.networkService.statHttpClient

    override fun reportClientError(statClientError: StatClientError?) {
        if (statClientError == null) {
            return
        }
        // TODO: 后期补齐
//        CoroutineScope(Dispatcher.WENEXT_THREAD_POOL).launch {
//            val newDeviceId = App.instance.deviceIdService.getNewDeviceIdErrorEmpty()
//            statHttpService.reportClientError(
//                ClientFatalErrorReportReq(
//                    deviceId = newDeviceId,
//                    module = statClientError.module,
//                    errorInfo = statClientError.errorInfo,
//                    sid = ProfileModule.getMyUid(),
//                    region = ProfileModule.getMyUserInfo()?.region,
//                    country = ProfileModule.getMyUserInfo()?.country,
//                )
//            )
//        }
    }

}