package ru.forbion.f7cloud.core.designsystem

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.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.shape.RoundedCornerShape
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.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage

data class F7BottomBarActions(
    val onChatsClick: () -> Unit = {},
    val onNavBackClick: () -> Unit = {},
    val onMailClick: () -> Unit = {},
    val onCardsClick: () -> Unit = {},
    val onConferencesClick: () -> Unit = {},
    val onMenuClick: () -> Unit = {},
)

private val BottomBarButtonSize = 55.dp
private val BottomBarIconSize = 34.dp

// Иконки-разделы (Почта/Карточки/Конференции) — серверные SVG с внутренним
// «воздухом», поэтому рендерим крупнее, чтобы заполняли кружок.
private val BottomBarSectionIconSize = 46.dp
private val BottomBarGap = 8.dp
private val BottomBarOuterPaddingH = 6.dp
private val BottomBarOuterPaddingV = 6.dp
private val BottomBarButtonShape = RoundedCornerShape(100.dp)
private val BottomBarBorderBrush = Brush.linearGradient(
    listOf(Color(0x33C0FF7B), Color(0x3370B62B)),
)

@Composable
fun F7MobileBottomBar(
    serverUrl: String,
    userId: String,
    config: F7BottomBarConfig,
    actions: F7BottomBarActions,
    activeTabKey: String = "",
    menuOpen: Boolean = false,
    chatsHighlighted: Boolean = false,
    navBackHighlighted: Boolean = false,
    showNotificationBadge: Boolean = false,
    modifier: Modifier = Modifier,
) {
    val base = serverUrl.trimEnd('/')
    val pillShape = RoundedCornerShape(percent = 50)
    Row(
        modifier = modifier
            .wrapContentWidth()
            .shadow(
                elevation = 2.dp,
                shape = pillShape,
                spotColor = Color(0xFFE6E6E6),
            )
            .clip(pillShape)
            .background(Color(0xFFF5F5F5))
            .padding(
                horizontal = BottomBarOuterPaddingH,
                vertical = BottomBarOuterPaddingV,
            ),
        horizontalArrangement = Arrangement.spacedBy(BottomBarGap),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        config.slots.forEach { slot ->
            when (slot) {
                F7BottomBarSlot.Chats -> F7BottomBarIconSlot(
                    iconUrl = if (chatsHighlighted) {
                        "$base/themes/forbion/images/header/chat-icon-green.svg"
                    } else {
                        "$base/themes/forbion/images/header/chat-icon-gray.svg"
                    },
                    contentDescription = "Чаты",
                    onClick = actions.onChatsClick,
                )
                F7BottomBarSlot.NavBack -> F7BottomBarIconSlot(
                    iconUrl = "$base/themes/forbion/images/header/sidebar-chevron-left.svg",
                    contentDescription = "Папки",
                    iconRotation = if (navBackHighlighted) 180f else 0f,
                    onClick = actions.onNavBackClick,
                )
                F7BottomBarSlot.SectionMail -> F7BottomBarIconSlot(
                    iconUrl = "$base/themes/forbion/images/header/mail-header-icon.svg",
                    contentDescription = "Почта",
                    iconSize = BottomBarSectionIconSize,
                    onClick = actions.onMailClick,
                )
                F7BottomBarSlot.SectionCards -> F7BottomBarIconSlot(
                    iconUrl = "$base/themes/forbion/images/header/deck-header-icon.svg",
                    contentDescription = "Карточки",
                    iconSize = BottomBarSectionIconSize,
                    onClick = actions.onCardsClick,
                )
                F7BottomBarSlot.SectionConferences -> F7BottomBarIconSlot(
                    iconUrl = "$base/themes/forbion/images/header/spreed-header-icon.svg",
                    contentDescription = "Конференции",
                    iconSize = BottomBarSectionIconSize,
                    onClick = actions.onConferencesClick,
                )
                F7BottomBarSlot.Menu -> F7BottomBarIconSlot(
                    iconUrl = if (menuOpen) {
                        "$base/themes/forbion/images/header/menu-burger-green.svg"
                    } else {
                        "$base/themes/forbion/images/header/menu-burger-gray.svg"
                    },
                    contentDescription = "Меню",
                    showBadge = showNotificationBadge,
                    onClick = actions.onMenuClick,
                )
            }
        }
    }
}

@Composable
private fun F7BottomBarIconSlot(
    iconUrl: String,
    contentDescription: String,
    onClick: () -> Unit,
    iconRotation: Float = 0f,
    showBadge: Boolean = false,
    iconSize: Dp = BottomBarIconSize,
) {
    // Зелёной подложки под активной кнопкой быть не должно — фон всегда нейтральный.
    Box(
        modifier = Modifier
            .size(BottomBarButtonSize)
            .clip(BottomBarButtonShape)
            .background(Color(0x99FFFFFF), BottomBarButtonShape)
            .border(
                width = 1.dp,
                brush = BottomBarBorderBrush,
                shape = BottomBarButtonShape,
            )
            .clickable(
                interactionSource = remember { MutableInteractionSource() },
                indication = null,
                onClick = onClick,
            ),
        contentAlignment = Alignment.Center,
    ) {
        AsyncImage(
            model = iconUrl,
            contentDescription = contentDescription,
            modifier = Modifier
                .size(iconSize)
                .graphicsLayer { rotationZ = iconRotation },
            contentScale = ContentScale.Fit,
        )
        if (showBadge) {
            Box(
                Modifier
                    .align(Alignment.TopEnd)
                    .padding(top = 10.dp, end = 10.dp)
                    .size(8.dp)
                    .clip(BottomBarButtonShape)
                    .background(Color(0xFFE53935)),
            )
        }
    }
}
