package com.blaze.rtnblazesdk.customization import BlazeReactFirstTimeSlideCTAStyle import BlazeReactFirstTimeSlideInstructionStyle import BlazeReactFirstTimeSlideTextStyle import BlazeReactImage import BlazeReactMargins import BlazeReactPlayerButtonCustomImageStates import android.content.Context import com.blaze.blazesdk.style.players.BlazeFirstTimeSlideCTAStyle import com.blaze.blazesdk.style.players.BlazeFirstTimeSlideInstructionStyle import com.blaze.blazesdk.style.players.BlazeFirstTimeSlideTextStyle import com.blaze.blazesdk.style.players.BlazePlayerButtonCustomImageStates import com.blaze.blazesdk.style.shared.models.BlazeInsets import com.blaze.blazesdk.style.shared.models.blazeDp fun BlazePlayerButtonCustomImageStates?.mergedWith( customization: BlazeReactPlayerButtonCustomImageStates, context: Context ): BlazePlayerButtonCustomImageStates? { val imageUnselectedPath = customization.unselectedImage?.toImageResId(context) ?: return this return BlazePlayerButtonCustomImageStates ( imageUnselectedPathResId = imageUnselectedPath, imageSelectedPathResId = customization.selectedImage?.toImageResId(context) ) } fun BlazeFirstTimeSlideInstructionStyle.mergedWith( customization: BlazeReactFirstTimeSlideInstructionStyle?, context: Context ): BlazeFirstTimeSlideInstructionStyle { customization ?: return this val merged = this merged.headerText = this.headerText.mergedWith(customization.headerText,context) merged.descriptionText = this.descriptionText.mergedWith(customization.descriptionText,context) return merged } fun BlazeFirstTimeSlideTextStyle.mergedWith( customization: BlazeReactFirstTimeSlideTextStyle?, context: Context ): BlazeFirstTimeSlideTextStyle { customization ?: return this val merged = this merged.text = customization.text ?: this.text customization.textColor?.colorFileName?.toColorResId(context)?.let { merged.textColorResId = it } merged.fontResId = customization.font?.toFontResId(context) merged.textSize = customization.textSize ?: this.textSize return merged } fun BlazeFirstTimeSlideCTAStyle.mergedWith( customization: BlazeReactFirstTimeSlideCTAStyle?, context: Context ): BlazeFirstTimeSlideCTAStyle { customization ?: return this val merged = this merged.title = customization.title ?: this.title merged.cornerRadius = customization.cornerRadius?.blazeDp ?: this.cornerRadius customization.backgroundColor?.colorFileName?.toColorResId(context)?.let { merged.backgroundColor = it } customization.textColor?.colorFileName?.toColorResId(context)?.let { merged.textColorResId = it } merged.fontResId = customization.font?.toFontResId(context) merged.textSize = customization.textSize ?: this.textSize return merged } fun BlazeInsets.mergedWith( customization: BlazeReactMargins?, ): BlazeInsets { customization ?: return this val merged = this merged.top = customization.top?.blazeDp ?: this.top merged.bottom = customization.bottom?.blazeDp ?: this.bottom merged.start = customization.leading?.blazeDp ?: this.start merged.end = customization.trailing?.blazeDp ?: this.end return merged } fun BlazeReactImage.toImageResId(context: Context): Int? { imageName ?: return null var imageResId = try { val imageFileNameExcludingExtension = imageName.split(".")[0] val imageID = context.resources.getIdentifier( imageFileNameExcludingExtension, "drawable", context.applicationContext.packageName ) if (imageID != 0) { imageID } else { null } } catch (e: Exception) { //NO OP null } return imageResId } fun safeParseColor(hexString: String?): Int? { hexString ?: return null return try { BlazeColorParser.rgbaToColorInt(colorString = hexString) } catch (e: Throwable) { null } } fun String?.toColorResId(context: Context): Int? { this ?: return null var colorResId = try { val fontFileNameExcludingExtension = this.split(".")[0] val colorID = context.resources.getIdentifier( fontFileNameExcludingExtension, "color", context.applicationContext.packageName) if (colorID != 0) { colorID } else { null } } catch (e: Exception) { //NO OP null } return colorResId }