package ch.threema.app.multidevice.wizard.steps

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.windowInsetsTopHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import ch.threema.app.R
import ch.threema.app.compose.common.buttons.primary.ButtonPrimary
import ch.threema.app.compose.common.spacer.DynamicSpacerSize1
import ch.threema.app.compose.common.spacer.DynamicSpacerSize4
import ch.threema.app.compose.common.text.ThemedText
import ch.threema.app.compose.common.text.rememberLinkifyWeb
import ch.threema.app.compose.preview.PreviewThreemaAll
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.utils.logScreenVisibility
import ch.threema.base.utils.getThreemaLogger

private val logger = getThreemaLogger("LinkNewDevicePFSInfoFragment")

class LinkNewDevicePFSInfoFragment : LinkNewDeviceFragment() {
    init {
        logScreenVisibility(logger)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        return ComposeView(requireContext()).apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent {
                ThreemaTheme {
                    LinkNewDevicePFSInfoContent(
                        modifier = Modifier.fillMaxSize(),
                        onClickLinkDevice = {
                            viewModel.switchToFragment(LinkNewDeviceScanQrFragment::class.java)
                        },
                        onClickCancel = viewModel::cancel,
                    )
                }
            }
        }
    }
}

@Composable
private fun LinkNewDevicePFSInfoContent(
    modifier: Modifier,
    onClickLinkDevice: () -> Unit,
    onClickCancel: () -> Unit,
) {
    Surface(
        modifier = modifier.nestedScroll(rememberNestedScrollInteropConnection()),
        color = MaterialTheme.colorScheme.surfaceContainerLow,
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(
                    horizontal = dimensionResource(R.dimen.grid_unit_x4),
                )
                .verticalScroll(rememberScrollState()),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Spacer(Modifier.windowInsetsTopHeight(WindowInsets.systemBars))

            DynamicSpacerSize1()

            Icon(
                modifier = Modifier.size(120.dp),
                painter = painterResource(R.drawable.ic_info_rounded),
                contentDescription = stringResource(R.string.accessibility_device_linking_pfs_warning_icon),
                tint = MaterialTheme.colorScheme.onSurface,
            )

            Spacer(Modifier.height(GridUnit.x6))

            val warningBodyRaw = stringResource(R.string.device_linking_pfs_warning_body)
            val faqEntryUrl = stringResource(R.string.device_linking_pfs_warning_faq_link_value)
            val warningBodyAnnotated = warningBodyRaw.rememberLinkifyWeb(faqEntryUrl)
            Text(
                textAlign = TextAlign.Center,
                text = warningBodyAnnotated,
            )

            Spacer(Modifier.height(GridUnit.x6))

            ButtonPrimary(
                modifier = Modifier.fillMaxWidth(),
                onClick = onClickLinkDevice,
                text = stringResource(R.string.device_linking_pfs_warning_button_continue),
                maxLines = 2,
            )

            Spacer(Modifier.height(GridUnit.x1_5))

            TextButton(
                modifier = Modifier.heightIn(min = GridUnit.x6),
                onClick = onClickCancel,
            ) {
                ThemedText(
                    modifier = Modifier.padding(horizontal = GridUnit.x2),
                    text = stringResource(R.string.device_linking_pfs_warning_button_cancel),
                    style = MaterialTheme.typography.labelLarge,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis,
                )
            }

            DynamicSpacerSize4()
        }
    }
}

@Composable
@PreviewThreemaAll
private fun LinkNewDevicePFSInfoFragmentContentPreviewFoldable() = ThreemaThemePreview {
    LinkNewDevicePFSInfoContent(
        modifier = Modifier.fillMaxSize(),
        onClickLinkDevice = {},
        onClickCancel = {},
    )
}
