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.height
import androidx.compose.foundation.layout.width
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 onTasksClick: () -> Unit = {},
    val onConferencesClick: () -> Unit = {},
    val onMenuClick: () -> Unit = {},
)

// Иконки нижней панели — ЛОКАЛЬНЫЕ mobile-*-icon из живой темы (assets/menu/), 1:1 с сайтом.
private fun barIcon(name: String): String = "file:///android_asset/menu/$name"

// Живой forbion (_header.css .header__mobile-bottom): пилюля 328×61, паддинг 3,
// фон #F5F5F5; кнопки 55, фон #FBFBFB, рамка #E6E6E6; активная кнопка-раздел и открытый
// бургер — полупрозрачный зелёный градиент 104°.
private val BottomBarWidth = 328.dp
private val BottomBarHeight = 61.dp
private val BottomBarButtonSize = 55.dp
// Живой сайт: app-link/бургер img = 25px; стрелка «назад» мельче.
private val BottomBarIconSize = 25.dp
private val BottomBarSectionIconSize = 25.dp
private val BottomBarBackIconSize = 18.dp
private val BottomBarOuterPadding = 3.dp
private val BottomBarButtonShape = RoundedCornerShape(100.dp)
private val BottomBarButtonBg = Color(0xFFFBFBFB)
private val BottomBarButtonBorder = Color(0xFFE6E6E6)
private val BottomBarActiveBrush = 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(100.dp)
    Row(
        modifier = modifier
            .width(BottomBarWidth)
            .height(BottomBarHeight)
            .shadow(
                elevation = 2.dp,
                shape = pillShape,
                spotColor = Color(0xFFE6E6E6),
            )
            .clip(pillShape)
            .background(Color(0xFFF5F5F5))
            .padding(BottomBarOuterPadding),
        horizontalArrangement = Arrangement.SpaceBetween,
        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 = "Чаты",
                    active = chatsHighlighted,
                    onClick = actions.onChatsClick,
                )
                F7BottomBarSlot.NavBack -> F7BottomBarIconSlot(
                    iconUrl = barIcon("sidebar-chevron-left.svg"),
                    contentDescription = "Назад",
                    iconSize = BottomBarBackIconSize,
                    iconRotation = if (navBackHighlighted) 180f else 0f,
                    onClick = actions.onNavBackClick,
                )
                F7BottomBarSlot.SectionMail -> F7BottomBarIconSlot(
                    iconUrl = barIcon("mobile-mail-icon.svg"),
                    contentDescription = "Почта",
                    iconSize = BottomBarSectionIconSize,
                    active = activeTabKey.equals("mail", ignoreCase = true),
                    onClick = actions.onMailClick,
                )
                F7BottomBarSlot.SectionTasks -> F7BottomBarIconSlot(
                    iconUrl = barIcon("mobile-tasks-icon.svg"),
                    contentDescription = "Задачи",
                    iconSize = BottomBarSectionIconSize,
                    active = activeTabKey.equals("tasks", ignoreCase = true),
                    onClick = actions.onTasksClick,
                )
                F7BottomBarSlot.SectionConferences -> F7BottomBarIconSlot(
                    iconUrl = barIcon("mobile-spreed-icon.svg"),
                    contentDescription = "Конференции",
                    iconSize = BottomBarSectionIconSize,
                    active = activeTabKey.equals("talk", ignoreCase = true),
                    onClick = actions.onConferencesClick,
                )
                F7BottomBarSlot.Menu -> F7BottomBarIconSlot(
                    iconUrl = barIcon("mobile-burger-icon.svg"),
                    contentDescription = "Меню",
                    active = menuOpen,
                    showBadge = showNotificationBadge,
                    onClick = actions.onMenuClick,
                )
            }
        }
    }
}

@Composable
private fun F7BottomBarIconSlot(
    iconUrl: String,
    contentDescription: String,
    onClick: () -> Unit,
    active: Boolean = false,
    iconRotation: Float = 0f,
    showBadge: Boolean = false,
    iconSize: Dp = BottomBarIconSize,
) {
    // Живой forbion: кнопка 55, фон #FBFBFB + рамка #E6E6E6; активная (текущий раздел /
    // открытый бургер) — полупрозрачный зелёный градиент 104°.
    Box(
        modifier = Modifier
            .size(BottomBarButtonSize)
            .clip(BottomBarButtonShape)
            .then(
                if (active) {
                    Modifier.background(BottomBarActiveBrush, BottomBarButtonShape)
                } else {
                    Modifier.background(BottomBarButtonBg, BottomBarButtonShape)
                },
            )
            .border(
                width = 1.dp,
                color = BottomBarButtonBorder,
                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)),
            )
        }
    }
}
