package com.assimilate.alltrans.common

import android.os.Bundle
import com.assimilate.alltrans.BuildConfig
import com.assimilate.alltrans.MyApp
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase


object FirebaseAnalyticsHelper {
    private val firebaseAnalytics: FirebaseAnalytics by lazy {
        Firebase.analytics
    }

    private fun logCusEvent(eventName: String, params: Map<String, Any?>? = null) {
//        if (BuildConfig.DEBUG) {
//            // 在调试模式下不上传事件
//            return
//        }
        val bundle = Bundle().apply {
            params?.forEach { (key, value) ->
                when (value) {
                    is String -> putString(key, value)
                    is Long -> putLong(key, value)
                    is Double -> putDouble(key, value)
                    is Int -> putInt(key, value)
                    else -> throw IllegalArgumentException("Unsupported bundle parameter type")
                }
            }
        }
        firebaseAnalytics.logEvent(eventName, bundle)
    }


    fun launchPvEvent() {
        logCusEvent(MyApp.Config.launchPv, null)
    }


    fun homePvEvent(from: String, value: String) {
        val params = mapOf(from to value)
        logCusEvent(MyApp.Config.homePv, params)
    }

    fun textTransSwitchEvent(from: String, value: String) {
        val params = mapOf(from to value)
        logCusEvent(MyApp.Config.textTransSwitch, params)
    }

    fun languageChooseEvent(from: String, value: String) {
        val params = mapOf(from to value)
        logCusEvent(MyApp.Config.languageChoose, params)
    }

    fun languageFromEvent(clickFrom: String, value: String) {
        val params = mapOf(clickFrom to value)
        logCusEvent(MyApp.Config.languageFrom, params)
    }

    fun textTransClickEvent() {
        logCusEvent(MyApp.Config.textTransClick, null)
    }

    fun textTransResultEvent(result: String, value: String) {
        val params = mapOf(result to value)
        logCusEvent(MyApp.Config.textTransResult, params)
    }

    fun textInputEvent() {
        logCusEvent(MyApp.Config.textInput, null)
    }

    fun textTransPasteEvent() {
        logCusEvent(MyApp.Config.textTransPaste, null)
    }

    fun textVoiceClickEvent() {

        logCusEvent(MyApp.Config.textVoiceClick, null)
    }


    fun textVoiceResultEvent(failReason: String, value: String) {
        val params = mapOf(failReason to value)
        logCusEvent(MyApp.Config.textVoiceResult, params)
    }


    fun textTransPlayEvent() {

        logCusEvent(MyApp.Config.textTransPlay, null)
    }

    fun textTransCopyEvent() {
        logCusEvent(MyApp.Config.textTransCopy, null)
    }

    fun textTransShareEvent() {
        logCusEvent(MyApp.Config.textTransShare, null)
    }

    fun textTransLikeEvent() {
        logCusEvent(MyApp.Config.textTransLike, null)
    }


    fun hoverButtonClickEvent() {
        logCusEvent(MyApp.Config.hoverButtonClick, null)
    }

    fun hoverLimitAgreeEvent() {
        logCusEvent(MyApp.Config.hoverLimitAgree, null)
    }

    fun hoverButtonCancelEvent() {
        logCusEvent(MyApp.Config.hoverButtonCancel, null)
    }

    fun hoverButtonAgreeEvent() {
        logCusEvent(MyApp.Config.hoverButtonAgree, null)
    }

    fun hoverScreenClickEvent() {
        logCusEvent(MyApp.Config.hoverScreenClick, null)
    }

    fun hoverScreenImpEvent() {
        logCusEvent(MyApp.Config.hoverScreenImp, null)
    }

    fun hoverScreenGlobalEvent() {
        logCusEvent(MyApp.Config.hoverScreenGlobal, null)
    }

    fun hoverScreenDistrictEvent() {
        logCusEvent(MyApp.Config.hoverScreenDistrict, null)
    }

    fun imageClickEvent(clickFrom: String, value: String) {
        val params = mapOf(clickFrom to value)
        logCusEvent(MyApp.Config.imageClick, params)
    }

    fun imageTransCameraEvent() {
        logCusEvent(MyApp.Config.imageTransCamera, null)
    }


    fun imageTransPhotoEvent() {
        logCusEvent(MyApp.Config.imageTransPhoto, null)
    }

    fun historyClickEvent() {
        logCusEvent(MyApp.Config.historyClick, null)
    }

    fun historyDeleteEvent() {
        logCusEvent(MyApp.Config.historyDelete, null)
    }

    fun directoryClickEvent() {
        logCusEvent(MyApp.Config.directoryClick, null)
    }

    fun directoryTransClickEvent() {
        logCusEvent(MyApp.Config.directoryTransClick, null)
    }

    fun directoryTransResultEvent(failReason: String, value: String) {
        val params = mapOf(failReason to value)
        logCusEvent(MyApp.Config.directoryTransResult, params)
    }

    fun hoverGlobalResultEvent(failReason: String, value: String) {
        val params = mapOf(failReason to value)
        logCusEvent(MyApp.Config.hoverGlobalResult, params)
    }

    fun hoverDistrictResultEvent(failReason: String, value: String) {
        val params = mapOf(failReason to value)
        logCusEvent(MyApp.Config.hoverDistrictResult, params)
    }

    fun imageCameraResultEvent(failReason: String, value: String) {
        val params = mapOf(failReason to value)
        logCusEvent(MyApp.Config.imageCameraResult, params)
    }

    fun imagePhotoResultEvent(failReason: String, value: String) {
        val params = mapOf(failReason to value)
        logCusEvent(MyApp.Config.imagePhotoResult, params)
    }


//    oeran  domo

//    fun logEvent(eventName: String, myParam: Map<String, String>? = null) {
//
//        val jsonObject = JSONObject()
//        jsonObject.put("fail", e.toString())
//        val map = mutableMapOf(Pair(MyApp.Config.CLICK_FROM, jsonObject.toString()))
//        FirebaseAnalyticsHelper.logEvent(AnalysisUtil.APP_FIRST_OPEN_FAIL, map)
//
//
//
//        if (myParam != null) {
//            val bundle = Bundle()
//            try {
//                for ((key, value) in myParam) {
//                    if (!TextUtils.isEmpty(value) && !TextUtils.isEmpty(key)) {
//                        bundle.putString(key, value)
//                    }
//                }
//                firebaseAnalytics.logEvent(eventName, bundle)
//            } catch (ignore: Exception) {
//            }
//        } else {
//            try {
//                firebaseAnalytics.logEvent(eventName, null)
//            } catch (ignore: Exception) {
//            }
//        }
//    }
//
}
