package com.zaojiao.app.feat.home.personal

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.zaojiao.app.core.navx.NavXUtils

@Composable
internal fun HomePersonalRoute(
    viewModel: HomePersonalViewModel = hiltViewModel()
) {
    val userUiState by viewModel.userUiState.collectAsStateWithLifecycle()
    val interactiveUiState by viewModel.interactiveUiState.collectAsStateWithLifecycle()

    HomePersonalPage(
        userUiState = userUiState,
        interactiveUiState = interactiveUiState,
        navToPageWithLogin = {
            NavXUtils.navToRoute(it, needLogin = true)
        },
        navToPageWithoutLogin = {
            NavXUtils.navToRoute(it, needLogin = false)
        },
    )
}

/**
 *  首页 - 我的勋章
 */
@Composable
fun HomePersonalPage(
    userUiState: HomePersonalUserUiState,
    interactiveUiState: HomePersonalInteractiveUiState,
    navToPageWithLogin: (route: String) -> Unit,
    navToPageWithoutLogin: (route: String) -> Unit,
) {
    Box(
        modifier = Modifier
            .background(
                brush = Brush.verticalGradient(
                    listOf(Color(0xFFFEFEFE), Color(0xFFFAFAFA)),
                )
            )
            .fillMaxSize()
    ) {
        val state = rememberLazyListState()

        LazyColumn(
            state = state
        ) {
            item {
                HomePersonalTopBar(
                    navToPageWithLogin = navToPageWithLogin,
                    navToPageWithoutLogin = navToPageWithoutLogin,
                )
            }

            item {
                HomePersonalUserBar(
                    userUiState = userUiState,
                    navToPageWithLogin = navToPageWithLogin,
                    navToPageWithoutLogin = navToPageWithoutLogin,
                )
            }

            item {
                HomePersonalEngage(
                    interactiveUiState = interactiveUiState,
                    navToPageWithLogin = navToPageWithLogin,
                    navToPageWithoutLogin = navToPageWithoutLogin,
                )
            }

            item {
                HomePersonalContent(
                    navToPageWithLogin = navToPageWithLogin,
                    navToPageWithoutLogin = navToPageWithoutLogin,
                )
            }

            item {
                HomePersonalAccount(
                    navToPageWithLogin = navToPageWithLogin,
                    navToPageWithoutLogin = navToPageWithoutLogin,
                )
            }

            item {
                HomePersonalService(
                    navToPageWithLogin = navToPageWithLogin,
                    navToPageWithoutLogin = navToPageWithoutLogin,
                )
            }

            item {
                HomePersonalBottom()
            }
        }

        val showTop by remember {
            derivedStateOf {
                state.firstVisibleItemIndex > 1
            }
        }

        AnimatedVisibility(
            visible = showTop,
            enter = fadeIn(),
            exit = fadeOut(),
        ) {
            HomePersonalScrollTopBar(
                userUiState = userUiState,
            )
        }
    }
}


