package com.rosetta.messenger.ui.chats.calls

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.height
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.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Mic
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.rosetta.messenger.network.CallPhase
import com.rosetta.messenger.network.CallUiState
import com.rosetta.messenger.repository.AvatarRepository
import com.rosetta.messenger.ui.components.AvatarImage

private val TelegramCallGreenStart = Color(0xFF3FD17F)
private val TelegramCallGreenEnd = Color(0xFF27C4AE)

@Composable
fun CallTopBanner(
    state: CallUiState,
    onOpenCall: () -> Unit,
    isSticky: Boolean = false,
    isDarkTheme: Boolean = true,
    avatarRepository: AvatarRepository? = null
) {
    AnimatedVisibility(
        visible = state.isVisible && state.phase != CallPhase.INCOMING,
        enter = slideInVertically(initialOffsetY = { -it / 2 }, animationSpec = tween(220)) +
            fadeIn(animationSpec = tween(220)),
        exit = slideOutVertically(targetOffsetY = { -it / 2 }, animationSpec = tween(180)) +
            fadeOut(animationSpec = tween(160))
    ) {
        val bannerModifier =
            if (isSticky) {
                Modifier
                    .fillMaxWidth()
                    .height(42.dp)
            } else {
                Modifier
                    .fillMaxWidth()
                    .statusBarsPadding()
                    .padding(horizontal = 10.dp, vertical = 8.dp)
                    .height(42.dp)
                    .clip(RoundedCornerShape(12.dp))
            }

        Row(
            modifier = bannerModifier
                .background(
                    Brush.horizontalGradient(
                        listOf(TelegramCallGreenStart, TelegramCallGreenEnd)
                    )
                )
                .clickable { onOpenCall() }
                .padding(horizontal = 12.dp),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Box(modifier = Modifier.offset(x = if (isSticky) (-4).dp else 0.dp)) {
                AvatarImage(
                    publicKey = state.peerPublicKey,
                    avatarRepository = avatarRepository,
                    size = 24.dp,
                    isDarkTheme = isDarkTheme,
                    showOnlineIndicator = false,
                    displayName = state.displayName
                )
            }

            Spacer(modifier = Modifier.width(8.dp))

            Text(
                text = buildBannerText(state),
                color = Color.White,
                fontSize = 16.sp,
                fontWeight = FontWeight.SemiBold,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
                modifier = Modifier.weight(1f)
            )

            Box(
                modifier =
                    Modifier
                        .size(30.dp)
                        .clip(CircleShape)
                        .background(Color.White.copy(alpha = 0.16f)),
                contentAlignment = Alignment.Center
            ) {
                Icon(
                    imageVector = Icons.Default.Mic,
                    contentDescription = "Voice call",
                    tint = Color.White,
                    modifier = Modifier.size(17.dp)
                )
            }
        }
    }
}

private fun buildBannerText(state: CallUiState): String {
    return state.displayName
}
