package ch.threema.app.activities.referral

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.PreviewDynamicColors
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import ch.threema.android.buildActivityIntent
import ch.threema.android.buildIntent
import ch.threema.android.showToast
import ch.threema.app.R
import ch.threema.app.activities.ThreemaActivity
import ch.threema.app.compose.common.appbars.AppBar
import ch.threema.app.compose.common.appbars.NavigationIcon
import ch.threema.app.compose.common.buttons.TextButtonPrimaryOverride
import ch.threema.app.compose.common.buttons.primary.ButtonPrimaryOverride
import ch.threema.app.compose.common.spacer.DynamicSpacerSize1
import ch.threema.app.compose.common.spacer.DynamicSpacerSize3
import ch.threema.app.compose.common.spacer.SpacerHorizontal
import ch.threema.app.compose.common.spacer.SpacerVertical
import ch.threema.app.compose.common.text.ThemedText
import ch.threema.app.compose.theme.ThreemaTheme
import ch.threema.app.compose.theme.ThreemaThemePreview
import ch.threema.app.compose.theme.dimens.GridUnit
import ch.threema.app.compose.theme.dimens.responsive
import ch.threema.app.framework.EventHandler
import ch.threema.app.utils.ConfigUtils
import ch.threema.app.utils.MimeUtil
import ch.threema.app.utils.logScreenVisibility
import ch.threema.app.webviews.ReferralTosActivity
import ch.threema.base.utils.getThreemaLogger
import ch.threema.domain.types.Identity
import org.koin.androidx.viewmodel.ext.android.viewModel

private val logger = getThreemaLogger("ReferralActivity")

class ReferralActivity : ThreemaActivity() {

    init {
        logScreenVisibility(logger)
    }

    private val viewModel by viewModel<ReferralViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (!ConfigUtils.isReferralProgramEnabled()) {
            finish()
            return
        }

        setContent {
            EventHandler(viewModel, ::handleScreenEvent)

            ThreemaTheme {
                ReferralScreenContent(
                    onClickBack = {
                        finish()
                    },
                    onClickShareInvitationLink = {
                        viewModel.onClickShareInvitationLink()
                    },
                    onClickViewTos = {
                        startActivity(ReferralTosActivity.createIntent(this))
                    },
                )
            }
        }
    }

    private fun handleScreenEvent(event: ReferralScreenEvent) {
        when (event) {
            is ReferralScreenEvent.ShareInvitationLink -> {
                shareInvitationLink(ownIdentity = event.ownIdentity)
            }
            ReferralScreenEvent.Error -> {
                showToast(R.string.an_error_occurred)
            }
        }
    }

    private fun shareInvitationLink(ownIdentity: Identity) {
        val resolvedPersonalInvitationLink = getString(R.string.referral_program_invitation_link, ownIdentity)
        val invitationMessageContent = getString(R.string.referral_invitation_message_content, resolvedPersonalInvitationLink)
        val sendLinkIntent = buildIntent {
            action = Intent.ACTION_SEND
            type = MimeUtil.MIME_TYPE_TEXT
            putExtra(Intent.EXTRA_TEXT, invitationMessageContent)
        }
        val wrappedChooserIntent = Intent.createChooser(sendLinkIntent, getString(R.string.share_via))
        startActivity(wrappedChooserIntent)
    }

    companion object {
        fun createIntent(context: Context) = buildActivityIntent<ReferralActivity>(context)
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun ReferralScreenContent(
    onClickBack: () -> Unit,
    onClickShareInvitationLink: () -> Unit,
    onClickViewTos: () -> Unit,
) {
    val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())

    Scaffold(
        modifier = Modifier
            .fillMaxSize()
            .nestedScroll(scrollBehavior.nestedScrollConnection),
        contentWindowInsets = WindowInsets.safeDrawing,
        topBar = {
            AppBar(
                title = stringResource(R.string.referral_screen_title),
                navigationIcon = NavigationIcon.back(
                    onClick = onClickBack,
                ),
                scrollBehavior = scrollBehavior,
            )
        },
    ) { insetsPadding ->

        Column(
            modifier = Modifier
                .padding(insetsPadding)
                .fillMaxSize(),
        ) {
            Column(
                modifier = Modifier
                    .padding(horizontal = GridUnit.x2.responsive)
                    .weight(1f)
                    .verticalScroll(rememberScrollState()),
            ) {
                DynamicSpacerSize3()

                ThemedText(
                    text = stringResource(R.string.referral_screen_subtitle),
                    style = MaterialTheme.typography.headlineSmall,
                    fontWeight = FontWeight.Bold,
                    lineHeight = 28.sp,
                )

                SpacerVertical(GridUnit.x3)

                Surface(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(
                            shape = RoundedCornerShape(GridUnit.x1_5),
                        ),
                    color = colorResource(R.color.work_primary_fixed),
                ) {
                    Icon(
                        modifier = Modifier
                            .padding(GridUnit.x6)
                            .size(80.dp),
                        painter = painterResource(R.drawable.ic_gift),
                        tint = colorResource(R.color.work_onPrimary_fixed),
                        contentDescription = null,
                    )
                }

                SpacerVertical(GridUnit.x3)

                ThemedText(
                    text = stringResource(R.string.referral_how_it_works_title),
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.SemiBold,
                )

                SpacerVertical(GridUnit.x1)

                Surface(
                    shape = RoundedCornerShape(GridUnit.x1),
                    color = MaterialTheme.colorScheme.surfaceContainerLow,
                ) {
                    Column {
                        ExplanationItem(
                            modifier = Modifier.padding(GridUnit.x2),
                            iconRes = R.drawable.ic_person_add_rounded,
                            title = R.string.referral_how_it_works_referral_title,
                            body = R.string.referral_how_it_works_referral_body,
                        )
                        ExplanationItem(
                            modifier = Modifier.padding(GridUnit.x2),
                            iconRes = R.drawable.ic_contract,
                            title = R.string.referral_how_it_works_requirements_title,
                            body = R.string.referral_how_it_works_requirements_body,
                        )
                        ExplanationItem(
                            modifier = Modifier.padding(GridUnit.x2),
                            iconRes = R.drawable.ic_confirmation,
                            title = R.string.referral_how_it_works_confirmation_title,
                            body = R.string.referral_how_it_works_confirmation_body,
                        )
                    }
                }

                SpacerVertical(GridUnit.x3)

                ThemedText(
                    text = stringResource(R.string.referral_your_reward_title),
                    style = MaterialTheme.typography.titleMedium,
                    fontWeight = FontWeight.SemiBold,
                )

                SpacerVertical(GridUnit.x1)

                Surface(
                    shape = RoundedCornerShape(GridUnit.x1),
                    color = MaterialTheme.colorScheme.surfaceContainerLow,
                ) {
                    ExplanationItem(
                        modifier = Modifier.padding(GridUnit.x2),
                        iconRes = R.drawable.ic_gift,
                        title = R.string.referral_your_reward_subtitle,
                        body = R.string.referral_your_reward_body,
                    )
                }

                DynamicSpacerSize3()
            }

            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = GridUnit.x2.responsive),
            ) {
                DynamicSpacerSize1()

                ButtonPrimaryOverride(
                    modifier = Modifier.fillMaxWidth(),
                    text = stringResource(R.string.referral_share_invitation_link),
                    colorPrimaryOverride = colorResource(R.color.work_primary_fixed),
                    colorOnPrimaryOverride = colorResource(R.color.work_onPrimary_fixed),
                    maxLines = 1,
                    onClick = onClickShareInvitationLink,
                )

                DynamicSpacerSize1()

                TextButtonPrimaryOverride(
                    modifier = Modifier.fillMaxWidth(),
                    text = stringResource(R.string.referral_view_terms_and_conditions),
                    colorPrimaryOverride = colorResource(R.color.work_primary_fixed),
                    onClick = onClickViewTos,
                    maxLines = 1,
                )
            }
        }
    }
}

@Composable
private fun ExplanationItem(
    modifier: Modifier = Modifier,
    @DrawableRes iconRes: Int,
    @StringRes title: Int,
    @StringRes body: Int,
) {
    Column(
        modifier = modifier,
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Icon(
                modifier = Modifier.size(GridUnit.x3),
                painter = painterResource(iconRes),
                contentDescription = null,
            )

            SpacerHorizontal(GridUnit.x2)

            ThemedText(
                text = stringResource(title),
                style = MaterialTheme.typography.titleMedium,
                fontWeight = FontWeight.Medium,
            )
        }

        SpacerVertical(GridUnit.x1)

        ThemedText(
            modifier = Modifier.padding(
                start = GridUnit.x5,
            ),
            text = stringResource(body),
            style = MaterialTheme.typography.bodyMedium,
            fontWeight = FontWeight.Normal,
        )
    }
}

@PreviewLightDark
@Composable
private fun ReferralScreenContent_Preview() {
    ThreemaThemePreview {
        ReferralScreenContent(
            onClickBack = {},
            onClickShareInvitationLink = {},
            onClickViewTos = {},
        )
    }
}

@PreviewDynamicColors
@Composable
private fun ReferralScreenContent_Preview_DynamicColors() {
    ThreemaThemePreview(
        shouldUseDynamicColors = true,
    ) {
        ReferralScreenContent(
            onClickBack = {},
            onClickShareInvitationLink = {},
            onClickViewTos = {},
        )
    }
}
