package com.aiosman.riderpro.ui.profile

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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
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.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.R
import com.aiosman.riderpro.exp.viewModelFactory
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.composables.StatusBarSpacer
import com.aiosman.riderpro.ui.index.tabs.profile.MomentPostUnit
import com.aiosman.riderpro.ui.index.tabs.profile.UserInformation
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun AccountProfile(id: String) {
    val model: AccountProfileViewModel = viewModel(factory = viewModelFactory {
        AccountProfileViewModel()
    }, key = "viewModel_${id}")
    val scope = rememberCoroutineScope()
    val items = model.momentsFlow.collectAsLazyPagingItems()
    val state = rememberPullRefreshState(model.refreshing, onRefresh = {
        model.loadProfile(id, pullRefresh = true)
    })
    val navController = LocalNavController.current
    LaunchedEffect(Unit) {
        model.loadProfile(id)
    }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .pullRefresh(state)
            .background(Color(0xFFF5F5F5))
    ) {
        LazyColumn(
            modifier = Modifier
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Top,
        ) {
            item {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(400.dp)

                ) {
                    if (model.profile != null) {
                        val banner = model.profile?.banner
                        if (banner != null) {
                            CustomAsyncImage(
                                LocalContext.current,
                                banner,
                                modifier = Modifier.fillMaxSize(),
                                contentDescription = "",
                                contentScale = ContentScale.Crop
                            )
                        } else {
                            Image(
                                painter = painterResource(id = R.drawable.rider_pro_moment_demo_2),
                                modifier = Modifier.fillMaxSize(),
                                contentDescription = "",
                                contentScale = ContentScale.Crop
                            )
                        }
                    }
                    Box(
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Column(
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            StatusBarSpacer()
                            Row(
                                modifier = Modifier.padding(vertical = 10.dp, horizontal = 18.dp)
                            ) {
                                Image(
                                    painter = painterResource(id = R.drawable.rider_pro_nav_back),
                                    contentDescription = "",
                                    modifier = Modifier
                                        .size(24.dp)
                                        .noRippleClickable {
                                            navController.popBackStack()
                                        },
                                    colorFilter = ColorFilter.tint(Color.White)
                                )
                            }
                        }

                    }


                }
                Spacer(modifier = Modifier.height(32.dp))
                model.profile?.let {
                    UserInformation(
                        isSelf = false,
                        accountProfileEntity = it,
                        onFollowClick = {
                            scope.launch {
                                if (it.isFollowing) {
                                    model.unFollowUser(id)
                                } else {
                                    model.followUser(id)
                                }
                            }
                        },
                    )
                }
//                RidingStyle()
            }

            items(items.itemCount) { idx ->
                val momentItem = items[idx] ?: return@items
                MomentPostUnit(momentItem)
            }
            item {
                Spacer(modifier = Modifier.height(32.dp))
            }

        }
        PullRefreshIndicator(model.refreshing, state, Modifier.align(Alignment.TopCenter))
    }


}