package ru.forbion.f7cloud.feature.files

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import ru.forbion.f7cloud.core.designsystem.F7Colors

enum class FilesCreateAction {
    UploadFiles,
    UploadFolders,
    NewFolder,
    FileRequest,
    NewDiagram,
    NewBoard,
    NewPresentation,
    NewSpreadsheet,
    NewDocument,
    NewTextFile,
    TemplateFolder,
    FolderDescription,
}

@Composable
fun FilesCreateMenu(
    serverUrl: String,
    visible: Boolean,
    onDismiss: () -> Unit,
    onAction: (FilesCreateAction) -> Unit,
    modifier: Modifier = Modifier,
    bottomOffset: androidx.compose.ui.unit.Dp = 88.dp,
) {
    if (!visible) return
    val base = serverUrl.trimEnd('/')
    Box(modifier = modifier.fillMaxSize()) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(bottom = bottomOffset)
                .background(Color.Black.copy(alpha = 0.18f))
                .clickable(
                    interactionSource = remember { MutableInteractionSource() },
                    indication = null,
                    onClick = onDismiss,
                ),
        )
        Column(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(start = 16.dp, end = 16.dp, bottom = bottomOffset)
                .navigationBarsPadding()
                .widthIn(max = 360.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .shadow(
                        elevation = 12.dp,
                        shape = RoundedCornerShape(16.dp),
                        spotColor = Color.Black.copy(alpha = 0.12f),
                    )
                    .clip(RoundedCornerShape(16.dp))
                    .background(F7Colors.SecondaryButtonBg)
                    .border(1.dp, F7Colors.Border, RoundedCornerShape(16.dp))
                    .clickable(
                        interactionSource = remember { MutableInteractionSource() },
                        indication = null,
                        onClick = {},
                    )
                    .verticalScroll(rememberScrollState())
                    .padding(vertical = 8.dp),
            ) {
                FilesCreateSectionHeader("Загрузить с устройства")
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/upload-black.svg",
                    label = "Загрузить файлы",
                    onClick = { onDismiss(); onAction(FilesCreateAction.UploadFiles) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/folder-black.svg",
                    label = "Загрузить папки",
                    onClick = { onDismiss(); onAction(FilesCreateAction.UploadFolders) },
                )
                FilesCreateDivider()
                FilesCreateSectionHeader("Создать новое")
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/add-folder-black.svg",
                    label = "Новая папка",
                    onClick = { onDismiss(); onAction(FilesCreateAction.NewFolder) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/plus-black.svg",
                    label = "Запрос на создание файла",
                    onClick = { onDismiss(); onAction(FilesCreateAction.FileRequest) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/file-presentation.svg",
                    label = "Новая диаграмма",
                    onClick = { onDismiss(); onAction(FilesCreateAction.NewDiagram) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/local-edit-black.svg",
                    label = "Новая доска",
                    onClick = { onDismiss(); onAction(FilesCreateAction.NewBoard) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/file-presentation.svg",
                    label = "Новая презентация",
                    onClick = { onDismiss(); onAction(FilesCreateAction.NewPresentation) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/file-sheet.svg",
                    label = "Новая таблица",
                    onClick = { onDismiss(); onAction(FilesCreateAction.NewSpreadsheet) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/file-doc-docx.svg",
                    label = "Новый документ",
                    onClick = { onDismiss(); onAction(FilesCreateAction.NewDocument) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/add-text-file-black.svg",
                    label = "Новый текстовый файл",
                    onClick = { onDismiss(); onAction(FilesCreateAction.NewTextFile) },
                )
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/plus-black.svg",
                    label = "Создать папку шаблонов",
                    onClick = { onDismiss(); onAction(FilesCreateAction.TemplateFolder) },
                )
                FilesCreateDivider()
                FilesCreateMenuItem(
                    iconUrl = "$base/themes/forbion/images/files/edit-pencil-black.svg",
                    label = "Добавить описание папки",
                    onClick = { onDismiss(); onAction(FilesCreateAction.FolderDescription) },
                )
            }
            Box(
                modifier = Modifier
                    .padding(top = 2.dp)
                    .size(width = 14.dp, height = 8.dp)
                    .clip(RoundedCornerShape(bottomStart = 2.dp, bottomEnd = 2.dp))
                    .background(F7Colors.SecondaryButtonBg)
                    .border(1.dp, F7Colors.Border, RoundedCornerShape(2.dp)),
            )
        }
    }
}

@Composable
private fun FilesCreateSectionHeader(text: String) {
    Text(
        text = text,
        modifier = Modifier.padding(horizontal = 16.dp, vertical = 6.dp),
        style = MaterialTheme.typography.labelMedium,
        color = F7Colors.TextSecondary,
    )
}

@Composable
private fun FilesCreateDivider() {
    HorizontalDivider(
        modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
        color = F7Colors.Border,
    )
}

@Composable
private fun FilesCreateMenuItem(
    iconUrl: String,
    label: String,
    onClick: () -> Unit,
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable(onClick = onClick)
            .padding(horizontal = 16.dp, vertical = 10.dp),
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.spacedBy(12.dp),
    ) {
        AsyncImage(
            model = iconUrl,
            contentDescription = null,
            modifier = Modifier.size(20.dp),
            contentScale = ContentScale.Fit,
        )
        Text(
            text = label,
            style = MaterialTheme.typography.bodyMedium,
            color = F7Colors.TextPrimary,
            maxLines = 2,
            overflow = TextOverflow.Ellipsis,
        )
    }
}
