package dev.adriankuta.visualizer.view

import android.Manifest
import android.content.pm.PackageManager
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
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.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.adriankuta.visualizer.AudioVisualizerFactory
import dev.adriankuta.visualizer.AudioVisualizerService
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import timber.log.Timber


@Composable
fun VisualizerScreen(
    viewModel: VisualizerScreenViewModel = hiltViewModel()
) {

    val uiState by viewModel.uiState.collectAsStateWithLifecycle()

}
@Composable
fun VisualizerScreen(
    modifier: Modifier = Modifier,
    audioVisualizer: AudioVisualizerService = remember {
        AudioVisualizerFactory.createDefault(
            CoroutineScope(Dispatchers.Main + SupervisorJob())
        )
    }
) {
    val context = LocalContext.current
    val lifecycleOwner = LocalLifecycleOwner.current
    val currentColor by audioVisualizer.currentColor
    val isActive by audioVisualizer.isActive

    // Permission state management
    var hasAudioPermission by remember {
        mutableStateOf(
            ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.RECORD_AUDIO
            ) == PackageManager.PERMISSION_GRANTED
        )
    }

    var showPermissionRationale by remember { mutableStateOf(false) }

    // Permission request launcher
    val permissionLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.RequestPermission()
    ) { isGranted ->
        hasAudioPermission = isGranted
        if (isGranted) {
            Timber.d("Audio permission granted, starting visualizer")
            audioVisualizer.start()
        } else {
            Timber.w("Audio permission denied")
            showPermissionRationale = true
        }
    }

    // Function to start audio visualizer with permission check
    fun startVisualizerWithPermissionCheck() {
        when {
            hasAudioPermission -> {
                audioVisualizer.start()
            }
            else -> {
                Timber.d("Requesting audio permission")
                permissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
            }
        }
    }

    // Handle lifecycle events
    DisposableEffect(lifecycleOwner) {
        val observer = LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_START -> startVisualizerWithPermissionCheck()
                Lifecycle.Event.ON_STOP -> audioVisualizer.stop()
                else -> {}
            }
        }

        lifecycleOwner.lifecycle.addObserver(observer)

        onDispose {
            lifecycleOwner.lifecycle.removeObserver(observer)
            audioVisualizer.release()
        }
    }

    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        // Title
        Text(
            text = "Audio Color Visualizer",
            fontSize = 24.sp,
            fontWeight = FontWeight.Bold,
            textAlign = TextAlign.Center,
            modifier = Modifier.padding(bottom = 32.dp)
        )

        // Color display area
        Box(
            modifier = Modifier
                .size(300.dp)
                .clip(RoundedCornerShape(16.dp))
                .background(currentColor)
                .padding(16.dp),
            contentAlignment = Alignment.Center
        ) {
            // Show color information
            ColorInfoCard(
                color = currentColor,
                isActive = isActive
            )
        }

        Spacer(modifier = Modifier.height(32.dp))

        // Status indicator
        StatusIndicator(
            isActive = isActive,
            hasPermission = hasAudioPermission
        )

        Spacer(modifier = Modifier.height(16.dp))

        // Instructions
        Text(
            text = when {
                !hasAudioPermission -> "Audio permission is required to visualize sound"
                isActive -> "Play some music or make sounds to see the colors change!"
                else -> "Starting audio visualizer..."
            },
            fontSize = 16.sp,
            textAlign = TextAlign.Center,
            color = MaterialTheme.colorScheme.onSurfaceVariant,
            modifier = Modifier.padding(horizontal = 32.dp)
        )

        // Permission request button (shown when permission is denied)
        if (!hasAudioPermission) {
            Spacer(modifier = Modifier.height(16.dp))

            Button(
                onClick = {
                    showPermissionRationale = false
                    permissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
                },
                modifier = Modifier.padding(horizontal = 32.dp)
            ) {
                Text("Grant Audio Permission")
            }
        }
    }

    // Permission rationale dialog
    if (showPermissionRationale) {
        AlertDialog(
            onDismissRequest = { showPermissionRationale = false },
            title = { Text("Audio Permission Required") },
            text = { 
                Text("This app needs access to your microphone to visualize audio and create colors based on sound. Please grant the audio recording permission to continue.")
            },
            confirmButton = {
                TextButton(
                    onClick = {
                        showPermissionRationale = false
                        permissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
                    }
                ) {
                    Text("Grant Permission")
                }
            },
            dismissButton = {
                TextButton(
                    onClick = { showPermissionRationale = false }
                ) {
                    Text("Cancel")
                }
            }
        )
    }
}

/**
 * Card displaying color information.
 */
@Composable
private fun ColorInfoCard(
    color: Color,
    isActive: Boolean,
    modifier: Modifier = Modifier
) {
    Card(
        modifier = modifier,
        colors = CardDefaults.cardColors(
            containerColor = Color.White.copy(alpha = 0.9f)
        ),
        elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
    ) {
        Column(
            modifier = Modifier.padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Current Color",
                fontSize = 14.sp,
                fontWeight = FontWeight.Medium,
                color = Color.Black
            )

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

            // RGB values
            val (r, g, b) = with(color) {
                Triple(
                    (red * 255).toInt(),
                    (green * 255).toInt(),
                    (blue * 255).toInt()
                )
            }

            Text(
                text = "RGB($r, $g, $b)",
                fontSize = 12.sp,
                color = Color.Black.copy(alpha = 0.7f),
                fontWeight = FontWeight.Light
            )
        }
    }
}

/**
 * Status indicator showing whether the visualizer is active and permission status.
 */
@Composable
private fun StatusIndicator(
    isActive: Boolean,
    hasPermission: Boolean,
    modifier: Modifier = Modifier
) {
    Row(
        modifier = modifier,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Box(
            modifier = Modifier
                .size(12.dp)
                .clip(RoundedCornerShape(6.dp))
                .background(
                    when {
                        !hasPermission -> Color.Gray
                        isActive -> Color.Green
                        else -> Color.Red
                    }
                )
        )

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

        Text(
            text = when {
                !hasPermission -> "Permission Required"
                isActive -> "Active"
                else -> "Inactive"
            },
            fontSize = 14.sp,
            color = MaterialTheme.colorScheme.onSurface
        )
    }
}
