package com.gh.gamecenter.provider

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import com.gh.gamecenter.GdtHelper
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.leon.channel.helper.ChannelReaderUtil

class FlavorProviderImp : IFlavorProvider {

    override fun init(application: Application, activity: Activity, activateRatio: Int) {
        GdtHelper.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 = ChannelReaderUtil.getChannel(application)
        if (channel == null || TextUtils.isEmpty(channel.trim())) {
            channel = GDT_DEFAULT_CHANNEL
        }
        return channel
    }

    override fun logEvent(content: String) {
        GdtHelper.logAction(content)
    }

    companion object {
        private const val GDT_DEFAULT_CHANNEL = "GDT_GHZS_01"
    }
}