package com.blaze.rtnblazesdk.customization import BlazeReactCustomActionButton import BlazeReactStoryPlayerButtonsStyle import BlazeReactStoryPlayerChipStyle import BlazeReactStoryPlayerChipsStyle import BlazeReactStoryPlayerCtaStyle import BlazeReactStoryPlayerFirstTimeSlideInstructionsStyle import BlazeReactStoryPlayerFirstTimeSlideStyle import BlazeReactStoryPlayerHeaderGradientStyle import BlazeReactStoryPlayerLastUpdateTextStyle import BlazeReactStoryPlayerProgressBarStyle import BlazeReactStoryPlayerStyle import BlazeReactStoryPlayerTitleImageSource import BlazeReactStoryPlayerTitleImageStaticSource import BlazeReactStoryPlayerTitleImageStyle import BlazeReactStoryPlayerTitleTextStyle import BlazeReactTextCase import android.content.Context import com.blaze.blazesdk.style.players.BlazeTextCase import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerButtonsStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerTitleImageStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerChipStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerChipsStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerCtaStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerCustomActionButton import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerFirstTimeSlideInstructionsStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerFirstTimeSlideStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerHeaderGradientStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerLastUpdateTextStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerProgressBarStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerStyle import com.blaze.blazesdk.style.players.stories.BlazeStoryPlayerTitleTextStyle import com.blaze.blazesdk.style.shared.models.blazeDp fun BlazeStoryPlayerStyle.mergedWith( customization: BlazeReactStoryPlayerStyle?, context: Context ): BlazeStoryPlayerStyle { customization ?: return this val merged = this merged.title = this.title.mergedWith(customization.title,context) merged.lastUpdate = this.lastUpdate.mergedWith(customization.lastUpdate, context) merged.buttons = this.buttons.mergedWith(customization.buttons,context) merged.chips = this.chips.mergedWith(customization.chips) merged.backgroundColor = safeParseColor(customization.backgroundColor) ?: this.backgroundColor merged.cta = this.cta.mergedWith(customization.cta, context) merged.headerGradient = this.headerGradient.mergedWith(customization.headerGradient) merged.firstTimeSlide.mergedWith(customization.firstTimeSlide,context) merged.progressBar = this.progressBar.mergedWith(customization.progressBar) return merged } fun BlazeStoryPlayerFirstTimeSlideStyle.mergedWith( customization: BlazeReactStoryPlayerFirstTimeSlideStyle?, context: Context ): BlazeStoryPlayerFirstTimeSlideStyle { customization ?: return this val merged = this merged.show = customization.show ?: this.show customization.backgroundColor?.colorFileName?.toColorResId(context)?.let { merged.backgroundColorResId = it } merged.cta = this.cta.mergedWith(customization.cta, context) merged.mainTitle = this.mainTitle.mergedWith(customization.mainTitle, context) merged.subtitle = this.subtitle.mergedWith(customization.subtitle, context) merged.instructions = this.instructions.mergedWith(customization.instructions, context) return merged } fun BlazeStoryPlayerFirstTimeSlideInstructionsStyle.mergedWith( customization: BlazeReactStoryPlayerFirstTimeSlideInstructionsStyle?, context: Context ): BlazeStoryPlayerFirstTimeSlideInstructionsStyle { customization ?: return this val merged = this merged.forward = this.forward.mergedWith(customization.forward,context) merged.pause = this.pause.mergedWith(customization.pause,context) merged.backward = this.backward.mergedWith(customization.backward,context) merged.transition = this.transition.mergedWith(customization.transition,context) return merged } fun BlazeStoryPlayerTitleTextStyle.mergedWith( customization: BlazeReactStoryPlayerTitleTextStyle?, context: Context ): BlazeStoryPlayerTitleTextStyle { customization ?: return this val merged = this merged.fontResId = customization.font?.toFontResId(context) merged.textSize = customization.textSize ?: merged.textSize merged.textColor = safeParseColor(customization.textColor) ?: this.textColor merged.isVisible = customization.isVisible ?: this.isVisible merged.image = merged.image.mergedWith(customization.image, context) return merged } fun BlazeStoryPlayerTitleImageStyle.mergedWith( customization: BlazeReactStoryPlayerTitleImageStyle?, context: Context ): BlazeStoryPlayerTitleImageStyle { customization ?: return this val merged = this merged.isVisible = customization.isVisible ?: this.isVisible merged.size = customization.size?.toInt()?.blazeDp ?: this.size customization.source?.let { reactSource -> when (reactSource.type) { "static" -> { reactSource.staticSource?.toNativeStaticSource(context)?.let { merged.source = it } } "dynamic" -> { merged.source = BlazeStoryPlayerTitleImageStyle.BlazeStoryPlayerTitleImageSource.Dynamic( fallback = reactSource.fallback?.toNativeStaticSource(context) ) } } } return merged } private fun BlazeReactStoryPlayerTitleImageStaticSource.toNativeStaticSource( context: Context ): BlazeStoryPlayerTitleImageStyle.BlazeStoryPlayerTitleImageSource.Static? { return when (type) { "image" -> { val resId = image?.toImageResId(context) ?: return null BlazeStoryPlayerTitleImageStyle.BlazeStoryPlayerTitleImageSource.Static.ResId(resId) } "url" -> { url ?: return null BlazeStoryPlayerTitleImageStyle.BlazeStoryPlayerTitleImageSource.Static.Url(url) } else -> null } } fun BlazeStoryPlayerLastUpdateTextStyle.mergedWith( customization: BlazeReactStoryPlayerLastUpdateTextStyle?, context: Context ): BlazeStoryPlayerLastUpdateTextStyle { customization ?: return this val merged = this merged.fontResId = customization.font?.toFontResId(context) merged.textColor = safeParseColor(customization.textColor) ?: this.textColor merged.textSize = customization.textSize ?: merged.textSize merged.textCase = merged.textCase.mergeWith(customization.textCase) merged.isVisible = customization.isVisible ?: this.isVisible return merged } fun BlazeTextCase.mergeWith(customization: BlazeReactTextCase?): BlazeTextCase { customization ?: return this return customization.mapToBlazeEnumClass() } fun BlazeStoryPlayerButtonsStyle.mergedWith( customization: BlazeReactStoryPlayerButtonsStyle?, context: Context ): BlazeStoryPlayerButtonsStyle { customization ?: return this val merged = this merged.mute = merged.mute.mergeButtonThemes(customization.mute, context) merged.exit = merged.exit.mergeButtonThemes(customization.exit, context) merged.share = merged.share.mergeButtonThemes(customization.share, context) mergeCustomActionButtons(customization.customActionButtons, context)?.let { merged.setTopStackCustomActionButtons(it) } return merged } private fun mergeCustomActionButtons( reactButtons: List?, context: Context ): List? { reactButtons ?: return null val nativeButtons = reactButtons.mapNotNull { reactButton -> val params = reactButton.customParams?.toNativeParams() ?: return@mapNotNull null val button = BlazeStoryPlayerCustomActionButton(customParams = params) button.style = button.style.mergeButtonThemes(reactButton.style, context) button } return nativeButtons.ifEmpty { return null } } fun BlazeStoryPlayerCtaStyle.mergedWith( customization: BlazeReactStoryPlayerCtaStyle?, context: Context ): BlazeStoryPlayerCtaStyle { customization ?: return this val merged = this merged.cornerRadius = customization.cornerRadius?.blazeDp ?: this.cornerRadius merged.textSize = customization.textSize ?: this.textSize merged.fontResId = customization.font?.toFontResId(context) merged.isVisible = customization.isVisible ?: this.isVisible return merged } fun BlazeStoryPlayerChipsStyle.mergedWith( customization: BlazeReactStoryPlayerChipsStyle?, ): BlazeStoryPlayerChipsStyle { customization ?: return this val merged = this merged.live = this.live.mergedWith(customization.live) merged.ad = this.ad.mergedWith(customization.ad) return merged } fun BlazeStoryPlayerChipStyle.mergedWith( customization: BlazeReactStoryPlayerChipStyle?, ): BlazeStoryPlayerChipStyle { customization ?: return this val merged = this merged.padding = this.padding.mergedWith(customization.titlePadding) merged.text = customization.text ?: this.text merged.textColor = safeParseColor(customization.textColor) ?: this.textColor merged.backgroundColor = safeParseColor(customization.backgroundColor) ?: this.backgroundColor merged.isVisible = customization.isVisible ?: this.isVisible return merged } fun BlazeStoryPlayerProgressBarStyle.mergedWith( customization: BlazeReactStoryPlayerProgressBarStyle?, ): BlazeStoryPlayerProgressBarStyle { customization ?: return this val merged = this merged.backgroundColor = safeParseColor(customization.backgroundColor) ?: this.backgroundColor merged.progressColor = safeParseColor(customization.progressColor) ?: this.progressColor return merged } fun BlazeStoryPlayerHeaderGradientStyle.mergedWith( customization: BlazeReactStoryPlayerHeaderGradientStyle?, ): BlazeStoryPlayerHeaderGradientStyle { customization ?: return this val merged = this merged.isVisible = customization.isVisible ?: this.isVisible merged.startColor = safeParseColor(customization.startColor) ?: this.startColor merged.endColor = safeParseColor(customization.endColor) ?: this.endColor return merged }