package com.example.smarthome.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
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.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun StatisticsScreen() {
    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)
        )
        
        LazyColumn(
            verticalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            item { OverallStatsCard() }
            item { RoomStatsCard() }
            item { DeviceRankingCard() }
        }
    }
}

@Composable
fun OverallStatsCard() {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .clip(RoundedCornerShape(24.dp))
            .background(
                Brush.linearGradient(
                    colors = listOf(
                        Color(0x60121212),
                        Color(0x50121212)
                    )
                )
            )
            .border(
                width = 1.dp,
                brush = Brush.linearGradient(
                    colors = listOf(
                        Color(0x22FFFFFF),
                        Color(0x0AFFFFFF)
                    )
                ),
                shape = RoundedCornerShape(24.dp)
            )
            .padding(20.dp)
    ) {
        Column {
            Text(
                text = "本月总览",
                fontSize = 16.sp,
                fontWeight = FontWeight.Bold,
                color = Color.White,
                modifier = Modifier.padding(bottom = 16.dp)
            )
            
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                StatItem(
                    label = "总消耗",
                    value = "342.5",
                    unit = "kWh",
                    color = Color(0xFFA9F0FF),
                    modifier = Modifier.weight(1f)
                )
                StatItem(
                    label = "总费用",
                    value = "¥205",
                    unit = "",
                    color = Color(0xFFB89CFF),
                    modifier = Modifier.weight(1f)
                )
                StatItem(
                    label = "运行时长",
                    value = "720",
                    unit = "小时",
                    color = Color(0xFF00E676),
                    modifier = Modifier.weight(1f)
                )
            }
        }
    }
}

@Composable
fun StatItem(label: String, value: String, unit: String, color: Color, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Row(
            verticalAlignment = Alignment.Bottom,
            horizontalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            Text(
                text = value,
                fontSize = 24.sp,
                fontWeight = FontWeight.Bold,
                color = color
            )
            if (unit.isNotEmpty()) {
                Text(
                    text = unit,
                    fontSize = 12.sp,
                    color = Color(0xFF9AA0A6),
                    modifier = Modifier.padding(bottom = 4.dp)
                )
            }
        }
        Spacer(modifier = Modifier.height(4.dp))
        Text(
            text = label,
            fontSize = 12.sp,
            color = Color(0xFF9AA0A6)
        )
    }
}

@Composable
fun RoomStatsCard() {
    val rooms = listOf(
        Triple("客厅", 85.2f, 0.35f),
        Triple("卧室", 62.8f, 0.26f),
        Triple("厨房", 48.5f, 0.20f),
        Triple("影音室", 32.1f, 0.13f),
        Triple("游戏房", 15.9f, 0.06f)
    )
    
    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)
            )
            
            rooms.forEach { (name, value, percentage) ->
                RoomStatItem(name, value, percentage)
                Spacer(modifier = Modifier.height(12.dp))
            }
        }
    }
}

@Composable
fun RoomStatItem(name: String, value: Float, percentage: Float) {
    Column {
        Row(
            modifier = Modifier.fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = name,
                fontSize = 14.sp,
                color = Color.White
            )
            Text(
                text = "${value} kWh",
                fontSize = 14.sp,
                fontWeight = FontWeight.Medium,
                color = Color(0xFFA9F0FF)
            )
        }
        Spacer(modifier = Modifier.height(6.dp))
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(6.dp)
                .clip(RoundedCornerShape(3.dp))
                .background(Color(0x33FFFFFF))
        ) {
            Box(
                modifier = Modifier
                    .fillMaxWidth(percentage)
                    .fillMaxHeight()
                    .background(
                        Brush.horizontalGradient(
                            listOf(Color(0xFFA9F0FF), Color(0xFFB89CFF))
                        )
                    )
            )
        }
    }
}

@Composable
fun DeviceRankingCard() {
    val devices = listOf(
        "空调" to "156.3 kWh",
        "热水器" to "78.5 kWh",
        "冰箱" to "45.2 kWh",
        "洗衣机" to "32.8 kWh",
        "电视" to "29.7 kWh"
    )
    
    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.forEachIndexed { index, (name, value) ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 8.dp),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Row(
                        horizontalArrangement = Arrangement.spacedBy(12.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Box(
                            modifier = Modifier
                                .size(32.dp)
                                .clip(RoundedCornerShape(8.dp))
                                .background(
                                    when (index) {
                                        0 -> Brush.linearGradient(listOf(Color(0xFFFFD700), Color(0xFFFFA500)))
                                        1 -> Brush.linearGradient(listOf(Color(0xFFC0C0C0), Color(0xFF808080)))
                                        2 -> Brush.linearGradient(listOf(Color(0xFFCD7F32), Color(0xFF8B4513)))
                                        else -> Brush.linearGradient(listOf(Color(0x33FFFFFF), Color(0x33FFFFFF)))
                                    }
                                ),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(
                                text = "${index + 1}",
                                fontSize = 14.sp,
                                fontWeight = FontWeight.Bold,
                                color = Color.White
                            )
                        }
                        Text(
                            text = name,
                            fontSize = 14.sp,
                            color = Color.White
                        )
                    }
                    Text(
                        text = value,
                        fontSize = 14.sp,
                        fontWeight = FontWeight.Medium,
                        color = Color(0xFF9AA0A6)
                    )
                }
            }
        }
    }
}
