package com.example.pmulabs.screensMobile.filterScreens

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.paging.LoadState
import androidx.paging.compose.collectAsLazyPagingItems
import com.example.pmulabs.basecomponents.navigate.BottomBarScreen
import com.example.pmulabs.basecomponents.navigate.CALENDAR_ARGUMENT_DATE
import com.example.pmulabs.designElem.elem.BackButton
import com.example.pmulabs.designElem.items.ArticleItem
import com.example.pmulabs.viewModels.AppViewModelProvider
import com.example.pmulabs.viewModels.ArticleScreenViewModel
import com.example.pmulabs.viewModels.CurrentUserViewModel
import java.text.SimpleDateFormat

@SuppressLint("UnrememberedMutableState")
@Composable
fun  CalendarScreen(navController: NavController, articleScreenViewModel: ArticleScreenViewModel= viewModel(factory = AppViewModelProvider.Factory), modifier: Modifier = Modifier, currentUserViewModel: CurrentUserViewModel = viewModel(factory = AppViewModelProvider.Factory)){
    val articles = articleScreenViewModel.articles.collectAsLazyPagingItems()
    var date=navController.currentBackStackEntry?.arguments?.getString(CALENDAR_ARGUMENT_DATE).toString()
    Log.d("date: ",date)
    val formatter = SimpleDateFormat("dd-MMMM-YY")

    LazyColumn(
        modifier= Modifier
            .background(Color.White)
            .fillMaxSize(),
        contentPadding = PaddingValues(top =75.dp, bottom = 10.dp, start = 10.dp,end=10.dp),
        verticalArrangement = Arrangement.spacedBy(1.dp)){
        item{
            BackButton(modifier=Modifier
                .clickable { navController.popBackStack()})
        }
        items(count = articles.itemCount) { index ->
            val article = articles[index]
            val publishDate=formatter.format(article?.publishDate)
            if(publishDate==date) {
                if (article != null) {
                    ArticleItem(
                        navController=navController,
                        article = article,
                        currentUserViewModel = currentUserViewModel,
                        onArticleClick = {
                            navController.navigate(
                                BottomBarScreen.ArticlePage.passId(
                                    article?.id.toString()
                                )
                            )
                        }
                    )
                }
            }
        }

        articles.apply {
            when {
                loadState.refresh is LoadState.Loading -> {
                    item { CircularProgressIndicator(modifier = Modifier.fillParentMaxSize(), color = Color(0xff423a99)) }
                }
                loadState.append is LoadState.Loading -> {
                    item { CircularProgressIndicator(modifier = Modifier.fillParentMaxSize(), color = Color(0xff423a99)) }
                }
                loadState.refresh is LoadState.Error -> {
                    val err = articles.loadState.refresh as LoadState.Error
                    item { Text(err.error.localizedMessage) }
                }
                loadState.append is LoadState.Error -> {
                    val err = articles.loadState.append as LoadState.Error
                    item { Text(err.error.localizedMessage) }
                }
            }
        }
    }
}