package com.tangem.features.staking.impl.presentation.ui

import androidx.compose.foundation.text.ClickableText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import com.tangem.core.ui.extensions.appendColored
import com.tangem.core.ui.extensions.stringResourceSafe
import com.tangem.core.ui.res.TangemTheme
import com.tangem.core.ui.test.StakingDetailsScreenTestTags
import com.tangem.features.staking.impl.R

private const val TERMS_OF_USE_KEY = "termsOfUse"
private const val PRIVACY_POLICY_KEY = "privacyPolicy"

@Composable
internal fun StakingTosText(onTermsOfServiceClick: () -> Unit, onPrivacyPolicyClick: () -> Unit) {
    val termsOfUse = stringResourceSafe(R.string.common_terms_of_use)
    val privacyPolicy = stringResourceSafe(R.string.common_privacy_policy)
    val tosText = stringResourceSafe(R.string.staking_legal, termsOfUse, privacyPolicy)

    val clickableAnnotation = buildAnnotatedString {
        append(tosText.substringBefore(termsOfUse))

        pushStringAnnotation(TERMS_OF_USE_KEY, "")
        appendColored(termsOfUse, TangemTheme.colors.text.accent)
        pop()

        append(tosText.substringAfter(termsOfUse).substringBefore(privacyPolicy))

        pushStringAnnotation(PRIVACY_POLICY_KEY, "")
        appendColored(privacyPolicy, TangemTheme.colors.text.accent)
        pop()
    }

    ClickableText(
        text = clickableAnnotation,
        style = TangemTheme.typography.caption2.copy(
            color = TangemTheme.colors.text.tertiary,
            textAlign = TextAlign.Center,
        ),
        onClick = { offset ->
            val isTermsOfUseClicked = clickableAnnotation.getStringAnnotations(
                tag = TERMS_OF_USE_KEY,
                start = offset,
                end = offset,
            ).any()

            val isPrivacyPolicyClicked = clickableAnnotation.getStringAnnotations(
                tag = PRIVACY_POLICY_KEY,
                start = offset,
                end = offset,
            ).any()

            if (isTermsOfUseClicked) {
                onTermsOfServiceClick()
            }
            if (isPrivacyPolicyClicked) {
                onPrivacyPolicyClick()
            }
        },
        modifier = Modifier.testTag(StakingDetailsScreenTestTags.TOS_TEXT),
    )
}