package com.example.smarthome.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.sp
import com.example.smarthome.R

data class SecurityEvent(
    val title: String,
    val time: String,
    val type: String, // "info", "warning", "alert"
    val description: String
)

@Composable
fun SecurityScreen() {
    val context = LocalContext.current
    val sharedPreferences = remember {
        context.getSharedPreferences("smart_home_prefs", android.content.Context.MODE_PRIVATE)
    }
    
    // 从SharedPreferences加载安防状态
    var isArmed by remember {
        mutableStateOf(sharedPreferences.getBoolean("security_armed", false))
    }
    
    // 保存安防状态的函数
    fun saveArmedState(armed: Boolean) {
        sharedPreferences.edit().putBoolean("security_armed", armed).apply()
        isArmed = armed
    }
    
    val events = remember {
        listOf(
            SecurityEvent("前门已锁定", "2分钟前", "info", "智能门锁自动上锁"),
            SecurityEvent("检测到异常移动", "15分钟前", "warning", "客厅摄像头检测到移动"),
            SecurityEvent("窗户已关闭", "1小时前", "info", "所有窗户传感器确认关闭"),
            SecurityEvent("烟雾警报测试", "今天 10:30", "info", "烟雾探测器自检正常"),
            SecurityEvent("门铃按下", "昨天 18:45", "info", "有访客按门铃")
        )
    }
    
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        Text(
            text = "安全中心",
            style = MaterialTheme.typography.titleLarge,
            fontWeight = FontWeight.Bold,
            color = Color.White,
            modifier = Modifier.padding(bottom = 8.dp)
        )
        
        Text(
            text = if (isArmed) "安防系统已启动" else "安防系统未启动",
            fontSize = 14.sp,
            color = if (isArmed) Color(0xFF00E676) else Color(0xFF9AA0A6),
            modifier = Modifier.padding(bottom = 24.dp)
        )
        
        LazyColumn(
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            item {
                SecurityStatusCard(isArmed = isArmed, onToggle = { saveArmedState(it) })
            }
            
            item {
                SecurityDevicesCard()
            }
            
            item {
                Text(
                    text = "最近事件",
                    fontSize = 16.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.White,
                    modifier = Modifier.padding(vertical = 8.dp)
                )
            }
            
            items(events) { event ->
                SecurityEventCard(event)
            }
        }
    }
}

@Composable
fun SecurityStatusCard(isArmed: Boolean, onToggle: (Boolean) -> Unit) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(24.dp))
            .background(
                if (isArmed) {
                    Brush.linearGradient(
                        listOf(Color(0xFF00E676), Color(0xFF00C853))
                    )
                } else {
                    Brush.linearGradient(
                        listOf(Color(0x60121212), Color(0x50121212))
                    )
                }
            )
            .padding(24.dp)
    ) {
        Column {
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically
            ) {
                Column {
                    Text(
                        text = if (isArmed) "安防已启动" else "安防未启动",
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Bold,
                        color = if (isArmed) Color.Black else Color.White
                    )
                    Spacer(modifier = Modifier.height(4.dp))
                    Text(
                        text = if (isArmed) "家庭安全受到保护" else "点击启动安防系统",
                        fontSize = 14.sp,
                        color = if (isArmed) Color(0xCC000000) else Color(0xFF9AA0A6)
                    )
                }
                
                Box(
                    modifier = Modifier
                        .size(64.dp)
                        .clip(RoundedCornerShape(16.dp))
                        .background(
                            if (isArmed) Color(0x33000000) else Color(0x33FFFFFF)
                        )
                        .clickable(
                            indication = null,
                            interactionSource = remember { MutableInteractionSource() }
                        ) { onToggle(!isArmed) },
                    contentAlignment = Alignment.Center
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.ic_security),
                        contentDescription = "安防",
                        modifier = Modifier.size(32.dp),
                        colorFilter = androidx.compose.ui.graphics.ColorFilter.tint(
                            if (isArmed) Color.Black else Color.White
                        )
                    )
                }
            }
        }
    }
}

@Composable
fun SecurityDevicesCard() {
    val devices = listOf(
        "智能门锁" to true,
        "门窗传感器" to true,
        "烟雾探测器" to true,
        "摄像头" to true,
        "燃气传感器" to true
    )
    
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(24.dp))
            .background(Color(0x50121212))
            .padding(20.dp)
    ) {
        Column {
            Text(
                text = "安防设备",
                fontSize = 16.sp,
                fontWeight = FontWeight.Bold,
                color = Color.White,
                modifier = Modifier.padding(bottom = 16.dp)
            )
            
            devices.forEach { (name, status) ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 8.dp),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = name,
                        fontSize = 14.sp,
                        color = Color.White
                    )
                    Row(
                        horizontalArrangement = Arrangement.spacedBy(8.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Box(
                            modifier = Modifier
                                .size(8.dp)
                                .clip(RoundedCornerShape(4.dp))
                                .background(if (status) Color(0xFF00E676) else Color(0xFFFF5252))
                        )
                        Text(
                            text = if (status) "正常" else "异常",
                            fontSize = 12.sp,
                            color = if (status) Color(0xFF00E676) else Color(0xFFFF5252)
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun SecurityEventCard(event: SecurityEvent) {
    val backgroundColor = when (event.type) {
        "alert" -> Color(0x33FF5252)
        "warning" -> Color(0x33FFA726)
        else -> Color(0x50121212)
    }
    
    val accentColor = when (event.type) {
        "alert" -> Color(0xFFFF5252)
        "warning" -> Color(0xFFFFA726)
        else -> Color(0xFFA9F0FF)
    }
    
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(16.dp))
            .background(backgroundColor)
            .padding(16.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(
                    text = event.title,
                    fontSize = 15.sp,
                    fontWeight = FontWeight.SemiBold,
                    color = Color.White
                )
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = event.description,
                    fontSize = 12.sp,
                    color = Color(0xFF9AA0A6)
                )
            }
            Text(
                text = event.time,
                fontSize = 11.sp,
                color = accentColor
            )
        }
    }
}
