package com.optix.android.ui.screens.settings

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.NetworkCheck
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.optix.android.ui.components.neonTextFieldColors
import com.optix.android.ui.theme.*

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NetworkSettingsSheet(
    viewModel: SettingsViewModel = hiltViewModel(),
    onDismiss: () -> Unit,
) {
    val settings by viewModel.networkSettings.collectAsState()
    var draft by remember(settings) { mutableStateOf(settings) }
    val tAccent = T.accent
    val tSurface = T.surface

    Column(
        modifier = Modifier
            .fillMaxWidth()
            .background(T.background)
            .verticalScroll(rememberScrollState()),
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .background(tSurface)
                .drawBehind {
                    drawLine(tAccent, Offset(0f, size.height), Offset(size.width, size.height), strokeWidth = 1f)
                }
                .padding(horizontal = 20.dp, vertical = 14.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(12.dp),
        ) {
            Box(
                modifier = Modifier
                    .size(32.dp)
                    .background(T.accent.copy(alpha = 0.12f), RoundedCornerShape(4.dp))
                    .border(1.dp, T.accent.copy(alpha = 0.4f), RoundedCornerShape(4.dp)),
                contentAlignment = Alignment.Center,
            ) {
                Icon(Icons.Default.NetworkCheck, null, tint = T.accent, modifier = Modifier.size(16.dp))
            }
            Column {
                Text("NETWORK SETTINGS", color = T.accent, style = MaterialTheme.typography.titleMedium)
                Text("Scan, proxy, and tool configuration", color = T.onSurfaceMid, style = MaterialTheme.typography.labelSmall)
            }
        }

        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 20.dp, vertical = 16.dp),
            verticalArrangement = Arrangement.spacedBy(14.dp),
        ) {
            SectionLabel("SCAN SETTINGS")

            OutlinedTextField(
                value = draft.defaultScanTimeout,
                onValueChange = { draft = draft.copy(defaultScanTimeout = it) },
                label = { Text("Default Scan Timeout (ms)", color = T.onSurfaceMid) },
                modifier = Modifier.fillMaxWidth(),
                colors = neonTextFieldColors(),
                singleLine = true,
                textStyle = MaterialTheme.typography.bodyMedium.copy(color = T.onSurface),
            )

            OutlinedTextField(
                value = draft.defaultPortRange,
                onValueChange = { draft = draft.copy(defaultPortRange = it) },
                label = { Text("Default Port Range", color = T.onSurfaceMid) },
                modifier = Modifier.fillMaxWidth(),
                colors = neonTextFieldColors(),
                singleLine = true,
                textStyle = MaterialTheme.typography.bodyMedium.copy(color = T.onSurface),
            )

            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Column {
                    Text("Auto-scan on open", color = T.onSurface, style = MaterialTheme.typography.bodyMedium)
                }
                Switch(
                    checked = draft.autoScanOnOpen,
                    onCheckedChange = { draft = draft.copy(autoScanOnOpen = it) },
                    colors = SwitchDefaults.colors(
                        checkedThumbColor = T.accent,
                        checkedTrackColor = T.accent.copy(alpha = 0.3f),
                        uncheckedThumbColor = T.onSurfaceMid,
                        uncheckedTrackColor = T.border
                    ),
                )
            }

            SectionLabel("PROXY / ROUTING")

            OutlinedTextField(
                value = draft.proxyHost,
                onValueChange = { draft = draft.copy(proxyHost = it) },
                label = { Text("Proxy Host", color = T.onSurfaceMid) },
                modifier = Modifier.fillMaxWidth(),
                colors = neonTextFieldColors(),
                singleLine = true,
                textStyle = MaterialTheme.typography.bodyMedium.copy(color = T.onSurface),
            )

            Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
                Button(
                    onClick = { viewModel.saveNetworkSettings(draft); onDismiss() },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = T.accent,
                        contentColor = if (T.isDark) Color.Black else Color.White
                    ),
                    shape = RoundedCornerShape(3.dp),
                    modifier = Modifier.weight(1f),
                ) { Text("SAVE") }
                Button(
                    onClick = onDismiss,
                    colors = ButtonDefaults.buttonColors(containerColor = T.surfaceVar, contentColor = T.onSurface),
                    shape = RoundedCornerShape(3.dp),
                    modifier = Modifier.weight(1f),
                ) { Text("CANCEL") }
            }

            Spacer(Modifier.height(8.dp))
        }
    }
}

@Composable
private fun SectionLabel(text: String) {
    Text(
        text,
        color = T.accent.copy(alpha = 0.7f),
        style = MaterialTheme.typography.labelSmall,
        modifier = Modifier.padding(top = 4.dp),
    )
    HorizontalDivider(color = T.border, thickness = 0.5.dp)
}
