package dev.jasonpearson.automobile.slides.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView

/**
 * Video player slide component using ExoPlayer with proper lifecycle management. Auto-pauses when
 * navigating away from the slide.
 */
@Composable
fun VideoPlayerSlideItem(
    videoUrl: String,
    caption: String? = null,
    contentDescription: String? = null,
    modifier: Modifier = Modifier,
    captionColor: Color = MaterialTheme.colorScheme.onSurfaceVariant,
    autoPlay: Boolean = false,
) {
  val context = LocalContext.current

  val exoPlayer = remember {
    ExoPlayer.Builder(context).build().apply {
      val mediaItem = MediaItem.fromUri(videoUrl)
      setMediaItem(mediaItem)
      prepare()
      playWhenReady = autoPlay
    }
  }

  Column(
      modifier = modifier.fillMaxSize().padding(24.dp),
      verticalArrangement = Arrangement.spacedBy(16.dp),
      horizontalAlignment = Alignment.CenterHorizontally,
  ) {
    // Video player
    Card(
        modifier = Modifier.weight(1f).fillMaxWidth(),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceVariant),
        elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
    ) {
      AndroidView(
          factory = { context ->
            PlayerView(context).apply {
              player = exoPlayer
              useController = true
              setShowBuffering(PlayerView.SHOW_BUFFERING_WHEN_PLAYING)
              controllerAutoShow = true
              controllerHideOnTouch = false
              setShutterBackgroundColor(android.graphics.Color.TRANSPARENT)
            }
          },
          modifier = Modifier.fillMaxSize(),
      )
    }

    // Caption
    caption?.let {
      Text(
          text = it,
          style =
              MaterialTheme.typography.headlineSmall.copy(
                  textAlign = TextAlign.Center,
                  color = captionColor,
                  fontWeight = FontWeight.Medium,
              ),
          modifier = Modifier.padding(horizontal = 16.dp),
      )
    }
  }

  // Lifecycle management
  DisposableEffect(exoPlayer) { onDispose { exoPlayer.release() } }
}

/** Simplified video player for preview purposes. */
@Composable
private fun VideoPlayerPreview(caption: String? = null, modifier: Modifier = Modifier) {
  Column(
      modifier = modifier.fillMaxSize().padding(24.dp),
      verticalArrangement = Arrangement.spacedBy(16.dp),
      horizontalAlignment = Alignment.CenterHorizontally,
  ) {
    // Video placeholder
    Card(
        modifier = Modifier.weight(1f).fillMaxWidth(),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surfaceVariant),
        elevation = CardDefaults.cardElevation(defaultElevation = 8.dp),
    ) {
      Column(
          modifier = Modifier.fillMaxSize(),
          horizontalAlignment = Alignment.CenterHorizontally,
          verticalArrangement = Arrangement.Center,
      ) {
        Text(
            text = "🎬",
            style = MaterialTheme.typography.displayLarge,
            modifier = Modifier.padding(bottom = 16.dp),
        )
        Text(
            text = "Video Player",
            style = MaterialTheme.typography.headlineMedium,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
        )
      }
    }

    // Caption
    caption?.let {
      Text(
          text = it,
          style =
              MaterialTheme.typography.headlineSmall.copy(
                  textAlign = TextAlign.Center,
                  color = MaterialTheme.colorScheme.onSurfaceVariant,
                  fontWeight = FontWeight.Medium,
              ),
          modifier = Modifier.padding(horizontal = 16.dp),
      )
    }
  }
}

@Preview(showBackground = true)
@Composable
fun VideoPlayerSlideItemPreview() {
  MaterialTheme { VideoPlayerPreview(caption = "AutoMobile Demo: Testing a Shopping App") }
}

@Preview(showBackground = true)
@Composable
fun VideoPlayerSlideItemNoCaptionPreview() {
  MaterialTheme { VideoPlayerPreview() }
}
