package com.tangem.features.pushnotifications.api.analytics

import com.tangem.core.analytics.models.AnalyticsEvent
import com.tangem.core.analytics.models.AnalyticsParam

sealed class PushNotificationAnalyticEvents(
    event: String,
    params: Map<String, String> = emptyMap(),
) : AnalyticsEvent(category = "Push", event = event, params = params) {

    data class ButtonAllow(
        val source: AnalyticsParam.ScreensSources,
    ) : PushNotificationAnalyticEvents(
        event = "Button - Allow",
        params = mapOf(
            AnalyticsParam.SOURCE to source.value,
        ),
    )

    data class ButtonCancel(
        val source: AnalyticsParam.ScreensSources,
    ) : PushNotificationAnalyticEvents(
        event = "Button - Cancel",
        params = mapOf(
            AnalyticsParam.SOURCE to source.value,
        ),
    )

    data class ButtonLater(
        val source: AnalyticsParam.ScreensSources,
    ) : PushNotificationAnalyticEvents(
        event = "Button - Later",
        params = mapOf(
            AnalyticsParam.SOURCE to source.value,
        ),
    )

    data class PermissionStatus(
        val isAllowed: Boolean,
    ) : PushNotificationAnalyticEvents(
        event = "Permission Status",
        params = mapOf(
            AnalyticsParam.STATE to if (isAllowed) "Allow" else "Cancel",
        ),
    )

    data class NotificationOpened(
        val type: String,
    ) : PushNotificationAnalyticEvents(
        event = "Push Notification Opened",
        params = mapOf(
            AnalyticsParam.TYPE to type,
        ),
    )

    data class NotificationsScreenOpened(
        val source: AnalyticsParam.ScreensSources,
    ) : PushNotificationAnalyticEvents(
        event = "Push Notification Screen Opened",
        params = mapOf(
            AnalyticsParam.SOURCE to source.value,
        ),
    )

    data class NotificationsEnabled(
        val isEnabled: Boolean,
    ) : PushNotificationAnalyticEvents(
        event = "Push Toggle Clicked",
        params = mapOf(
            AnalyticsParam.STATE to if (isEnabled) "On" else "Off",
        ),
    )

    data class NotificationSettingsScreenOpened(
        val isSystemPermissionEnabled: Boolean,
    ) : PushNotificationAnalyticEvents(
        event = "Notification Settings Screen Opened",
        params = mapOf(
            AnalyticsParam.STATE to isSystemPermissionEnabled.toString(),
        ),
    )

    data class ToggleClicked(
        val toggleType: String,
        val isEnabled: Boolean,
    ) : PushNotificationAnalyticEvents(
        event = "Toggle Clicked",
        params = mapOf(
            "Toggle Type" to toggleType,
            AnalyticsParam.STATE to if (isEnabled) "On" else "Off",
        ),
    )

    class BannerOpenSettingsTapped : PushNotificationAnalyticEvents(
        event = "Banner - Open Settings Tapped",
    )

    data class NotificationSettingsErrorShown(
        val toggleType: String,
        val errorType: String,
    ) : PushNotificationAnalyticEvents(
        event = "Notification Settings Error Shown",
        params = mapOf(
            "Toggle Type" to toggleType,
            AnalyticsParam.ERROR_TYPE to errorType,
        ),
    )

    data class WarningScreenShown(
        val source: AnalyticsParam.ScreensSources,
        val variant: String,
    ) : PushNotificationAnalyticEvents(
        event = "[Warning Screen] Shown",
        params = mapOf(
            WARNING_SCREEN_PARAM_VARIANT to variant,
            WARNING_SCREEN_PARAM_ZONE to source.toWarningScreenZone(),
        ),
    )

    data class WarningScreenEnableTapped(
        val source: AnalyticsParam.ScreensSources,
        val variant: String,
    ) : PushNotificationAnalyticEvents(
        event = "[Warning Screen] Enable Tapped",
        params = mapOf(
            WARNING_SCREEN_PARAM_VARIANT to variant,
            WARNING_SCREEN_PARAM_ZONE to source.toWarningScreenZone(),
        ),
    )

    data class WarningScreenSkipTapped(
        val source: AnalyticsParam.ScreensSources,
        val variant: String,
    ) : PushNotificationAnalyticEvents(
        event = "[Warning Screen] Skip Tapped",
        params = mapOf(
            WARNING_SCREEN_PARAM_VARIANT to variant,
            WARNING_SCREEN_PARAM_ZONE to source.toWarningScreenZone(),
        ),
    )
}

private const val WARNING_SCREEN_PARAM_VARIANT = "variant"
private const val WARNING_SCREEN_PARAM_ZONE = "zone"

private fun AnalyticsParam.ScreensSources.toWarningScreenZone(): String = when (this) {
    AnalyticsParam.ScreensSources.Onboarding -> "onboarding"
    AnalyticsParam.ScreensSources.Main -> "main"
    else -> value
}