package ru.forbion.f7cloud.mobile.ui

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Settings
import ru.forbion.f7cloud.core.designsystem.F7AppMenuItem

enum class AppTab(
    val title: String,
    val headerIconPath: String,
    val menuIconPath: String,
) {
    Mail("Почта", "mail-header-icon.svg", "mail-glass.svg"),
    Files("Файлы", "files-header-icon.svg", "files-glass.svg"),
    Calendar("Календарь", "calendar-header-icon.svg", "calendar-glass.svg"),
    Contacts("Контакты", "contacts-header-icon.svg", "contact-glass.svg"),
    Talk("Конференции", "spreed-header-icon.svg", "spreed-glass.svg"),
    Deck("Карточки", "deck-header-icon.svg", "deck-glass.svg"),
    Tasks("Задачи", "task-header-icon.svg", "task-glass.svg"),
    Support("Поддержка", "icon-header-f7support.svg", "icon-header-f7support.svg"),
    ;

    fun menuIconUrl(serverUrl: String): String {
        return "${serverUrl.trimEnd('/')}/themes/forbion/images/header/$menuIconPath"
    }
}

private data class AppMenuEntry(
    val tab: AppTab?,
    val label: String,
    val iconPath: String,
    val webPath: String? = null,
)

// Состав меню — по дизайну выдвижного меню (Почта/Конференции/Задачи/Поддержка — только
// в нижней панели; Уведомления/Настройки добавлены как нативные пункты, см. appMenuItems).
private val coreAppMenuEntries = listOf(
    AppMenuEntry(AppTab.Files, "Файлы", "files-glass.svg"),
    AppMenuEntry(AppTab.Calendar, "Календарь", "calendar-glass.svg"),
    AppMenuEntry(AppTab.Contacts, "Контакты", "contact-glass.svg"),
    AppMenuEntry(AppTab.Deck, "Карточки", "deck-glass.svg"),
    AppMenuEntry(null, "Заметки", "notes-glass.svg", webPath = "/apps/notes/"),
)

data class AppMenuExternalSite(
    val name: String,
    val iconUrl: String,
    val openUrl: String,
)

val appMenuTabs: List<AppTab> = AppTab.entries

fun appMenuItems(
    serverUrl: String,
    active: AppTab,
    externalSites: List<AppMenuExternalSite> = emptyList(),
): List<F7AppMenuItem> {
    val base = serverUrl.trimEnd('/')
    val core = coreAppMenuEntries.map { entry ->
        F7AppMenuItem(
            label = entry.label,
            iconUrl = "$base/themes/forbion/images/header/${entry.iconPath}",
            selected = entry.tab == active,
            externalUrl = entry.webPath?.let { "$base$it" },
        )
    }
    val external = externalSites.map { site ->
        F7AppMenuItem(
            label = site.name,
            iconUrl = site.iconUrl,
            selected = false,
            externalUrl = site.openUrl,
        )
    }
    // Нативные пункты по дизайну — действия обрабатываются в AppScaffold по label
    val native = listOf(
        F7AppMenuItem(
            label = "Уведомления",
            iconUrl = "",
            selected = false,
            localIcon = Icons.Filled.Notifications,
        ),
        F7AppMenuItem(
            label = "Настройки",
            iconUrl = "",
            selected = false,
            localIcon = Icons.Filled.Settings,
        ),
    )
    return core + external + native
}

fun appTabFromMenuIndex(index: Int): AppTab? {
    return coreAppMenuEntries.getOrNull(index)?.tab
}
