package com.adealink.weparty.module.playmate.event

import com.adealink.frame.log.Log
import com.adealink.frame.statistics.BaseStatEvent
import com.adealink.frame.statistics.CommonEventKey
import com.adealink.frame.statistics.IEventValue
import com.adealink.weparty.App
import com.adealink.weparty.module.playmate.data.PlaymateExposureReq
import com.adealink.weparty.module.playmate.data.PlaymateVisitorReq
import com.adealink.weparty.module.playmate.data.TAG_PLAYMATE_EVENT
import com.adealink.weparty.module.playmate.datasource.PlaymateHttpService
import kotlinx.coroutines.launch

class PlaymateStatEvent(override val action: IEventValue) : BaseStatEvent("playmate") {

    private val playmateHttpService by lazy {
        App.instance.networkService.getHttpService(PlaymateHttpService::class.java)
    }

    enum class Action(override val value: String, override val desc: String) : IEventValue {
        EXPOSURE("exposure", "陪玩师曝光"),
        VISIT("visit", "访问")
    }

    val id = Param(CommonEventKey.ID)
    val ids = Param("ids")

    override fun send() {
        launch {
            when (action) {
                Action.EXPOSURE -> {
                    val playmateIds = ids.value as? List<String> ?: return@launch
                    Log.d(TAG_PLAYMATE_EVENT, "report exposure, id:${playmateIds.joinToString(separator = ",")}")
                    playmateHttpService.reportPlaymateExposure(PlaymateExposureReq(playmateIds))
                }

                Action.VISIT -> {
                    val uid = id.value as? String ?: return@launch
                    Log.d(TAG_PLAYMATE_EVENT, "report visitor, id:$uid")
                    playmateHttpService.reportPlaymateVisitor(PlaymateVisitorReq(uid))
                }
            }
        }
    }

    override fun toString(): String {
        return "PlaymateStatEvent(id:${id.value})"
    }
}