package com.aiosman.ravenow.ui.index.tabs.profile.composable

import androidx.compose.foundation.ExperimentalFoundationApi
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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.PagerState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
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.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun UserContentPageIndicator(
    pagerState: PagerState,
    showAgentTab: Boolean = true
){
    val scope = rememberCoroutineScope()
    val AppColors = LocalAppTheme.current
    
    Column(
        modifier = Modifier.fillMaxWidth()
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp),
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            // 动态 Tab
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .weight(1f)
                    .noRippleClickable {
                        scope.launch {
                            pagerState.scrollToPage(0)
                        }
                    }
                    .padding(vertical = 12.dp)
            ) {
                Text(
                    text = stringResource(R.string.index_dynamic),
                    fontSize = 16.sp,
                    fontWeight = if (pagerState.currentPage == 0) FontWeight.SemiBold else FontWeight.Medium,
                    color = if (pagerState.currentPage == 0) AppColors.text else AppColors.secondaryText
                )
                Spacer(modifier = Modifier.height(if (pagerState.currentPage == 0) 6.dp else 4.dp))
                if (pagerState.currentPage == 0) {
                    Box(
                        modifier = Modifier
                            .width(20.dp)
                            .height(2.dp)
                            .background(AppColors.text)
                    )
                }
            }
            
            // 智能体 Tab (只在非智能体用户时显示)
            if (showAgentTab) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier
                        .weight(1f)
                        .noRippleClickable {
                            scope.launch {
                                pagerState.scrollToPage(1)
                            }
                        }
                        .padding(vertical = 12.dp)
                ) {
                    Text(
                        text = stringResource(R.string.chat_ai),
                        fontSize = 16.sp,
                        fontWeight = if (pagerState.currentPage == 1) FontWeight.SemiBold else FontWeight.Medium,
                        color = if (pagerState.currentPage == 1) AppColors.text else AppColors.secondaryText
                    )
                    Spacer(modifier = Modifier.height(if (pagerState.currentPage == 1) 6.dp else 4.dp))
                    if (pagerState.currentPage == 1) {
                        Box(
                            modifier = Modifier
                                .width(20.dp)
                                .height(2.dp)
                                .background(AppColors.text)
                        )
                    }
                }
            }
            
            // 群聊 Tab (只在非智能体用户时显示)
            if (showAgentTab) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier
                        .weight(1f)
                        .noRippleClickable {
                            scope.launch {
                                pagerState.scrollToPage(2)
                            }
                        }
                        .padding(vertical = 12.dp)
                ) {
                    Text(
                        text = stringResource(R.string.chat_group),
                        fontSize = 16.sp,
                        fontWeight = if (pagerState.currentPage == 2) FontWeight.SemiBold else FontWeight.Medium,
                        color = if (pagerState.currentPage == 2) AppColors.text else AppColors.secondaryText
                    )
                    Spacer(modifier = Modifier.height(if (pagerState.currentPage == 2) 6.dp else 4.dp))
                    if (pagerState.currentPage == 2) {
                        Box(
                            modifier = Modifier
                                .width(20.dp)
                                .height(2.dp)
                                .background(AppColors.text)
                        )
                    }
                }
            }
        }
    }
}
