package ru.forbion.f7cloud.feature.mail

data class MailBackStackState(
    val searchFilterOpen: Boolean,
    val snoozeSheetOpen: Boolean,
    val tagsSheetOpen: Boolean,
    val moveSheetOpen: Boolean,
    val settingsOpen: Boolean,
    val editingAccountId: Int?,
    val sidebarOpen: Boolean,
    val messageOpen: Boolean,
)

fun MailBackStackState.canGoBack(): Boolean =
    searchFilterOpen ||
        snoozeSheetOpen ||
        tagsSheetOpen ||
        moveSheetOpen ||
        settingsOpen ||
        sidebarOpen ||
        messageOpen

fun navigateMailBack(
    state: MailBackStackState,
    closeSearchFilter: () -> Unit,
    closeSnoozeSheet: () -> Unit,
    closeTagsSheet: () -> Unit,
    closeMoveSheet: () -> Unit,
    closeAccountSettings: () -> Unit,
    closeSettings: () -> Unit,
    closeSidebar: () -> Unit,
    closeMessage: () -> Unit,
): Boolean = when {
    state.searchFilterOpen -> {
        closeSearchFilter()
        true
    }
    state.snoozeSheetOpen -> {
        closeSnoozeSheet()
        true
    }
    state.tagsSheetOpen -> {
        closeTagsSheet()
        true
    }
    state.moveSheetOpen -> {
        closeMoveSheet()
        true
    }
    state.settingsOpen && state.editingAccountId != null -> {
        closeAccountSettings()
        true
    }
    state.settingsOpen -> {
        closeSettings()
        true
    }
    state.sidebarOpen -> {
        closeSidebar()
        true
    }
    state.messageOpen -> {
        closeMessage()
        true
    }
    else -> false
}

data class MailComposeBackStackState(
    val attachmentMenuOpen: Boolean,
    val sendLaterMenuOpen: Boolean,
    val moreMenuOpen: Boolean,
    val toolbarVisible: Boolean,
)

fun MailComposeBackStackState.canGoBack(): Boolean =
    attachmentMenuOpen || sendLaterMenuOpen || moreMenuOpen || toolbarVisible

fun navigateMailComposeBack(
    state: MailComposeBackStackState,
    closeAttachmentMenu: () -> Unit,
    closeSendLaterMenu: () -> Unit,
    closeMoreMenu: () -> Unit,
    closeToolbar: () -> Unit,
    closeScreen: () -> Unit,
): Boolean = when {
    state.attachmentMenuOpen -> {
        closeAttachmentMenu()
        true
    }
    state.sendLaterMenuOpen -> {
        closeSendLaterMenu()
        true
    }
    state.moreMenuOpen -> {
        closeMoreMenu()
        true
    }
    state.toolbarVisible -> {
        closeToolbar()
        true
    }
    else -> {
        closeScreen()
        true
    }
}
