package com.aiosman.ravenow.ui.index.tabs.moment.tabs.news

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.R
import com.aiosman.ravenow.entity.MomentEntity
import com.aiosman.ravenow.exp.formatPostTime2
import com.aiosman.ravenow.ui.composables.CustomAsyncImage

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FullArticleModal(
    moment: MomentEntity,
    onDismiss: () -> Unit
) {
    val appColors = LocalAppTheme.current
    val context = LocalContext.current
    val configuration = LocalConfiguration.current
    val screenHeight = configuration.screenHeightDp.dp
    // 根据屏幕高度计算最大高度，用于限制弹窗最大高度
    // 小屏幕（< 600dp）：75%，中等屏幕（600-800dp）：70%，大屏幕（> 800dp）：65%
    val maxSheetHeight = when {
        screenHeight.value < 600 -> screenHeight * 0.75f
        screenHeight.value <= 800 -> screenHeight * 0.7f
        else -> screenHeight * 0.65f
    }
    
    val sheetState = rememberModalBottomSheetState(
        skipPartiallyExpanded = true
    )
    
    // 确保弹窗从下往上展开
    androidx.compose.runtime.LaunchedEffect(Unit) {
        sheetState.expand()
    }
    
    ModalBottomSheet(
        onDismissRequest = onDismiss,
        sheetState = sheetState,
        containerColor = appColors.background,
        shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .heightIn(max = maxSheetHeight)
        ) {
            // 滚动内容
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState())
            ) {
                // 新闻图片区域 - 固定高度和宽度
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(250.dp)
                        .background(color = appColors.secondaryBackground)
                ) {
                    if (moment.images.isNotEmpty()) {
                        val firstImage = moment.images[0]
                        CustomAsyncImage(
                            context = context,
                            imageUrl = firstImage.url,
                            contentDescription = "新闻图片",
                            contentScale = ContentScale.Fit,
                            blurHash = firstImage.blurHash,
                            modifier = Modifier.fillMaxSize()
                        )
                    } else {
                        Image(
                            painter = androidx.compose.ui.res.painterResource(id = R.drawable.default_moment_img),
                            contentDescription = "默认图片",
                            contentScale = ContentScale.Fit,
                            modifier = Modifier.fillMaxSize()
                        )
                    }
                }
                
                Spacer(modifier = Modifier.height(16.dp))
                
                // 新闻标题
                Text(
                    text = if (moment.newsTitle.isNotEmpty()) moment.newsTitle else moment.nickname,
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Bold,
                    color = appColors.text,
                    lineHeight = 28.sp,
                    modifier = Modifier.padding(horizontal = 10.dp)
                )
                
                Spacer(modifier = Modifier.height(12.dp))
                
                // 新闻来源和发布时间
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 10.dp),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    // 来源按钮
                    Button(
                        onClick = {  },
                        modifier = Modifier.height(28.dp),
                        colors = ButtonDefaults.buttonColors(
                            containerColor = Color(0xFF7c68ef)
                        ),
                        contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 12.dp, vertical = 4.dp),
                        shape = RoundedCornerShape(14.dp)
                    ) {
                        Text(
                            text = if (moment.newsSource.isNotEmpty()) moment.newsSource else moment.nickname,
                            fontSize = 12.sp,
                            color = Color.White,
                        )
                    }
                    
                    // 发布时间
                    Text(
                        text = moment.time.formatPostTime2(),
                        fontSize = 12.sp,
                        color = appColors.secondaryText
                    )
                }
                
                Spacer(modifier = Modifier.height(16.dp))
                
                // 帖子内容
                NewsContent(
                    content = com.aiosman.ravenow.utils.Utils.unescapeHtml(
                        if (moment.newsContent.isNotEmpty()) moment.newsContent else (moment.momentTextContent ?: "")
                    ),
                    images = moment.images,
                    context = context
                )
                Spacer(modifier = Modifier.height(200.dp))
            }
        }
    }
}

@Composable
private fun NewsContent(
    content: String,
    images: List<com.aiosman.ravenow.entity.MomentImageEntity>,
    context: android.content.Context
) {
    val appColors = LocalAppTheme.current
    
    Column(
        modifier = Modifier.padding(horizontal = 16.dp)
    ) {
        Text(
            text = content,
            fontSize = 16.sp,
            color = appColors.text,
            lineHeight = 24.sp
        )
        
        // 图片内容
        if (images.isNotEmpty()) {
            Spacer(modifier = Modifier.height(16.dp))
            
            images.forEach { image ->
                Spacer(modifier = Modifier.height(12.dp))
                
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(8.dp))
                ) {
                    CustomAsyncImage(
                        context = context,
                        imageUrl = image.url,
                        contentDescription = "内容图片",
                        contentScale = ContentScale.Fit,
                        blurHash = image.blurHash,
                        modifier = Modifier.fillMaxWidth()
                    )
                }
            }
        }
    }
}
