package com.aiosman.riderpro.ui.favourite

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.R
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.comment.NoticeScreenHeader
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.composables.StatusBarSpacer
import com.aiosman.riderpro.ui.favourite.FavouriteListViewModel.refreshPager
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import com.aiosman.riderpro.ui.navigateToPost

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun FavouriteListPage() {
    val model = FavouriteListViewModel
    var dataFlow = model.favouriteMomentsFlow
    var moments = dataFlow.collectAsLazyPagingItems()
    val context = LocalContext.current
    val navController = LocalNavController.current
    val state = rememberPullRefreshState(FavouriteListViewModel.isLoading, onRefresh = {
        model.refreshPager(force = true)
    })
    LaunchedEffect(Unit) {
        refreshPager()
    }
    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        StatusBarSpacer()
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)
                .pullRefresh(state)
        ) {
            Column(
                modifier = Modifier.fillMaxSize()
            ) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp, vertical = 16.dp),
                ) {
                    NoticeScreenHeader(stringResource(R.string.favourites_upper), moreIcon = false)
                }
                LazyVerticalGrid(
                    columns = GridCells.Fixed(3),
                    modifier = Modifier.fillMaxSize()
                ) {
                    items(moments.itemCount) { idx ->
                        val momentItem = moments[idx] ?: return@items
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .aspectRatio(1f)
                                .padding(2.dp)
                                .noRippleClickable {
                                    navController.navigateToPost(
                                        id = momentItem.id,
                                        highlightCommentId = 0,
                                        initImagePagerIndex = 0
                                    )
                                }
                        ) {
                            CustomAsyncImage(
                                imageUrl = momentItem.images[0].thumbnail,
                                contentDescription = "",
                                modifier = Modifier
                                    .fillMaxSize()
                                    .clip(RoundedCornerShape(8.dp)),
                                context = context
                            )
                            if (momentItem.images.size > 1) {
                                Box(
                                    modifier = Modifier
                                        .padding(top = 8.dp, end = 8.dp)
                                        .align(Alignment.TopEnd)
                                ) {
                                    Image(
                                        modifier = Modifier.size(24.dp),
                                        painter = painterResource(R.drawable.rider_pro_picture_more),
                                        contentDescription = "",
                                    )
                                }
                            }


                        }

                    }
                }

            }

            PullRefreshIndicator(
                FavouriteListViewModel.isLoading,
                state,
                Modifier.align(Alignment.TopCenter)
            )

        }

    }
}