package com.tangem.features.disclaimer.impl.model

import com.tangem.common.routing.AppRoute
import com.tangem.common.ui.notifications.NotificationId
import com.tangem.core.decompose.di.ModelScoped
import com.tangem.core.decompose.model.Model
import com.tangem.core.decompose.model.ParamsContainer
import com.tangem.core.decompose.navigation.Router
import com.tangem.core.navigation.finisher.AppFinisher
import com.tangem.domain.card.repository.CardRepository
import com.tangem.domain.notifications.repository.NotificationsRepository
import com.tangem.domain.settings.NeverRequestPermissionUseCase
import com.tangem.domain.settings.NeverToInitiallyAskPermissionUseCase
import com.tangem.features.disclaimer.api.components.DisclaimerComponent
import com.tangem.features.disclaimer.impl.entity.DisclaimerUM
import com.tangem.features.pushnotifications.api.utils.PUSH_PERMISSION
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@ModelScoped
@Suppress("LongParameterList")
internal class DisclaimerModel @Inject constructor(
    override val dispatchers: CoroutineDispatcherProvider,
    private val cardRepository: CardRepository,
    private val router: Router,
    private val neverToInitiallyAskPermissionUseCase: NeverToInitiallyAskPermissionUseCase,
    private val neverRequestPermissionUseCase: NeverRequestPermissionUseCase,
    private val appFinisher: AppFinisher,
    private val notificationsRepository: NotificationsRepository,
    paramsContainer: ParamsContainer,
) : Model() {

    private val params: DisclaimerComponent.Params = paramsContainer.require()

    val state: MutableStateFlow<DisclaimerUM> = MutableStateFlow(
        DisclaimerUM(
            onAccept = ::onAccept,
            url = DISCLAIMER_URL,
            isTosAccepted = params.isTosAccepted,
            popBack = ::popBack,
        ),
    )

    private fun onAccept() = modelScope.launch {
        if (params.isTosAccepted) {
            router.pop()
        } else {
            cardRepository.acceptTangemTOS()
            val nextRoute = params.nextRoute
            if (nextRoute != null) {
                router.replaceAll(nextRoute)
                return@launch
            }
            val shouldAskPushPermission = notificationsRepository.shouldShowSubscribeOnNotificationsAfterUpdate()
            if (shouldAskPushPermission) {
                notificationsRepository.setShouldShowNotifications(
                    key = NotificationId.EnablePushesReminderNotification.key,
                    value = false,
                )
                router.push(AppRoute.PushNotification(AppRoute.PushNotification.Source.Stories))
            } else {
                neverToInitiallyAskPermissionUseCase(PUSH_PERMISSION)
                neverRequestPermissionUseCase(PUSH_PERMISSION)
                router.replaceAll(AppRoute.Home())
            }
        }
    }

    private fun popBack() {
        if (params.isTosAccepted) {
            router.pop()
        } else {
            appFinisher.finish()
        }
    }

    private companion object {
        const val DISCLAIMER_URL = "https://tangem.com/tangem_tos.html"
    }
}