package com.blaze.rtnblazegam.module import android.os.Bundle import android.util.Log import androidx.annotation.Keep import com.blaze.blazesdk.ads.banners.BlazeGAMBannerHandlerEventType import com.blaze.blazesdk.ads.custom_native.BlazeGoogleCustomNativeAdsHandler import com.blaze.gam.BlazeGAM import com.blaze.gam.banner.BlazeGAMBannerAdsAdData import com.blaze.gam.banner.BlazeGAMBannerAdsDelegate import com.blaze.gam.custom_native.BlazeCustomNativeAdData import com.blaze.gam.custom_native.BlazeGAMCustomNativeAdsDelegate import com.blaze.gam.custom_native.BlazeGamCustomNativeAdRequestInformation import com.blaze.rtnblazesdk.ads.BlazeRTNContentExtraInfo import com.blaze.rtnblazesdk.ads.toReactNativeModel import com.blaze.rtnblazesdk.utils.populateContainer import com.blaze.rtnblazesdk.shared.blazeAsyncBridge import com.blaze.rtnblazesdk.shared.callJSMethod import com.facebook.proguard.annotations.DoNotStrip import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.ReadableMap import javax.annotation.Nonnull class BlazeGAMSdkModule(private val context: ReactApplicationContext) : ReactContextBaseJavaModule(context) { @Nonnull override fun getName(): String { return NAME } private val customNativeAdsDelegate: BlazeGAMCustomNativeAdsDelegate = object : BlazeGAMCustomNativeAdsDelegate { override fun onGAMCustomNativeAdEvent( eventType: BlazeGoogleCustomNativeAdsHandler.EventType, adData: BlazeCustomNativeAdData ) { this@BlazeGAMSdkModule.onGAMCustomNativeAdEvent(eventType, adData) } override fun onGAMCustomNativeAdError(errMsg: String) { this@BlazeGAMSdkModule.onGAMCustomNativeAdError(errMsg) } override suspend fun customGAMTargetingProperties( requestData: BlazeGamCustomNativeAdRequestInformation ): Map { return this@BlazeGAMSdkModule.onCustomNativeCustomGAMTargetingProperties(requestData) ?: super.customGAMTargetingProperties( requestData = requestData ) } override suspend fun publisherProvidedId( requestData: BlazeGamCustomNativeAdRequestInformation ): String? { return this@BlazeGAMSdkModule.publisherProvidedId(requestData) } override suspend fun networkExtras( requestData: BlazeGamCustomNativeAdRequestInformation ): Bundle? { return this@BlazeGAMSdkModule.networkExtras(requestData) } } private val bannerAdsDelegate: BlazeGAMBannerAdsDelegate = object : BlazeGAMBannerAdsDelegate { override fun onGAMBannerAdsAdEvent( eventType: BlazeGAMBannerHandlerEventType, adData: BlazeGAMBannerAdsAdData ) { this@BlazeGAMSdkModule.onGAMBannerAdsAdEvent( eventType = eventType, adData = adData ) } override fun onGAMBannerAdsAdError( errorMsg: String, adData: BlazeGAMBannerAdsAdData ) { this@BlazeGAMSdkModule.onGAMBannerAdsAdError( errorMsg = errorMsg, adData = adData ) } } @ReactMethod @DoNotStrip fun enableCustomNativeAds(options: ReadableMap) { // Initialize the sdk. val options = options.extractEnableCustomNativeAdsOptions() ?: run { Log.e( TAG, "Error extracting params of `enableCustomNativeAds`: $options" ) return } BlazeGAM.enableCustomNativeAds( context = context, defaultAdsConfig = options.defaultAdConfig?.asNativeModel, delegate = customNativeAdsDelegate ) } @ReactMethod @DoNotStrip fun disableCustomNativeAds() { BlazeGAM.disableCustomNativeAds() } @ReactMethod @DoNotStrip fun enableBannerAds() { // Initialize the sdk. BlazeGAM.enableBannerAds( context = context, delegate = bannerAdsDelegate ) } @ReactMethod @DoNotStrip fun disableBannerAds() { BlazeGAM.disableBannerAds() } private fun sendJsEvent(event: SdkModuleJsEvent) { context.emitDeviceEvent(event.eventName, event.params) } // MARK: - Custom Native Delegate Implementation Functions private fun onGAMCustomNativeAdError(errorMessage: String) { val event = SdkModuleJsEvent.OnCustomNativeAdErrorEvent(errorMessage = errorMessage) sendJsEvent(event) } private fun onGAMCustomNativeAdEvent( eventType: BlazeGoogleCustomNativeAdsHandler.EventType, adData: BlazeCustomNativeAdData ) { val event = SdkModuleJsEvent.OnCustomNativeAdEvent(eventType = eventType, adData = adData) sendJsEvent(event) } private suspend fun onCustomNativeCustomGAMTargetingProperties( requestData: BlazeGamCustomNativeAdRequestInformation ): Map? { val asyncBridge = context.blazeAsyncBridge ?: return null return try { val result: Map = asyncBridge.callJSMethod( name = "BlazeGAM.customGAMTargetingProperties", params = requestData.toReactNativeModel() ) result } catch (e: Exception) { // Log specific error and continue with fallback Log.d(TAG, "Error customGAMTargetingProperties: ${e.message}") null } } private suspend fun publisherProvidedId( requestData: BlazeGamCustomNativeAdRequestInformation ): String? { val asyncBridge = context.blazeAsyncBridge ?: return null return try { val result: String? = asyncBridge.callJSMethod( name = "BlazeGAM.publisherProvidedId", params = requestData.toReactNativeModel() ) result } catch (e: Exception) { Log.d(TAG, "Error publisherProvidedId: ${e.message}") null } } private suspend fun networkExtras( requestData: BlazeGamCustomNativeAdRequestInformation ): Bundle? { val asyncBridge = context.blazeAsyncBridge ?: return null return try { val dict: Map = asyncBridge.callJSMethod( name = "BlazeGAM.networkExtras", params = requestData.toReactNativeModel() ) dict.populateContainer( container = Bundle(), putString = Bundle::putString, putBoolean = Bundle::putBoolean, putInt = Bundle::putInt, putDouble = Bundle::putDouble, putFloat = Bundle::putFloat, putLong = Bundle::putLong ) } catch (e: Exception) { Log.d(TAG, "Error networkExtras: ${e.message}") null } } // MARK: - Banner Ads Delegate Implementation Functions private fun onGAMBannerAdsAdEvent( eventType: BlazeGAMBannerHandlerEventType, adData: BlazeGAMBannerAdsAdData ) { val event = SdkModuleJsEvent.OnGAMBannerAdsAdEvent(eventType = eventType, adData = adData) sendJsEvent(event) } private fun onGAMBannerAdsAdError( errorMsg: String, adData: BlazeGAMBannerAdsAdData ) { val event = SdkModuleJsEvent.OnGAMBannerAdsAdError( errorMessage = errorMsg, adData = adData ) sendJsEvent(event) } companion object { const val NAME = "RTNBlazeGAM" val TAG: String = BlazeGAMSdkModule::class.java.simpleName } } @Keep private data class RTNBlazeGAMCustomNativeAdRequestParams( val requestDataInfo: RequestDataInfo ) { @Keep data class RequestDataInfo( val adUnitId: String, val templateId: String, val adContext: Map, val extraInfo: BlazeRTNContentExtraInfo ) } private fun BlazeGamCustomNativeAdRequestInformation.toReactNativeModel(): RTNBlazeGAMCustomNativeAdRequestParams { return RTNBlazeGAMCustomNativeAdRequestParams( requestDataInfo = RTNBlazeGAMCustomNativeAdRequestParams.RequestDataInfo( adUnitId = this.adUnitId, templateId = this.templateId, adContext = this.adContext, extraInfo = this.extraInfo.toReactNativeModel() ) ) }