package com.rosetta.messenger.ui.components.metaball

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color as ComposeColor
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView

/**
 * Compose wrapper around Java [ProfileGooeyView] — the 1:1 Telegram port.
 *
 * Uses AndroidView to embed the View-based gooey effect inside Compose layout.
 * The avatar content is rendered via a nested ComposeView inside ProfileGooeyView.
 *
 * @param collapseProgress 0 = fully expanded, 1 = fully collapsed
 * @param expansionProgress 0 = normal, 1 = pulled down (overscroll)
 * @param statusBarHeight status bar height in dp
 * @param headerHeight current header height in dp
 * @param hasAvatar whether user has an avatar image
 * @param avatarColor fallback background color for avatar
 * @param modifier modifier for the container
 * @param avatarContent composable content to draw inside the gooey avatar
 */
@Composable
fun ProfileGooeyEffect(
    collapseProgress: Float,
    expansionProgress: Float,
    statusBarHeight: Dp,
    headerHeight: Dp,
    hasAvatar: Boolean,
    avatarColor: ComposeColor,
    modifier: Modifier = Modifier,
    avatarContent: @Composable BoxScope.() -> Unit = {},
) {
    val density = LocalDensity.current

    // Convert Dp to px for the View layer
    val statusBarPx = with(density) { statusBarHeight.toPx() }
    val headerPx = with(density) { headerHeight.toPx() }
    val avatarSizeDp = 100f // matches AVATAR_SIZE_DP in ProfileGooeyView
    val avatarSizePx = with(density) { avatarSizeDp.dp.toPx() }.toInt()

    // Compute avatar position/scale based on collapse progress  
    val expandedHeaderDp = 300f // should match EXPANDED_HEADER_HEIGHT from ProfileScreen
    val collapsedHeaderDp = 56f // should match COLLAPSED_HEADER_HEIGHT  
    val contentAreaDp = expandedHeaderDp - 70f
    
    val avatarExpandedYDp = statusBarHeight + ((contentAreaDp - avatarSizeDp).dp / 2)
    val avatarCollapsedYDp = statusBarHeight + ((collapsedHeaderDp - 30f).dp / 2) // collapsed avatar ~30dp
    val avatarYDp = androidx.compose.ui.unit.lerp(avatarExpandedYDp, avatarCollapsedYDp, collapseProgress)
    val avatarYPx = with(density) { avatarYDp.toPx() }.toInt()

    // Avatar scale: 1.0 at expanded, ~0.3 at collapsed
    val avatarScale = androidx.compose.ui.unit.lerp(1f.dp, 0.3f.dp, collapseProgress)
    val avatarScaleFloat = avatarScale.value

    AndroidView(
        modifier = modifier,
        factory = { context ->
            ProfileGooeyView(context).apply {
                // Add a child ComposeView that renders the avatar content
                val composeChild = ComposeView(context).apply {
                    layoutParams = FrameLayout.LayoutParams(
                        avatarSizePx,
                        avatarSizePx,
                        Gravity.CENTER_HORIZONTAL or Gravity.TOP
                    ).apply {
                        topMargin = avatarYPx
                    }
                }
                addView(composeChild)

                setGooeyEnabled(true)
                setIntensity(15f)
            }
        },
        update = { gooeyView ->
            // Update gooey parameters each recomposition
            val blurIntensity = collapseProgress.coerceIn(0f, 1f)
            gooeyView.setBlurIntensity(blurIntensity)
            gooeyView.setPullProgress(expansionProgress)

            // Update child position and scale
            val child = gooeyView.getChildAt(0)
            if (child != null) {
                val lp = child.layoutParams as FrameLayout.LayoutParams
                lp.topMargin = avatarYPx
                lp.width = avatarSizePx
                lp.height = avatarSizePx
                child.layoutParams = lp
                child.scaleX = avatarScaleFloat
                child.scaleY = avatarScaleFloat
                child.pivotX = avatarSizePx / 2f
                child.pivotY = avatarSizePx / 2f

                // Update ComposeView content
                if (child is ComposeView) {
                    child.setContent {
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .background(avatarColor),
                            contentAlignment = Alignment.Center,
                            content = avatarContent
                        )
                    }
                }
            }

            gooeyView.invalidate()
        }
    )
}
