package melody.offline.music.util

import android.os.Bundle
import android.text.TextUtils
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import melody.offline.music.ads.AdPlacement
import melody.offline.music.ads.AnalysisAdState

object AnalysisUtil {

    const val PARAM_VALUE = "param_value"

    const val USER_LAUNCH = "user_launch"//用户启动
    const val LAUNCH_PV = "launch_pv"//启动页曝光
    const val HOME_A_PV = "home_a_pv"//A面首页曝光
    const val HOME_B_PV = "home_b_pv"//B面首页曝光
    const val HOME_B_MODULE_SHOW_SUCCESS_ACTION = "home_b_module_show_success_action"//首页资源曝光成功
    const val HOME_B_MODULE_SHOW_FAIL_ACTION = "home_b_module_show_fail_action"//首页资源曝光失败
    const val HOME_B_MODULE_TRY_AGAIN_ACTION = "home_b_module_try_again_action"//首页资源曝光失败
    const val HOME_B_MODULE_CLICK = "home_b_module_click"//点击首页模块
    const val ME_B_PV = "me_b_pv"//B面我的曝光
    const val PLAYER_B_PV = "player_b_pv"//B面播放器曝光
    const val PLAYER_B_DELAY_ACTION = "player_b_delay_action"//统计加载时间
    const val PLAYER_B_SUCCESS_ACTION = "player_b_success_action"//播放成功
    const val PLAYER_B_LOVE_CLICK = "player_b_love_click"//点击收藏
    const val PLAYER_B_UN_LOVE_CLICK = "player_b_un_love_click"//点击收藏
    const val PLAYER_B_DOWNLOAD_CLICK = "player_b_download_click"//点击下载
    const val PLAYER_B_DOWNLOAD_SUCCESS_ACTION = "player_b_download_success_action"//下载成功
    const val SEARCH_PV = "search_pv"//B面搜索曝光
    const val SEARCH_SUG_SHOW = "search_sug_show"//搜索SUG曝光
    const val SEARCH_SUG_CLICK = "search_sug_click"//点击sug结果
    const val SEARCH_RESULT_PV = "search_result_pv"//搜索结果曝光
    const val SEARCH_RESULT_SUCCESS_ACTION = "search_result_success_action"//搜索有结果
    const val SEARCH_TRIGGER = "search_trigger "//触发搜索
    const val RATING_DIALOG_SHOW = "rating_dialog_show"
    const val RATING_DIALOG_OK_CLICK = "rating_dialog_ok_click"
    const val RATING_DIALOG_CANCEL_CLICK = "rating_dialog_cancel_click"
    const val HOME_A_TO_B = "home_a_to_b"
    const val APP_FIRST_OPEN_SUCCESS = "app_first_open_success"
    const val APP_FIRST_OPEN_FAIL = "app_first_open_fail"
    const val APP_OPEN_SUCCESS = "app_open_success"
    const val APP_OPEN_FAIL = "app_open_fail"


    private const val AD_INST_SPLASH_SUCCESS = "ad_user_open_success"//首页开屏广告展示成功
    private const val AD_INST_SPLASH_FAIL = "ad_user_open_fail"//首页开屏广告展示失败
    private const val AD_INST_SPLASH_LOAD_FAIL = "ad_user_open_load_fail"//首页开屏广告展示load失败

    private const val AD_INST_SEARCH_SUCCESS = "ad_inst_search_success"
    private const val AD_INST_SEARCH_FAIL = "ad_inst_search_fail"
    private const val AD_INST_SEARCH_LOAD_FAIL = "ad_inst_search_load_fail"

    private const val AD_INST_INTO_PLAY_SUCCESS = "ad_inst_into_play_success"
    private const val AD_INST_INTO_PLAY_FAIL = "ad_inst_into_play_fail"
    private const val AD_INST_INTO_PLAY_LOAD_FAIL = "ad_inst_into_play_load_fail"

    private const val AD_INST_CUTTING_SONG_SUCCESS = "ad_inst_cutting_song_success"
    private const val AD_INST_CUTTING_SONG_FAIL = "ad_inst_cutting_song_fail"
    private const val AD_INST_CUTTING_SONG_LOAD_FAIL = "ad_inst_cutting_song_load_fail"

    private const val AD_INST_DOWNLOAD_SUCCESS = "ad_inst_download_success"
    private const val AD_INST_DOWNLOAD_FAIL = "ad_inst_download_fail"
    private const val AD_INST_DOWNLOAD_LOAD_FAIL = "ad_inst_download_load_fail"

    private const val AD_INST_ME_PAGE_LIST_SUCCESS = "ad_inst_me_page_list_success"
    private const val AD_INST_ME_PAGE_LIST_FAIL = "ad_inst_me_page_list_fail"
    private const val AD_INST_ME_PAGE_LIST_LOAD_FAIL = "ad_inst_me_page_list_load_fail"


    private var firebaseAnalytics: FirebaseAnalytics? = null

    fun logEvent(eventName: String, myParam: Map<String, String>? = null) {
        try {
            if (firebaseAnalytics == null) {
                firebaseAnalytics = Firebase.analytics
            }
        } catch (ignore: Exception) {
        }
        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) {
            }
        }
    }

    /**
     * 统一广告打点方法
     */
    fun placeToLogEvent(
        place: String, state: Int, param: Map<String, String>? = null
    ) {
        when (place) {//对应广告位，进行打点。
            //开屏广告，启动页广告位
            AdPlacement.INST_SPLASH -> {
                when (state) {
                    AnalysisAdState.AD_LOAD_FAILED -> {
                        logEvent(AD_INST_SPLASH_LOAD_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOW_FAILED -> {
                        logEvent(AD_INST_SPLASH_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOWN -> {
                        logEvent(AD_INST_SPLASH_SUCCESS)
                    }
                }
            }

            AdPlacement.INST_SEARCH -> {
                when (state) {
                    AnalysisAdState.AD_LOAD_FAILED -> {
                        logEvent(AD_INST_SEARCH_LOAD_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOW_FAILED -> {
                        logEvent(AD_INST_SEARCH_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOWN -> {
                        logEvent(AD_INST_SEARCH_SUCCESS)
                    }
                }
            }

            AdPlacement.INST_INTO_PLAY -> {
                when (state) {
                    AnalysisAdState.AD_LOAD_FAILED -> {
                        logEvent(AD_INST_INTO_PLAY_LOAD_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOW_FAILED -> {
                        logEvent(AD_INST_INTO_PLAY_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOWN -> {
                        logEvent(AD_INST_INTO_PLAY_SUCCESS)
                    }
                }
            }

            AdPlacement.INST_CUTTING_SONG -> {
                when (state) {
                    AnalysisAdState.AD_LOAD_FAILED -> {
                        logEvent(AD_INST_CUTTING_SONG_LOAD_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOW_FAILED -> {
                        logEvent(AD_INST_CUTTING_SONG_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOWN -> {
                        logEvent(AD_INST_CUTTING_SONG_SUCCESS)
                    }
                }
            }

            AdPlacement.INST_DOWNLOAD -> {
                when (state) {
                    AnalysisAdState.AD_LOAD_FAILED -> {
                        logEvent(AD_INST_DOWNLOAD_LOAD_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOW_FAILED -> {
                        logEvent(AD_INST_DOWNLOAD_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOWN -> {
                        logEvent(AD_INST_DOWNLOAD_SUCCESS)
                    }
                }
            }

            AdPlacement.INST_ME_PAGE_LIST -> {
                when (state) {
                    AnalysisAdState.AD_LOAD_FAILED -> {
                        logEvent(AD_INST_ME_PAGE_LIST_LOAD_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOW_FAILED -> {
                        logEvent(AD_INST_ME_PAGE_LIST_FAIL, param)
                    }

                    AnalysisAdState.AD_SHOWN -> {
                        logEvent(AD_INST_ME_PAGE_LIST_SUCCESS)
                    }
                }
            }
        }
    }
}