package hu.bbara.purefin.ui.screen.home.components

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Person
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeTopBar(
    onSearchClick: () -> Unit,
    onProfileClick: () -> Unit,
    onCheckForUpdates: () -> Unit,
    isCheckingForUpdates: Boolean,
    onSettingsClick: () -> Unit,
    onLogoutClick: () -> Unit,
    scrollBehavior: TopAppBarScrollBehavior? = null,
) {
    var isProfileMenuExpanded by remember { mutableStateOf(false) }

    DefaultTopBar(
        scrollBehavior = scrollBehavior,
        rightActions = {
            DefaultTopBarSearchButton(onClick = onSearchClick)
            DefaultTopBarIconButton(
                imageVector = Icons.Outlined.Person,
                contentDescription = "Profile",
                onClick = { isProfileMenuExpanded = true },
            )
            DropdownMenu(
                expanded = isProfileMenuExpanded,
                onDismissRequest = { isProfileMenuExpanded = false },
            ) {
                DropdownMenuItem(
                    text = { Text("Profile") },
                    onClick = {
                        isProfileMenuExpanded = false
                        onProfileClick()
                    }
                )
                DropdownMenuItem(
                    text = { Text(if (isCheckingForUpdates) "Checking updates..." else "Check for updates") },
                    enabled = !isCheckingForUpdates,
                    onClick = {
                        isProfileMenuExpanded = false
                        onCheckForUpdates()
                    }
                )
                DropdownMenuItem(
                    text = { Text("Settings") },
                    onClick = {
                        isProfileMenuExpanded = false
                        onSettingsClick()
                    }
                )
                DropdownMenuItem(
                    text = { Text("Logout") },
                    onClick = {
                        isProfileMenuExpanded = false
                        onLogoutClick()
                    }
                )
            }
        }
    )
}
