package com.example.smarthome.ui

import android.content.Context
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.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.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.draw.shadow
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.smarthome.R

data class Scene(
    val id: String,
    val name: String,
    val description: String,
    val icon: Int,
    val gradient: Brush,
    var isActive: Boolean = false
)

@Composable
fun SceneScreen() {
    val context = LocalContext.current
    val prefs = remember { context.getSharedPreferences("scenes", Context.MODE_PRIVATE) }
    
    val scenes = remember {
        listOf(
            Scene("home", "回家模式", "开启灯光、空调，关闭安防", R.drawable.ic_mode_home, 
                Brush.linearGradient(listOf(Color(0xFF00D1FF), Color(0xFF00C9A7)))),
            Scene("away", "出门模式", "关闭所有设备，开启安防", R.drawable.ic_mode_away,
                Brush.linearGradient(listOf(Color(0xFFFF8A65), Color(0xFFFF7043)))),
            Scene("fun", "玩乐模式", "影音设备开启，灯光调暗", R.drawable.ic_mode_fun,
                Brush.linearGradient(listOf(Color(0xFF6A3DFF), Color(0xFF3A0CA3)))),
            Scene("sleep", "睡眠模式", "关闭灯光，空调调至舒适温度", R.drawable.ic_curtain,
                Brush.linearGradient(listOf(Color(0xFF5E35B1), Color(0xFF311B92)))),
            Scene("reading", "阅读模式", "主灯调亮，关闭电视", R.drawable.ic_light,
                Brush.linearGradient(listOf(Color(0xFFFFD54F), Color(0xFFFF8F00)))),
            Scene("movie", "观影模式", "灯光调暗，窗帘关闭", R.drawable.ic_media,
                Brush.linearGradient(listOf(Color(0xFFE91E63), Color(0xFFC2185B))))
        ).map { scene ->
            scene.copy(isActive = prefs.getBoolean(scene.id, false))
        }
    }
    
    var sceneStates by remember { mutableStateOf(scenes.associate { it.id to it.isActive }) }
    
    fun toggleScene(sceneId: String) {
        val newState = !(sceneStates[sceneId] ?: false)
        sceneStates = sceneStates.toMutableMap().apply { put(sceneId, newState) }
        prefs.edit().putBoolean(sceneId, newState).apply()
    }
    
    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 = "一键切换家居场景",
            fontSize = 14.sp,
            color = Color(0xFF9AA0A6),
            modifier = Modifier.padding(bottom = 24.dp)
        )
        
        LazyVerticalGrid(
            columns = GridCells.Adaptive(minSize = 160.dp),
            horizontalArrangement = Arrangement.spacedBy(16.dp),
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            items(scenes) { scene ->
                SceneCard(
                    scene = scene,
                    isActive = sceneStates[scene.id] ?: false,
                    onToggle = { toggleScene(scene.id) }
                )
            }
        }
    }
}

@Composable
fun SceneCard(scene: Scene, isActive: Boolean, onToggle: () -> Unit) {
    
    Box(
        modifier = Modifier
            .height(180.dp)
            .then(
                if (isActive) {
                    Modifier
                        .shadow(12.dp, RoundedCornerShape(24.dp))
                        .clip(RoundedCornerShape(24.dp))
                        .background(scene.gradient)
                } else {
                    Modifier
                        .clip(RoundedCornerShape(24.dp))
                        .background(Color(0x50121212))
                }
            )
            .clickable(
                indication = null,
                interactionSource = remember { MutableInteractionSource() }
            ) { onToggle() }
            .padding(20.dp)
    ) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.SpaceBetween
        ) {
            Box(
                modifier = Modifier
                    .size(56.dp)
                    .clip(RoundedCornerShape(16.dp))
                    .background(Color(0x33FFFFFF)),
                contentAlignment = Alignment.Center
            ) {
                Image(
                    painter = painterResource(id = scene.icon),
                    contentDescription = scene.name,
                    modifier = Modifier.size(32.dp),
                    colorFilter = androidx.compose.ui.graphics.ColorFilter.tint(Color.White)
                )
            }
            
            Column {
                Text(
                    text = scene.name,
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.White
                )
                Spacer(modifier = Modifier.height(4.dp))
                Text(
                    text = scene.description,
                    fontSize = 12.sp,
                    color = Color(0xCCFFFFFF),
                    lineHeight = 16.sp
                )
            }
        }
        
        if (isActive) {
            Box(
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .size(24.dp)
                    .clip(RoundedCornerShape(12.dp))
                    .background(Color(0x33FFFFFF)),
                contentAlignment = Alignment.Center
            ) {
                Text(text = "✓", color = Color.White, fontSize = 14.sp, fontWeight = FontWeight.Bold)
            }
        }
    }
}
