package com.wisp.app.ui.screen

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Visibility
import androidx.compose.material.icons.outlined.VisibilityOff
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Outline
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlin.math.abs
import kotlin.math.cos
import kotlin.math.pow
import kotlin.math.sign
import kotlin.math.sin
import androidx.compose.ui.res.stringResource
import com.wisp.app.R
import com.wisp.app.nostr.RemoteSignerBridge
import com.wisp.app.nostr.toHex
import com.wisp.app.relay.TorStatus
import com.wisp.app.viewmodel.AuthViewModel

@Composable
fun AuthScreen(
    viewModel: AuthViewModel,
    isTorEnabled: Boolean = false,
    torStatus: TorStatus = TorStatus.DISABLED,
    onToggleTor: (Boolean) -> Unit = {},
    showSignUp: Boolean = true,
    onAuthenticated: (isNewAccount: Boolean) -> Unit
) {
    val context = LocalContext.current
    val nsecInput by viewModel.nsecInput.collectAsState()
    val error by viewModel.error.collectAsState()
    var nsecVisible by remember { mutableStateOf(false) }
    val signerAvailable = remember { RemoteSignerBridge.isSignerAvailable(context) }

    // Track when signer login completes so we can navigate after the composable
    // is back to RESUMED state (activity result callbacks fire during STARTED,
    // which causes navigateSafe() to silently drop the navigation call).
    var signerLoginComplete by remember { mutableStateOf(false) }
    if (signerLoginComplete) {
        LaunchedEffect(Unit) {
            signerLoginComplete = false
            onAuthenticated(false)
        }
    }

    val signerLauncher = rememberLauncherForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        val data = result.data ?: return@rememberLauncherForActivityResult
        val result = data.getStringExtra("result") ?: return@rememberLauncherForActivityResult
        val pkg = data.getStringExtra("package")
        // Amber returns npub bech32 — decode to hex
        val pubkeyHex = if (result.startsWith("npub1")) {
            try { com.wisp.app.nostr.Nip19.npubDecode(result).toHex() } catch (_: Exception) { return@rememberLauncherForActivityResult }
        } else {
            result
        }
        viewModel.loginWithSigner(pubkeyHex, pkg)
        signerLoginComplete = true
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(32.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        Icon(
            painter = painterResource(R.drawable.ic_wisp_logo),
            contentDescription = stringResource(R.string.onboarding_wisp_logo),
            tint = androidx.compose.ui.graphics.Color.Unspecified,
            modifier = Modifier
                .size(108.dp)
                .drawBehind {
                    drawCircle(
                        brush = androidx.compose.ui.graphics.Brush.radialGradient(
                            colors = listOf(
                                androidx.compose.ui.graphics.Color.Black,
                                androidx.compose.ui.graphics.Color.Black.copy(alpha = 0.6f),
                                androidx.compose.ui.graphics.Color.Transparent
                            ),
                            radius = size.minDimension * 0.65f
                        ),
                        radius = size.minDimension * 0.65f
                    )
                }
        )
        Text(
            text = stringResource(R.string.auth_wisp),
            style = MaterialTheme.typography.titleLarge.copy(
                fontFamily = FontFamily.SansSerif,
                fontSize = 36.sp,
                fontWeight = FontWeight.W500
            ),
            color = MaterialTheme.colorScheme.primary
        )
        Spacer(Modifier.height(4.dp))
        Text(
            text = stringResource(R.string.auth_tagline),
            style = MaterialTheme.typography.bodyMedium,
            color = MaterialTheme.colorScheme.onSurfaceVariant
        )

        Spacer(Modifier.height(16.dp))

        val pillBorderColor = when (torStatus) {
            TorStatus.CONNECTED -> MaterialTheme.colorScheme.primary
            TorStatus.ERROR -> MaterialTheme.colorScheme.error
            else -> MaterialTheme.colorScheme.outlineVariant
        }
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center,
            modifier = Modifier
                .border(1.dp, pillBorderColor, RoundedCornerShape(24.dp))
                .clickable { onToggleTor(!isTorEnabled) }
                .padding(horizontal = 16.dp, vertical = 8.dp)
        ) {
            Box(contentAlignment = Alignment.Center, modifier = Modifier.size(20.dp)) {
                if (torStatus == TorStatus.STARTING) {
                    CircularProgressIndicator(
                        modifier = Modifier.size(18.dp),
                        strokeWidth = 2.dp,
                        color = MaterialTheme.colorScheme.primary
                    )
                } else {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_tor_onion),
                        contentDescription = stringResource(R.string.cd_toggle_tor),
                        modifier = Modifier.size(18.dp),
                        tint = when (torStatus) {
                            TorStatus.CONNECTED -> MaterialTheme.colorScheme.primary
                            TorStatus.ERROR -> MaterialTheme.colorScheme.error
                            else -> MaterialTheme.colorScheme.onSurfaceVariant
                        }
                    )
                }
            }
            Spacer(Modifier.width(8.dp))
            Text(
                text = when (torStatus) {
                    TorStatus.STARTING -> stringResource(R.string.auth_connecting_to_tor)
                    TorStatus.CONNECTED -> stringResource(R.string.auth_connected_via_tor)
                    TorStatus.ERROR -> stringResource(R.string.auth_tor_error)
                    else -> stringResource(R.string.auth_connect_via_tor)
                },
                style = MaterialTheme.typography.labelMedium,
                color = when (torStatus) {
                    TorStatus.CONNECTED -> MaterialTheme.colorScheme.primary
                    TorStatus.ERROR -> MaterialTheme.colorScheme.error
                    else -> MaterialTheme.colorScheme.onSurfaceVariant
                }
            )
        }

        Spacer(Modifier.height(16.dp))

        if (showSignUp) {
            Button(
                onClick = {
                    if (viewModel.signUp()) onAuthenticated(true)
                },
                modifier = Modifier.fillMaxWidth()
            ) {
                Text(stringResource(R.string.auth_sign_up))
            }

            Spacer(Modifier.height(24.dp))
            HorizontalDivider(color = MaterialTheme.colorScheme.outline)
            Spacer(Modifier.height(24.dp))

            Text(
                text = stringResource(R.string.auth_or_log_in_with_key),
                style = MaterialTheme.typography.bodyMedium,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
            Spacer(Modifier.height(12.dp))
        } else {
            Spacer(Modifier.height(12.dp))
        }


        OutlinedTextField(
            value = nsecInput,
            onValueChange = { viewModel.updateNsecInput(it) },
            label = { Text(stringResource(R.string.auth_nsec_or_npub)) },
            singleLine = true,
            visualTransformation = if (nsecVisible) VisualTransformation.None else PasswordVisualTransformation(),
            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
            trailingIcon = {
                IconButton(onClick = { nsecVisible = !nsecVisible }) {
                    Icon(
                        imageVector = if (nsecVisible) Icons.Outlined.VisibilityOff else Icons.Outlined.Visibility,
                        contentDescription = if (nsecVisible) stringResource(R.string.auth_hide_key) else stringResource(R.string.auth_show_key)
                    )
                }
            },
            modifier = Modifier.fillMaxWidth()
        )

        Spacer(Modifier.height(12.dp))

        OutlinedButton(
            onClick = {
                if (viewModel.logIn()) onAuthenticated(false)
            },
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(stringResource(R.string.auth_log_in))
        }

        if (signerAvailable) {
            Spacer(Modifier.height(24.dp))
            HorizontalDivider(color = MaterialTheme.colorScheme.outline)
            Spacer(Modifier.height(24.dp))

            OutlinedButton(
                onClick = {
                    val permissions = """[{"type":"sign_event","kind":0},{"type":"sign_event","kind":1},{"type":"sign_event","kind":3},{"type":"sign_event","kind":5},{"type":"sign_event","kind":6},{"type":"sign_event","kind":7},{"type":"sign_event","kind":13},{"type":"sign_event","kind":9734},{"type":"sign_event","kind":10000},{"type":"sign_event","kind":10001},{"type":"sign_event","kind":10002},{"type":"sign_event","kind":10030},{"type":"sign_event","kind":10063},{"type":"sign_event","kind":22242},{"type":"sign_event","kind":24242},{"type":"sign_event","kind":30000},{"type":"sign_event","kind":30003},{"type":"sign_event","kind":30030},{"type":"nip44_encrypt"},{"type":"nip44_decrypt"}]"""
                    val intent = RemoteSignerBridge.buildGetPublicKeyIntent(permissions)
                    signerLauncher.launch(intent)
                },
                modifier = Modifier.fillMaxWidth()
            ) {
                Text(stringResource(R.string.auth_login_with_signer))
            }
        }

        error?.let {
            Spacer(Modifier.height(16.dp))
            Text(
                text = it,
                color = MaterialTheme.colorScheme.error,
                style = MaterialTheme.typography.bodySmall
            )
        }
    }
}

/** Superellipse-based squircle shape (n≈5) for smooth continuous-curvature corners. */
private val SquircleShape = object : Shape {
    override fun createOutline(size: Size, layoutDirection: LayoutDirection, density: Density): Outline {
        val path = Path()
        val w = size.width
        val h = size.height
        val n = 5.0 // superellipse exponent — higher = more square, lower = more round
        val steps = 360
        for (i in 0 until steps) {
            val angle = 2.0 * Math.PI * i / steps
            val cosA = cos(angle)
            val sinA = sin(angle)
            val x = (w / 2) * abs(cosA).pow(2.0 / n) * sign(cosA) + w / 2
            val y = (h / 2) * abs(sinA).pow(2.0 / n) * sign(sinA) + h / 2
            if (i == 0) path.moveTo(x.toFloat(), y.toFloat())
            else path.lineTo(x.toFloat(), y.toFloat())
        }
        path.close()
        return Outline.Generic(path)
    }
}
