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

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
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.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.aiosman.ravenow.AppStore
import com.aiosman.ravenow.LocalAppTheme
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.R
import com.aiosman.ravenow.ui.NavigationRoute
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.dynamic.Dynamic
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.expolre.Explore
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.hot.HotMomentsList
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.timeline.TimelineMomentsList
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.news.NewsScreen
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.recommend.RecommendScreen
import com.aiosman.ravenow.ui.index.tabs.search.SearchViewModel
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.positionInParent
import com.aiosman.ravenow.ui.composables.TabItem
import com.aiosman.ravenow.ui.composables.UnderlineTabItem
import com.aiosman.ravenow.ui.composables.rememberDebouncer
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.shorts.ShortVideoScreen

/**
 * 动态列表
 */
@OptIn(ExperimentalMaterialApi::class, ExperimentalFoundationApi::class)
@Composable
fun MomentsList(
    isPageVisible: Boolean = true
) {
    val AppColors = LocalAppTheme.current
    val navController = LocalNavController.current
    val navigationBarPaddings =
        WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding() + 48.dp
    val statusBarPaddingValues = WindowInsets.systemBars.asPaddingValues()
    // 根据登录状态设置标签页数量：游客模式5个tab，非游客模式6个tab
    val tabCount = if (AppStore.isGuest) 5 else 6
    var pagerState = rememberPagerState { tabCount }
    var scope = rememberCoroutineScope()
    val scrollState = rememberScrollState()
    val density = LocalDensity.current
    // 存储每个标签的位置和宽度信息 (页面索引, 位置, 宽度)
    val tabPositions = remember { mutableStateOf<List<Triple<Int, Float, Float>>>(emptyList()) }
    // 存储容器宽度
    val containerWidth = remember { mutableStateOf(0f) }
    // 记录上一次的页面索引
    val previousPage = remember { mutableStateOf(pagerState.currentPage) }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(
                top = statusBarPaddingValues.calculateTopPadding(),
                bottom = navigationBarPaddings
            ),
        horizontalAlignment = Alignment.CenterHorizontally,
    ) {
        // 顶部区域：可滚动的标签页 + 搜索按钮
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(54.dp)
                .padding(horizontal = 16.dp),
            horizontalArrangement = Arrangement.SpaceBetween,
            verticalAlignment = Alignment.CenterVertically
        ) {
            // 可滚动的标签页行
            Box(
                modifier = Modifier
                    .weight(1f)
                    .onGloballyPositioned { coordinates ->
                        containerWidth.value = with(density) { coordinates.size.width.toDp().toPx() }
                    }
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .horizontalScroll(scrollState),
                    horizontalArrangement = Arrangement.Start,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    val tabDebouncer = rememberDebouncer()
                    
                    // 推荐标签
                    Box(
                        modifier = Modifier.onGloballyPositioned { coordinates ->
                            val position = with(density) { coordinates.positionInParent().x.toDp().toPx() }
                            val width = with(density) { coordinates.size.width.toDp().toPx() }
                            val currentPositions = tabPositions.value.toMutableList()
                            val existingIndex = currentPositions.indexOfFirst { it.first == 0 }
                            if (existingIndex == -1) {
                                currentPositions.add(Triple(0, position, width))
                                tabPositions.value = currentPositions.sortedBy { it.first }
                            } else {
                                currentPositions[existingIndex] = Triple(0, position, width)
                                tabPositions.value = currentPositions
                            }
                        }
                    ) {
                        UnderlineTabItem(
                            text = stringResource(R.string.tab_recommend),
                            isSelected = pagerState.currentPage == 0,
                            onClick = {
                                tabDebouncer {
                                    scope.launch {
                                        pagerState.animateScrollToPage(0)
                                    }
                                }
                            }
                        )
                    }

                    
                    // 短视频标签
                    Box(
                        modifier = Modifier.onGloballyPositioned { coordinates ->
                            val position = with(density) { coordinates.positionInParent().x.toDp().toPx() }
                            val width = with(density) { coordinates.size.width.toDp().toPx() }
                            val currentPositions = tabPositions.value.toMutableList()
                            val existingIndex = currentPositions.indexOfFirst { it.first == 1 }
                            if (existingIndex == -1) {
                                currentPositions.add(Triple(1, position, width))
                                tabPositions.value = currentPositions.sortedBy { it.first }
                            } else {
                                currentPositions[existingIndex] = Triple(1, position, width)
                                tabPositions.value = currentPositions
                            }
                        }
                    ) {
                        UnderlineTabItem(
                            text = stringResource(R.string.tab_short_video),
                            isSelected = pagerState.currentPage == 1,
                            onClick = {
                                tabDebouncer {
                                    scope.launch {
                                        pagerState.animateScrollToPage(1)
                                    }
                                }
                            }
                        )
                    }

                    
                    // 动态标签
                    Box(
                        modifier = Modifier.onGloballyPositioned { coordinates ->
                            val position = with(density) { coordinates.positionInParent().x.toDp().toPx() }
                            val width = with(density) { coordinates.size.width.toDp().toPx() }
                            val currentPositions = tabPositions.value.toMutableList()
                            val existingIndex = currentPositions.indexOfFirst { it.first == 2 }
                            if (existingIndex == -1) {
                                currentPositions.add(Triple(2, position, width))
                                tabPositions.value = currentPositions.sortedBy { it.first }
                            } else {
                                currentPositions[existingIndex] = Triple(2, position, width)
                                tabPositions.value = currentPositions
                            }
                        }
                    ) {
                        UnderlineTabItem(
                            text = stringResource(R.string.moment),
                            isSelected = pagerState.currentPage == 2,
                            onClick = {
                                tabDebouncer {
                                    scope.launch {
                                        // 如果当前在动态标签右边的标签页，立即向左滚动显示推荐标签
                                        if (pagerState.currentPage > 2 && containerWidth.value > 0) {
                                            val recommendTab = tabPositions.value.find { it.first == 0 }
                                            if (recommendTab != null) {
                                                val (_, recommendPosition, _) = recommendTab
                                                val currentScroll = scrollState.value.toFloat()
                                                val scrollToPosition = recommendPosition.coerceAtLeast(0f)
                                                if (kotlin.math.abs(scrollToPosition - currentScroll) > 1f) {
                                                    scrollState.animateScrollTo(scrollToPosition.toInt())
                                                }
                                            }
                                        }
                                        pagerState.animateScrollToPage(2)
                                    }
                                }
                            }
                        )
                    }

                    
                    // 只有非游客用户才显示"关注"tab
                    if (!AppStore.isGuest) {
                        Box(
                            modifier = Modifier.onGloballyPositioned { coordinates ->
                                val position = with(density) { coordinates.positionInParent().x.toDp().toPx() }
                                val width = with(density) { coordinates.size.width.toDp().toPx() }
                                val currentPositions = tabPositions.value.toMutableList()
                                val existingIndex = currentPositions.indexOfFirst { it.first == 3 }
                                if (existingIndex == -1) {
                                    currentPositions.add(Triple(3, position, width))
                                    tabPositions.value = currentPositions.sortedBy { it.first }
                                } else {
                                    currentPositions[existingIndex] = Triple(3, position, width)
                                    tabPositions.value = currentPositions
                                }
                            }
                        ) {
                            UnderlineTabItem(
                                text = stringResource(R.string.index_following),
                                isSelected = pagerState.currentPage == 3,
                                onClick = {
                                    tabDebouncer {
                                        scope.launch {
                                            // 如果当前在关注标签左边的标签页，立即向右滚动显示新闻标签
                                            if (pagerState.currentPage < 3 && containerWidth.value > 0) {
                                                val newsTab = tabPositions.value.find { it.first == 5 }
                                                if (newsTab != null) {
                                                    val (_, newsPosition, newsWidth) = newsTab
                                                    val currentScroll = scrollState.value.toFloat()
                                                    val scrollToPosition = (newsPosition + newsWidth - containerWidth.value).coerceAtLeast(0f)
                                                    if (kotlin.math.abs(scrollToPosition - currentScroll) > 1f) {
                                                        scrollState.animateScrollTo(scrollToPosition.toInt())
                                                    }
                                                }
                                            }
                                            pagerState.animateScrollToPage(3)
                                        }
                                    }
                                }
                            )
                        }

                        
                        // 热门标签
                        Box(
                            modifier = Modifier.onGloballyPositioned { coordinates ->
                                val position = with(density) { coordinates.positionInParent().x.toDp().toPx() }
                                val width = with(density) { coordinates.size.width.toDp().toPx() }
                                val currentPositions = tabPositions.value.toMutableList()
                                val existingIndex = currentPositions.indexOfFirst { it.first == 4 }
                                if (existingIndex == -1) {
                                    currentPositions.add(Triple(4, position, width))
                                    tabPositions.value = currentPositions.sortedBy { it.first }
                                } else {
                                    currentPositions[existingIndex] = Triple(4, position, width)
                                    tabPositions.value = currentPositions
                                }
                            }
                        ) {
                            UnderlineTabItem(
                                text = stringResource(R.string.index_hot),
                                isSelected = pagerState.currentPage == 4,
                                onClick = {
                                    tabDebouncer {
                                        scope.launch {
                                            pagerState.animateScrollToPage(4)
                                        }
                                    }
                                }
                            )
                        }
                    } else {
                        // 热门标签 (游客模式) - 在游客模式下，热门标签对应第3页
                        Box(
                            modifier = Modifier.onGloballyPositioned { coordinates ->
                                val position = with(density) { coordinates.positionInParent().x.toDp().toPx() }
                                val width = with(density) { coordinates.size.width.toDp().toPx() }
                                val currentPositions = tabPositions.value.toMutableList()
                                val existingIndex = currentPositions.indexOfFirst { it.first == 3 }
                                if (existingIndex == -1) {
                                    currentPositions.add(Triple(3, position, width))
                                    tabPositions.value = currentPositions.sortedBy { it.first }
                                } else {
                                    currentPositions[existingIndex] = Triple(3, position, width)
                                    tabPositions.value = currentPositions
                                }
                            }
                        ) {
                            UnderlineTabItem(
                                text = stringResource(R.string.index_hot),
                                isSelected = pagerState.currentPage == 3,
                                onClick = {
                                    tabDebouncer {
                                        scope.launch {
                                            pagerState.animateScrollToPage(3)
                                        }
                                    }
                                }
                            )
                        }
                    }


                    // 新闻标签 - 在游客模式下对应第4页，非游客模式下对应第5页
                    val newsPageIndex = if (AppStore.isGuest) 4 else 5
                    Box(
                        modifier = Modifier.onGloballyPositioned { coordinates ->
                            val position = with(density) { coordinates.positionInParent().x.toDp().toPx() }
                            val width = with(density) { coordinates.size.width.toDp().toPx() }
                            val currentPositions = tabPositions.value.toMutableList()
                            val existingIndex = currentPositions.indexOfFirst { it.first == newsPageIndex }
                            if (existingIndex == -1) {
                                currentPositions.add(Triple(newsPageIndex, position, width))
                                tabPositions.value = currentPositions.sortedBy { it.first }
                            } else {
                                currentPositions[existingIndex] = Triple(newsPageIndex, position, width)
                                tabPositions.value = currentPositions
                            }
                        }
                    ) {
                        UnderlineTabItem(
                            text = stringResource(R.string.tab_news),
                            isSelected = pagerState.currentPage == newsPageIndex,
                            onClick = {
                                tabDebouncer {
                                    scope.launch {
                                        pagerState.animateScrollToPage(newsPageIndex)
                                    }
                                }
                            }
                        )
                    }
                }
            }
            
            // 搜索按钮
            val lastClickTime = remember { mutableStateOf(0L) }
            val clickDelay = 500L
            Image(
                painter = painterResource(id = R.drawable.rider_pro_nav_search),
                contentDescription = "search",
                modifier = Modifier
                    .size(24.dp)
                    .noRippleClickable {
                        val currentTime = System.currentTimeMillis()
                        if (currentTime - lastClickTime.value > clickDelay) {
                            lastClickTime.value = currentTime
                            navController.navigate(NavigationRoute.Search.route)
                        }
                    },
                colorFilter = ColorFilter.tint(AppColors.text)
            )
        }
        
        // 监听页面变化，实现自动滚动
        LaunchedEffect(pagerState.currentPage, pagerState.currentPageOffsetFraction) {
            val currentPage = pagerState.currentPage
            val offsetFraction = pagerState.currentPageOffsetFraction
            val prevPage = previousPage.value
            
            // 只在页面切换接近完成时执行（避免滑动过程中频繁触发）
            val absOffset = kotlin.math.abs(offsetFraction)
            if (absOffset < 0.1f && containerWidth.value > 0) {
                // 情况1：从关注标签页左边的标签页（0,1,2）滑动到关注标签页（3）时，向右滚动显示新闻标签（5）
                if (currentPage == 3 && prevPage < 3 && !AppStore.isGuest) {
                    val newsTab = tabPositions.value.find { it.first == 5 }
                    if (newsTab != null) {
                        val (_, newsPosition, newsWidth) = newsTab
                        val currentScroll = scrollState.value.toFloat()
                        // 计算滚动位置，使新闻标签可见（确保新闻标签的结束位置在可见区域内）
                        val scrollToPosition = (newsPosition + newsWidth - containerWidth.value).coerceAtLeast(0f)
                        if (kotlin.math.abs(scrollToPosition - currentScroll) > 1f) {
                            scrollState.animateScrollTo(scrollToPosition.toInt())
                        }
                    }
                }
                
                // 情况2：从动态标签页右边的标签页（3,4,5）滑动到动态标签页（2）时，向左滚动显示推荐标签（0）
                if (currentPage == 2 && prevPage > 2) {
                    val recommendTab = tabPositions.value.find { it.first == 0 }
                    if (recommendTab != null) {
                        val (_, recommendPosition, _) = recommendTab
                        val currentScroll = scrollState.value.toFloat()
                        // 计算滚动位置，使推荐标签可见（确保推荐标签的起始位置在可见区域内）
                        val scrollToPosition = recommendPosition.coerceAtLeast(0f)
                        if (kotlin.math.abs(scrollToPosition - currentScroll) > 1f) {
                            scrollState.animateScrollTo(scrollToPosition.toInt())
                        }
                    }
                }
                
                // 更新上一次的页面索引
                if (currentPage != prevPage) {
                    previousPage.value = currentPage
                }
            }
        }

        HorizontalPager(
            state = pagerState,
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
        ) {
            when (it) {
                0 -> {
                    // 推荐页面
                    RecommendScreen()
                }
                1 -> {
                    // 短视频页面
                    ShortVideoScreen(isPageVisible = pagerState.currentPage == 1 && isPageVisible)
                }
                2 -> {
                    // 动态页面 - 暂时显示时间线内容
                    Dynamic()
                }
                3 -> {
                    // 关注页面 (仅非游客用户) 或 热门页面 (游客用户)
                    if (AppStore.isGuest) {
                        HotMomentsList()
                    } else {
                        TimelineMomentsList()
                    }
                }
                4 -> {
                    // 热门页面 (仅非游客用户) 或 新闻页面 (游客用户)
                    if (AppStore.isGuest) {
                        NewsScreen()
                    } else {
                        HotMomentsList()
                    }
                }
                5 -> {
                    // 新闻页面 (仅非游客用户)
                    if (!AppStore.isGuest) {
                        NewsScreen()
                    }
                }
            }
        }
    }
}
@Composable
fun CustomTabItem(
    text: String,
    isSelected: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    val AppColors = LocalAppTheme.current

    Column(
        modifier = modifier
            .noRippleClickable { onClick() },
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = text,
            fontSize = 15.sp,
            color = if (isSelected) AppColors.tabSelectedText else AppColors.tabUnselectedText,
            modifier = Modifier
                .clip(RoundedCornerShape(12.dp))
                .background(if (isSelected) AppColors.tabSelectedBackground else AppColors.tabUnselectedBackground)
                .padding(horizontal = 16.dp, vertical = 8.dp)
        )
    }
}

