package id.iiyh.inatrading.core.ui.components

import androidx.compose.foundation.background
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.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Notifications
import androidx.compose.material.icons.outlined.ShoppingCart
import androidx.compose.material.icons.outlined.SettingsInputAntenna
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.ripple
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.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import id.iiyh.inatrading.R
import id.iiyh.inatrading.core.ui.theme.BrandRed
import id.iiyh.inatrading.core.ui.theme.OnSurface
import id.iiyh.inatrading.core.ui.theme.OnSurfaceVariant

/**
 * Top App Bar bersama untuk semua main screen.
 *
 * Layout: [Logo] ————————————— [Cart+Badge] [Notif]
 */
@Composable
fun InaTopAppBar(
    modifier: Modifier = Modifier,
    cartItemCount: Int = 0,
    onMenuClick: () -> Unit = {},
    onRfidClick: () -> Unit = {},
    onCartClick: () -> Unit = {},
    onNotifClick: () -> Unit = {},
) {
    Box(
        modifier = modifier
            .fillMaxWidth()
            .shadow(
                elevation    = 0.dp,
                ambientColor = OnSurface.copy(alpha = 0.04f),
                spotColor    = OnSurface.copy(alpha = 0.04f),
            )
            .background(Color.White.copy(alpha = 0.85f))
            .statusBarsPadding()
            .padding(horizontal = 16.dp, vertical = 8.dp),
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            // INA Trading logo image
            InaLogo(size = LogoSize.Small)

            Spacer(modifier = Modifier.weight(1f))

            // Cart icon dengan badge
            TopBarIcon(
                icon        = Icons.Outlined.SettingsInputAntenna,
                contentDesc = stringResource(R.string.topbar_rfid),
                onClick     = onRfidClick,
            )

            Box {
                TopBarIcon(
                    icon        = Icons.Outlined.ShoppingCart,
                    contentDesc = stringResource(R.string.topbar_cart),
                    onClick     = onCartClick,
                )
                if (cartItemCount > 0) {
                    Box(
                        modifier = Modifier
                            .size(14.dp)
                            .offset(x = 6.dp, y = (-2).dp)
                            .background(BrandRed, CircleShape)
                            .align(Alignment.TopEnd),
                        contentAlignment = Alignment.Center,
                    ) {
                        Text(
                            text  = if (cartItemCount > 9) "9+" else cartItemCount.toString(),
                            style = MaterialTheme.typography.labelSmall.copy(fontSize = 9.sp),
                            color = Color.White,
                        )
                    }
                }
            }

            // Notif icon
            TopBarIcon(
                icon        = Icons.Outlined.Notifications,
                contentDesc = stringResource(R.string.topbar_notifications),
                onClick     = onNotifClick,
                modifier    = Modifier.padding(start = 4.dp),
            )
        }
    }
}

@Composable
private fun TopBarIcon(
    icon: ImageVector,
    contentDesc: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    Box(
        modifier = modifier
            .size(36.dp)
            .clickable(
                interactionSource = remember { MutableInteractionSource() },
                indication        = ripple(bounded = false, radius = 18.dp),
                onClick           = onClick,
            ),
        contentAlignment = Alignment.Center,
    ) {
        Icon(
            imageVector        = icon,
            contentDescription = contentDesc,
            tint               = OnSurfaceVariant,
            modifier           = Modifier.size(22.dp),
        )
    }
}
