package com.rosetta.messenger.ui.chats.attach

import android.Manifest
import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import android.content.pm.PackageManager
import com.rosetta.messenger.ui.chats.components.ThumbnailPosition
import com.rosetta.messenger.ui.icons.TelegramIcons
import com.rosetta.messenger.ui.onboarding.PrimaryBlue
import androidx.compose.material3.Icon

/**
 * Photo/Video tab content for the attach alert.
 * Shows a 3-column grid with camera preview in the first cell,
 * album dropdown, multi-select with numbered badges.
 */
@Composable
internal fun AttachAlertPhotoLayout(
    state: AttachAlertUiState,
    gridState: LazyGridState,
    cameraEnabled: Boolean = true,
    onCameraClick: () -> Unit,
    onItemClick: (MediaItem, ThumbnailPosition) -> Unit,
    onItemCheckClick: (MediaItem) -> Unit,
    onItemLongClick: (MediaItem) -> Unit,
    onRequestPermission: () -> Unit,
    isDarkTheme: Boolean,
    modifier: Modifier = Modifier
) {
    val textColor = if (isDarkTheme) Color.White else Color.Black
    val secondaryTextColor = Color(0xFF8E8E93)

    when {
        !state.hasPermission -> {
            PermissionRequestView(
                isDarkTheme = isDarkTheme,
                textColor = textColor,
                secondaryTextColor = secondaryTextColor,
                onRequestPermission = onRequestPermission
            )
        }
        state.isLoading -> {
            Box(
                modifier = modifier.fillMaxWidth(),
                contentAlignment = Alignment.Center
            ) {
                CircularProgressIndicator(
                    color = PrimaryBlue,
                    modifier = Modifier.size(48.dp)
                )
            }
        }
        state.visibleMediaItems.isEmpty() -> {
            Box(
                modifier = modifier.fillMaxWidth(),
                contentAlignment = Alignment.Center
            ) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    Icon(
                        painter = TelegramIcons.Photos,
                        contentDescription = null,
                        tint = secondaryTextColor,
                        modifier = Modifier.size(64.dp)
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    Text(
                        text = "No photos or videos",
                        color = secondaryTextColor,
                        fontSize = 16.sp
                    )
                }
            }
        }
        else -> {
            MediaGrid(
                mediaItems = state.visibleMediaItems,
                selectedItemOrder = state.selectedItemOrder,
                showCameraItem = state.visibleAlbum?.isAllMedia != false,
                cameraEnabled = cameraEnabled,
                gridState = gridState,
                onCameraClick = onCameraClick,
                onItemClick = onItemClick,
                onItemCheckClick = onItemCheckClick,
                onItemLongClick = onItemLongClick,
                isDarkTheme = isDarkTheme,
                modifier = modifier
            )
        }
    }
}
