package com.aiosman.riderpro

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
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.defaultMinSize
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.PlatformTextStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import androidx.paging.LoadState
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.compose.collectAsLazyPagingItems

private val DATA = (0..60).toList().map { momentTestItem }

@Composable
fun PagingBackendSample() {
    val myBackend = remember { TestBackend(DATA) }
    val pager = remember {
        Pager(
            PagingConfig(
                pageSize = myBackend.DataBatchSize,
                enablePlaceholders = true,
                maxSize = 200
            )
        ) {
            myBackend.getAllData()
        }
    }
    val lazyPagingItems = pager.flow.collectAsLazyPagingItems()

    LazyColumn {
        if (lazyPagingItems.loadState.refresh == LoadState.Loading) {
            item {
                MomentListLoading()
            }
        }
        items(count = lazyPagingItems.itemCount) { index ->
            val item = lazyPagingItems[index]
            if (item != null) {
                MomentCard(item)
            }
        }
        if (lazyPagingItems.loadState.append == LoadState.Loading) {
            item {
                MomentListLoading()
            }
        }
    }
}

@Composable
fun MomentCard(momentItem: MomentItem) {
    Column(
        modifier = Modifier.fillMaxWidth()
    ) {
        MomentTopRowGroup(momentItem = momentItem)
        MomentContentGroup(momentItem = momentItem)
        val momentOperateBtnBoxModifier = Modifier
            .fillMaxHeight()
            .weight(1f)
        ModificationListHeader()
        MomentBottomOperateRowGroup(momentOperateBtnBoxModifier)
    }
}

@Composable
fun ModificationListHeader() {
    val navController = LocalNavController.current
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .background(Color(0xFFF8F8F8))
                .padding(4.dp)
                .clickable {
                    navController.navigate("ModificationList")
                }
        ) {
            Row(verticalAlignment = Alignment.CenterVertically) {
                Box(
                    modifier = Modifier
                        .background(Color(0xFFEB4869))
                        .padding(8.dp)
                ) {
                    Icon(
                        Icons.Filled.Build,
                        contentDescription = "Modification Icon",
                        tint = Color.White, // Assuming the icon should be white
                        modifier = Modifier.size(12.dp)
                    )
                }
                Spacer(modifier = Modifier.width(12.dp))
                Text(
                    text = "Modification List",
                    color = Color(0xFF333333),
                    fontSize = 14.sp,
                    textAlign = TextAlign.Left
                )
            }
        }
    }
}

@Composable
fun MomentName(name: String) {
    Text(
        modifier = Modifier,
        textAlign = TextAlign.Start,
        text = name,
        color = Color(0f, 0f, 0f),
        fontSize = 16.sp, style = TextStyle(fontWeight = FontWeight.Bold)
    )
}

@Composable
fun MomentFollowBtn() {
    Box(
        modifier = Modifier
            .size(width = 53.dp, height = 18.dp)
            .padding(start = 8.dp),
        contentAlignment = Alignment.Center
    ) {
        Image(
            modifier = Modifier
                .fillMaxSize(),
            painter = painterResource(id = R.drawable.follow_bg),
            contentDescription = ""
        )
        Text(
            text = "Follow",
            color = Color.White,
            fontSize = 12.sp
        )
    }
}

@Composable
fun MomentPostLocation(location: String) {
    Text(
        text = location,
        color = Color(0f, 0f, 0f, 0.6f),
        fontSize = 12.sp
    )
}

@Composable
fun MomentPostTime(time: String) {
    Text(
        modifier = Modifier.padding(start = 8.dp),
        text = time, color = Color(0f, 0f, 0f, 0.6f),
        fontSize = 12.sp
    )
}

@Composable
fun MomentTopRowGroup(momentItem: MomentItem) {
    Row(
        modifier = Modifier
            .height(40.dp)
            .padding(top = 0.dp, bottom = 0.dp, start = 24.dp, end = 24.dp)
    ) {
        Image(
            painter = painterResource(id = momentItem.avatar),
            contentDescription = ""
        )
        Column(
            modifier = Modifier
                .defaultMinSize()
                .padding(start = 12.dp, end = 12.dp)
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(22.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                MomentName(momentItem.nickname)
                MomentFollowBtn()
            }
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(21.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                MomentPostLocation(momentItem.location)
                MomentPostTime(momentItem.time)
            }
        }
    }
}

@Composable
fun MomentContentGroup(momentItem: MomentItem) {
    Text(
        text = momentItem.momentTextContent,
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 22.dp, bottom = 16.dp, start = 24.dp, end = 24.dp),
        fontSize = 16.sp
    )
    Image(
        modifier = Modifier
            .fillMaxWidth(),
        painter = painterResource(id = momentItem.momentPicture),
        contentDescription = ""
    )
}

@Composable
fun MomentOperateBtn(@DrawableRes icon: Int, count: String) {
    Row(verticalAlignment = Alignment.CenterVertically) {
        Image(
            modifier = Modifier
                .size(width = 24.dp, height = 24.dp),
            painter = painterResource(id = icon),
            contentDescription = ""
        )
        Text(
            text = count,
            modifier = Modifier.padding(start = 7.dp),
            fontSize = 12.sp,
        )
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MomentBottomOperateRowGroup(modifier: Modifier) {
    var showCommentModal by remember { mutableStateOf(false) }
    if (showCommentModal) {
        ModalBottomSheet(
            onDismissRequest = { showCommentModal = false },
            containerColor = Color.White,
            sheetState = rememberModalBottomSheetState(
                skipPartiallyExpanded = true
            )
        ) {
            CommentModalContent()
        }
    }
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(56.dp)
    ) {
        Box(
            modifier = modifier,
            contentAlignment = Alignment.Center
        ) {
            MomentOperateBtn(icon = R.drawable.rider_pro_like, count = "21")
        }
        Box(
            modifier = modifier.clickable {
                showCommentModal = true
            },
            contentAlignment = Alignment.Center
        ) {
            MomentOperateBtn(icon = R.drawable.rider_pro_moment_comment, count = "43")
        }
        Box(
            modifier = modifier,
            contentAlignment = Alignment.Center
        ) {
            MomentOperateBtn(icon = R.drawable.rider_pro_share, count = "33")
        }
        Box(
            modifier = modifier,
            contentAlignment = Alignment.Center
        ) {
            MomentOperateBtn(icon = R.drawable.rider_pro_favoriate, count = "211")
        }
    }
}

@Composable
fun MomentListLoading() {
    CircularProgressIndicator(
        modifier =
        Modifier
            .fillMaxWidth()
            .wrapContentWidth(Alignment.CenterHorizontally),
        color = Color.Red
    )
}