package io.nexilis.alpha.ui.main

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Call
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.VideoCall
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import io.nexilis.alpha.ui.screen.Screen

@Composable
fun ActionsMenu(navController: NavHostController, navMainController: NavHostController) {
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val navMainBackStackEntry by navMainController.currentBackStackEntryAsState()
    var isChat = false
    navBackStackEntry?.destination?.route.let {
        it?.let {
            isChat = it.startsWith("${Screen.Chat.route}/")
        }
    }
    navMainBackStackEntry?.destination?.route.let {
        it?.let {
            isChat = it.startsWith("${Screen.Chat.route}/")
        }
    }
    if (isChat) {
        ChatMenu(navController = navMainController)
    } else {
        GlobalMenu(navController = navController)
    }
}

@Composable
fun GlobalMenu(navController: NavHostController) {
    var isOpenMenu by remember {
        mutableStateOf(false)
    }
    IconButton(onClick = { isOpenMenu = !isOpenMenu }) {
        Icon(
            imageVector = Icons.Default.MoreVert,
            contentDescription = "",
            tint = MaterialTheme.colorScheme.primary
        )
    }
    DropdownMenu(
        expanded = isOpenMenu,
        onDismissRequest = { isOpenMenu = false }) {
        DropdownMenuItem(
            text = { Text(text = "New group") },
            onClick = { /*TODO*/ })
        DropdownMenuItem(
            text = { Text(text = "Add contact") },
            onClick = {
                isOpenMenu = false
                navController.navigate(Screen.Friend.route) {
                    launchSingleTop = true
                    restoreState = true
                }
            })
        DropdownMenuItem(
            text = { Text(text = "Settings") },
            onClick = { /*TODO*/ })
    }
}

@Composable
fun ChatMenu(navController: NavHostController) {
    var isOpenMenu by remember {
        mutableStateOf(false)
    }
    IconButton(onClick = { }) {
        Icon(
            imageVector = Icons.Default.VideoCall,
            contentDescription = "",
            tint = MaterialTheme.colorScheme.primary
        )
    }
    IconButton(onClick = { }) {
        Icon(
            imageVector = Icons.Default.Call,
            contentDescription = "",
            tint = MaterialTheme.colorScheme.primary
        )
    }
    IconButton(onClick = { isOpenMenu = !isOpenMenu }) {
        Icon(
            imageVector = Icons.Default.MoreVert,
            contentDescription = "",
            tint = MaterialTheme.colorScheme.primary
        )
    }
    DropdownMenu(
        expanded = isOpenMenu,
        onDismissRequest = { isOpenMenu = false }) {
        DropdownMenuItem(
            text = { Text(text = "Search chat") },
            onClick = { })
        DropdownMenuItem(
            text = { Text(text = "Clear chat") },
            onClick = { })
    }
}