package com.rosetta.messenger.ui.chats

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import com.rosetta.messenger.network.SearchUser
import com.rosetta.messenger.repository.AvatarRepository
import com.rosetta.messenger.ui.onboarding.PrimaryBlue
import com.rosetta.messenger.ui.icons.TelegramIcons
import compose.icons.TablerIcons
import compose.icons.tablericons.*
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun RequestsListScreen(
        isDarkTheme: Boolean,
        chatsViewModel: ChatsListViewModel,
        pinnedChats: Set<String>,
        onTogglePin: (String) -> Unit,
        onBack: () -> Unit,
        onUserSelect: (SearchUser) -> Unit,
        avatarRepository: AvatarRepository? = null
) {
        val chatsState by chatsViewModel.chatsState.collectAsState()
        val syncInProgress by chatsViewModel.syncInProgress.collectAsState()
        val requests = if (syncInProgress) emptyList() else chatsState.requests
        val blockedUsers by chatsViewModel.blockedUsers.collectAsState()
        val scope = rememberCoroutineScope()
        val backgroundColor = if (isDarkTheme) Color(0xFF1A1A1A) else Color(0xFFF2F2F7)
        val headerColor = if (isDarkTheme) Color(0xFF2A2A2A) else Color(0xFF0D8CF4)

        Scaffold(
                topBar = {
                        TopAppBar(
                                navigationIcon = {
                                        IconButton(onClick = onBack) {
                                                Icon(
                                                        imageVector = TablerIcons.ChevronLeft,
                                                        contentDescription = "Back",
                                                        tint = Color.White
                                                )
                                        }
                                },
                                title = {
                                        Text(
                                                text = if (syncInProgress) "Synchronizing..." else "Requests",
                                                fontWeight = FontWeight.Bold,
                                                fontSize = 20.sp,
                                                color = Color.White
                                        )
                                },
                                colors =
                                        TopAppBarDefaults.topAppBarColors(
                                                containerColor = headerColor,
                                                scrolledContainerColor = headerColor,
                                                navigationIconContentColor = Color.White,
                                                titleContentColor = Color.White,
                                                actionIconContentColor = Color.White
                                        )
                        )
                },
                containerColor = backgroundColor
        ) { paddingValues ->
                Box(
                        modifier =
                                Modifier.fillMaxSize()
                                        .background(backgroundColor)
                                        .padding(paddingValues)
                ) {
                        RequestsScreen(
                                requests = requests,
                                isDarkTheme = isDarkTheme,
                                onRequestClick = { request ->
                                        onUserSelect(chatsViewModel.dialogToSearchUser(request))
                                },
                                avatarRepository = avatarRepository,
                                blockedUsers = blockedUsers,
                                pinnedChats = pinnedChats,
                                onTogglePin = onTogglePin,
                                onDeleteDialog = { opponentKey ->
                                        scope.launch {
                                                chatsViewModel.deleteDialog(opponentKey)
                                        }
                                },
                                onBlockUser = { opponentKey ->
                                        scope.launch { chatsViewModel.blockUser(opponentKey) }
                                },
                                onUnblockUser = { opponentKey ->
                                        scope.launch { chatsViewModel.unblockUser(opponentKey) }
                                }
                        )
                }
        }
}
