package com.fireworksdk.bridge.models import com.fireworksdk.bridge.models.enums.FWCtaDelayType import com.fireworksdk.bridge.models.enums.FWFeedCompleteAction import com.fireworksdk.bridge.models.enums.FWPlayerStyle import com.fireworksdk.bridge.models.enums.FWShareButtonPosition import com.fireworksdk.bridge.models.enums.FWVideoCompleteAction import com.fireworksdk.bridge.models.enums.FWVideoPlayerCTAWidth import com.fireworksdk.bridge.models.enums.FWVideoPlayerScrollDirection import org.json.JSONObject object FWVideoPlayerConfigModelDeserializer { private const val PLAYER_STYLE_KEY = "playerStyle" private const val SCROLL_DIRECTION_KEY = "scrollDirection" private const val ENABLE_SCROLL_FOR_VERTICAL_KEY = "enableScrollForVertical" private const val VIDEO_COMPLETE_ACTION_KEY = "videoCompleteAction" private const val FEED_COMPLETE_ACTION_KEY = "feedCompleteAction" private const val SHOW_SHARE_BUTTON_KEY = "showShareButton" private const val CTA_BUTTON_STYLE_KEY = "ctaButtonStyle" private const val SHOW_MUTE_BUTTON_KEY = "showMuteButton" private const val SHOW_PLAYBACK_BUTTON_KEY = "showPlaybackButton" private const val SHOW_BRANDING_KEY = "showBranding" private const val CTA_DELAY_KEY = "ctaDelay" private const val CTA_HIGHLIGHT_DELAY_KEY = "ctaHighlightDelay" private const val SHARE_BASE_URL_KEY = "shareBaseURL" private const val CTA_WIDTH_KEY = "ctaWidth" private const val ENABLE_PIP_KEY = "enablePictureInPicture" private const val SHOW_VIDEO_DETAIL_TITLE_KEY = "showVideoDetailTitle" private const val BUTTON_CONFIGURATION_KEY = "buttonConfiguration" private const val VIDEO_PLAYER_LOGO_CONFIGURATION_KEY = "videoPlayerLogoConfiguration" private const val COUNT_DOWN_TIMER_CONFIGURATION_KEY = "countdownTimerConfiguration" private const val ACTION_BUTTON_STYLE_KEY = "actionButtonStyle" private const val CANCEL_BUTTON_STYLE_KEY = "cancelButtonStyle" private const val CHAT_STYLE_KEY = "chatStyle" private const val BACKGROUND_COLOR_KEY = "backgroundColor" private const val FONT_SIZE_KEY = "fontSize" private const val TEXT_COLOR_KEY = "textColor" private const val SHAPE_KEY = "shape" private const val TYPE_KEY = "type" private const val VALUE_KEY = "value" private const val ANDROID_FONT_INFO_KEY = "androidFontInfo" private const val SHADOW_COLOR_KEY = "shadowColor" private const val SHOW_FULL_SCREEN_ICON = "enableFullScreen" private const val ENABLE_AUTOPLAY = "enableAutoplay" private const val ENABLE_AUTOPAUSE = "enableAutopause" private const val ENABLE_SMALL_SIZE_IN_COMPACT = "enableSmallSizeInCompact" private const val SHOULD_EXTEND_MEDIA_OUTSIDE_SAFE_AREA_KEY = "shouldExtendMediaOutsideSafeArea" private const val ADDITIONAL_CONTROLS_INSET_KEY = "additionalControlsInset" private const val IS_ARROW_BUTTON_VISIBLE_KEY = "isArrowButtonVisible" private const val IS_FULLSCREEN_ARROW_BUTTON_VISIBLE_KEY = "isFullscreenArrowButtonVisible" private const val SHOW_MORE_BUTTON_KEY = "showMoreButton" private const val SHARE_BUTTON_POSITION_KEY = "shareButtonPosition" private const val HANDLE_APPEARANCE_MANUALLY_KEY = "handleAppearanceManually" fun deserialize(responseJson: JSONObject?): FWVideoPlayerConfigModel? { responseJson ?: return null val playerStyle = if (responseJson.has(PLAYER_STYLE_KEY)) responseJson.optString(PLAYER_STYLE_KEY) else null val scrollDirection = if (responseJson.has(SCROLL_DIRECTION_KEY)) responseJson.optString(SCROLL_DIRECTION_KEY) else null val enableScrollForVertical = if (responseJson.has(ENABLE_SCROLL_FOR_VERTICAL_KEY)) responseJson.optBoolean(ENABLE_SCROLL_FOR_VERTICAL_KEY) else null val videoCompleteAction = if (responseJson.has(VIDEO_COMPLETE_ACTION_KEY)) responseJson.optString(VIDEO_COMPLETE_ACTION_KEY) else null val feedCompleteAction = if (responseJson.has(FEED_COMPLETE_ACTION_KEY)) responseJson.optString(FEED_COMPLETE_ACTION_KEY) else null val showShareButton = if (responseJson.has(SHOW_SHARE_BUTTON_KEY)) responseJson.optBoolean(SHOW_SHARE_BUTTON_KEY) else null val ctaButtonStyle = deserializeCtaButtonStyle(responseJson.optJSONObject(CTA_BUTTON_STYLE_KEY)) val showMuteButton = if (responseJson.has(SHOW_MUTE_BUTTON_KEY)) responseJson.optBoolean(SHOW_MUTE_BUTTON_KEY) else null val showPlaybackButton = if (responseJson.has(SHOW_PLAYBACK_BUTTON_KEY)) responseJson.optBoolean(SHOW_PLAYBACK_BUTTON_KEY) else null val showBranding = if (responseJson.has(SHOW_BRANDING_KEY)) responseJson.optBoolean(SHOW_BRANDING_KEY) else null val ctaDelay = deserializeCtaDelay(responseJson.optJSONObject(CTA_DELAY_KEY)) val ctaHighlightDelay = deserializeCtaDelay(responseJson.optJSONObject(CTA_HIGHLIGHT_DELAY_KEY)) val shareBaseUrl = if (responseJson.has(SHARE_BASE_URL_KEY)) responseJson.optString(SHARE_BASE_URL_KEY) else null val ctaWidth = if (responseJson.has(CTA_WIDTH_KEY)) responseJson.optString(CTA_WIDTH_KEY) else null val enablePictureInPicture = if (responseJson.has(ENABLE_PIP_KEY)) responseJson.optBoolean(ENABLE_PIP_KEY) else null val showVideoDetailTitle = if (responseJson.has(SHOW_VIDEO_DETAIL_TITLE_KEY)) responseJson.optBoolean(SHOW_VIDEO_DETAIL_TITLE_KEY) else null val buttonConfigurationJsonObject = responseJson.optJSONObject(BUTTON_CONFIGURATION_KEY) val videoPlayerLogoConfigurationJsonObject = responseJson.optJSONObject(VIDEO_PLAYER_LOGO_CONFIGURATION_KEY) val countdownTimerConfigurationJsonObject = responseJson.optJSONObject(COUNT_DOWN_TIMER_CONFIGURATION_KEY) val actionButtonStyleJsonObject = responseJson.optJSONObject(ACTION_BUTTON_STYLE_KEY) val cancelButtonStyleJsonObject = responseJson.optJSONObject(CANCEL_BUTTON_STYLE_KEY) val chatStyleJsonObject = responseJson.optJSONObject(CHAT_STYLE_KEY) val showFullScreenIcon = if (responseJson.has(SHOW_FULL_SCREEN_ICON)) responseJson.optBoolean(SHOW_FULL_SCREEN_ICON) else null val enableAutoPlay = if (responseJson.has(ENABLE_AUTOPLAY)) responseJson.optBoolean(ENABLE_AUTOPLAY) else null val enableAutoPause = if (responseJson.has(ENABLE_AUTOPAUSE)) responseJson.optBoolean(ENABLE_AUTOPAUSE) else null val enableSmallSizeInCompact = if (responseJson.has(ENABLE_SMALL_SIZE_IN_COMPACT)) responseJson.optBoolean(ENABLE_SMALL_SIZE_IN_COMPACT) else null val shouldExtendMediaOutsideSafeArea = if (responseJson.has(SHOULD_EXTEND_MEDIA_OUTSIDE_SAFE_AREA_KEY)) responseJson.optBoolean(SHOULD_EXTEND_MEDIA_OUTSIDE_SAFE_AREA_KEY) else null val additionalControlsInset = if (responseJson.has(ADDITIONAL_CONTROLS_INSET_KEY)) FWControlsInsetModelDeserializer.deserialize(responseJson.optJSONObject(ADDITIONAL_CONTROLS_INSET_KEY)) else null val isArrowButtonVisible = if (responseJson.has(IS_ARROW_BUTTON_VISIBLE_KEY)) responseJson.optBoolean(IS_ARROW_BUTTON_VISIBLE_KEY) else null val isFullscreenArrowButtonVisible = if (responseJson.has(IS_FULLSCREEN_ARROW_BUTTON_VISIBLE_KEY)) responseJson.optBoolean(IS_FULLSCREEN_ARROW_BUTTON_VISIBLE_KEY) else null val showMoreButton = if (responseJson.has(SHOW_MORE_BUTTON_KEY)) responseJson.optBoolean(SHOW_MORE_BUTTON_KEY) else null val shareButtonPosition = if (responseJson.has(SHARE_BUTTON_POSITION_KEY)) responseJson.optString(SHARE_BUTTON_POSITION_KEY) else null val handleAppearanceManually = if (responseJson.has(HANDLE_APPEARANCE_MANUALLY_KEY)) responseJson.optBoolean(HANDLE_APPEARANCE_MANUALLY_KEY) else null return FWVideoPlayerConfigModel( playerStyle = if (!playerStyle.isNullOrBlank()) FWPlayerStyle.deserialize(playerStyle) else null, scrollDirection = if (!scrollDirection.isNullOrBlank()) FWVideoPlayerScrollDirection.deserialize(scrollDirection) else null, enableScrollForVertical = enableScrollForVertical, videoCompleteAction = if (!videoCompleteAction.isNullOrBlank()) FWVideoCompleteAction.deserialize(videoCompleteAction) else null, feedCompleteAction = if (!feedCompleteAction.isNullOrBlank()) FWFeedCompleteAction.deserialize(feedCompleteAction) else null, showShareButton = showShareButton, ctaButtonStyle = ctaButtonStyle, showMuteButton = showMuteButton, showPlaybackButton = showPlaybackButton, showBranding = showBranding, ctaDelay = ctaDelay, ctaHighlightDelay = ctaHighlightDelay, shareBaseUrl = shareBaseUrl, ctaWidth = if (!ctaWidth.isNullOrBlank()) FWVideoPlayerCTAWidth.deserialize(ctaWidth) else null, enablePictureInPicture = enablePictureInPicture, showVideoDetailTitle = showVideoDetailTitle, buttonConfiguration = FWPlayerButtonConfigurationDeserializer.deserialize(buttonConfigurationJsonObject), videoPlayerLogoConfiguration = FWVideoPlayerLogoConfigurationModelDeserializer.deserialize(videoPlayerLogoConfigurationJsonObject), countdownTimerConfiguration = FWCountdownTimerConfigurationModelDeserializer.deserialize(countdownTimerConfigurationJsonObject), actionButtonStyle = FWActionButtonDeserializer.deserialize(actionButtonStyleJsonObject), cancelButtonStyle = FWActionButtonDeserializer.deserialize(cancelButtonStyleJsonObject), showFullScreenIcon = showFullScreenIcon, enableAutoPlay = enableAutoPlay, enableAutoPause = enableAutoPause, enableSmallSizeInCompact = enableSmallSizeInCompact, chatStyle = FWChatConfigModelDeserializer.deserialize(chatStyleJsonObject), shouldExtendMediaOutsideSafeArea = shouldExtendMediaOutsideSafeArea, additionalControlsInset = additionalControlsInset, isArrowButtonVisible = isArrowButtonVisible, isFullscreenArrowButtonVisible = isFullscreenArrowButtonVisible, showMoreButton = showMoreButton, shareButtonPosition = if (!shareButtonPosition.isNullOrBlank()) FWShareButtonPosition.deserialize(shareButtonPosition) else null, handleAppearanceManually = handleAppearanceManually, ) } private fun deserializeCtaButtonStyle(ctaButtonStyleJson: JSONObject?): FWVideoPlayerConfigModel.FWCtaButtonStyleModel { ctaButtonStyleJson ?: return FWVideoPlayerConfigModel.FWCtaButtonStyleModel() val backgroundColor = if (ctaButtonStyleJson.has(BACKGROUND_COLOR_KEY)) ctaButtonStyleJson.optString(BACKGROUND_COLOR_KEY) else null val fontSize = if (ctaButtonStyleJson.has(FONT_SIZE_KEY)) ctaButtonStyleJson.optInt(FONT_SIZE_KEY) else null val textColor = if (ctaButtonStyleJson.has(TEXT_COLOR_KEY)) ctaButtonStyleJson.optString(TEXT_COLOR_KEY) else null val shape = if (ctaButtonStyleJson.has(SHAPE_KEY)) ctaButtonStyleJson.optString(SHAPE_KEY) else null return FWVideoPlayerConfigModel.FWCtaButtonStyleModel( backgroundColor = backgroundColor, fontSize = fontSize, textColor = textColor, shape = if (!shape.isNullOrBlank()) FWShape.deserialize(shape) else null, ) } private fun deserializeCtaDelay(ctaDelayJson: JSONObject?): FWVideoPlayerConfigModel.FWCtaDelayModel { ctaDelayJson ?: return FWVideoPlayerConfigModel.FWCtaDelayModel() val type = if (ctaDelayJson.has(TYPE_KEY)) ctaDelayJson.optString(TYPE_KEY) else null val value = if (!ctaDelayJson.isNull(VALUE_KEY)) ctaDelayJson.optDouble(VALUE_KEY) else null return FWVideoPlayerConfigModel.FWCtaDelayModel( type = if (!type.isNullOrBlank()) FWCtaDelayType.deserialize(type) else null, value = value, ) } }