package com.gh.gamecenter.provider

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import com.gh.gamecenter.KuaishouHelper
import com.gh.gamecenter.core.provider.IFlavorProvider
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.TimeUtils
import com.halo.assistant.HaloApp
import com.kwai.monitor.payload.TurboHelper

class FlavorProviderImp : IFlavorProvider {

    override fun init(application: Application, activity: Activity, activateRatio: Int) {
        KuaishouHelper.init(application, getChannelStr(application))

        if (HaloApp.getInstance().isBrandNewInstall) {
            logEvent("EVENT_ACTIVE")
            SPUtils.setLong("TIME_OF_BRAND_NEW_INSTALL", System.currentTimeMillis() / 1000)
        } else {
            val shouldSendRetentionLogEvent =
                SPUtils.getBoolean("SHOULD_SEND_RETENTION_EVENT", true)
            val installTimeNotToday =
                !TimeUtils.isToday(SPUtils.getLong("TIME_OF_BRAND_NEW_INSTALL", System.currentTimeMillis() / 1000))
            if (shouldSendRetentionLogEvent && installTimeNotToday) {
                logEvent("EVENT_NEXTDAY_STAY")
                SPUtils.setBoolean("SHOULD_SEND_RETENTION_EVENT", false)
            }
        }
    }

    override fun getChannelStr(application: Application): String {
        var channel = TurboHelper.getChannel(application)
        if (channel == null || TextUtils.isEmpty(channel.trim())) {
            channel = KUAISHOU_CHANNEL
        }
        return channel
    }

    override fun logEvent(content: String) {
        KuaishouHelper.onEvent(content)
    }

    companion object {
        private const val KUAISHOU_CHANNEL = "KS-GHZS-01"
    }
}