package com.example.godeye.ui.components

import android.util.Log
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.example.godeye.R
import com.example.godeye.models.CameraRequest
import com.example.godeye.utils.Constants

/**
 * Диалог запроса доступа к камере от оператора
 */
@Composable
fun CameraRequestDialog(
    request: CameraRequest,
    onAccept: () -> Unit,
    onDeny: () -> Unit,
    onDismiss: () -> Unit
) {
    var rememberChoice by remember { mutableStateOf(false) }

    // Лог открытия диалога
    LaunchedEffect(Unit) {
        Log.d("GodEye", "CameraRequestDialog открыт: sessionId=${request.sessionId}, operatorId=${request.operatorId}, cameraType=${request.cameraType}")
    }

    Dialog(onDismissRequest = {
        Log.d("GodEye", "Диалог закрыт пользователем")
        onDismiss()
    }) {
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            shape = RoundedCornerShape(16.dp),
            elevation = CardDefaults.cardElevation(defaultElevation = 8.dp)
        ) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(24.dp),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // Иконка камеры
                Icon(
                    imageVector = Icons.Default.Person, // Заменено с PhotoCamera на Person
                    contentDescription = null,
                    modifier = Modifier.size(48.dp),
                    tint = MaterialTheme.colorScheme.primary
                )

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

                // Заголовок
                Text(
                    text = stringResource(R.string.camera_request_title),
                    style = MaterialTheme.typography.headlineSmall,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center
                )

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

                // Основное сообщение
                Text(
                    text = stringResource(
                        R.string.camera_request_message,
                        request.operatorId,
                        getCameraTypeName(request.cameraType)
                    ),
                    style = MaterialTheme.typography.bodyLarge,
                    textAlign = TextAlign.Center
                )

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

                // ID сессии
                Text(
                    text = stringResource(R.string.session_id_label, request.sessionId),
                    style = MaterialTheme.typography.bodyMedium,
                    color = MaterialTheme.colorScheme.onSurfaceVariant,
                    textAlign = TextAlign.Center
                )

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

                // Чекбокс "Запомнить выбор"
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Checkbox(
                        checked = rememberChoice,
                        onCheckedChange = { rememberChoice = it }
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Text(
                        text = stringResource(R.string.remember_choice),
                        style = MaterialTheme.typography.bodyMedium
                    )
                }

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

                // Кнопки
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.spacedBy(12.dp)
                ) {
                    // Кнопка "Отклонить"
                    OutlinedButton(
                        onClick = {
                            Log.d("GodEye", "Пользователь отклонил запрос камеры")
                            onDeny()
                        },
                        modifier = Modifier
                            .weight(1f)
                            .height(48.dp)
                    ) {
                        Text(stringResource(R.string.deny_button))
                    }

                    // Кнопка "Разрешить"
                    Button(
                        onClick = {
                            Log.d("GodEye", "Пользователь разрешил доступ к камере")
                            onAccept()
                        },
                        modifier = Modifier
                            .weight(1f)
                            .height(48.dp)
                    ) {
                        Text(stringResource(R.string.allow_button))
                    }
                }
            }
        }
    }
}

/**
 * Получить локализованное название типа камеры
 */
@Composable
private fun getCameraTypeName(cameraType: String): String {
    return when (cameraType) {
        Constants.CameraTypes.BACK -> stringResource(R.string.camera_type_back)
        Constants.CameraTypes.FRONT -> stringResource(R.string.camera_type_front)
        Constants.CameraTypes.WIDE -> stringResource(R.string.camera_type_wide)
        Constants.CameraTypes.TELEPHOTO -> stringResource(R.string.camera_type_telephoto)
        else -> cameraType
    }
}
