package com.rosetta.messenger.ui.components

import androidx.compose.foundation.clickable
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.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import com.rosetta.messenger.R
import com.rosetta.messenger.ui.onboarding.PrimaryBlue
import com.rosetta.messenger.ui.theme.LocalRosettaIsDarkTheme

/**
 * Значок верификации пользователя
 * Аналогичен VerifiedBadge в React Native приложении
 * 
 * @param verified Уровень верификации (0 = нет, 1 = стандартная, 2+ = особая)
 * @param size Размер значка в dp
 * @param isDarkTheme Тема приложения (если не передано - вычисляется из MaterialTheme)
 */
@Composable
fun VerifiedBadge(
    verified: Int,
    size: Int = 16,
    modifier: Modifier = Modifier,
    isDarkTheme: Boolean = LocalRosettaIsDarkTheme.current,
    badgeTint: Color? = null
) {
    if (verified <= 0) return
    
    var showDialog by remember { mutableStateOf(false) }
    val inlineOffsetY: Dp = when {
        size <= 16 -> (-0.5).dp
        size <= 20 -> (-1).dp
        else -> 0.dp
    }
    
    // Цвет верификации: в тёмной теме — как индикаторы прочтения (PrimaryBlue), в светлой — #ACD2F9
    val badgeColor =
        badgeTint ?: PrimaryBlue
    
    // Текст аннотации
    val annotationText = when (verified) {
        1 -> "This is an official account belonging to a public figure, brand, or organization."
        2 -> "This is official account belonging to administration of Rosetta."
        else -> "This user is administrator of this group."
    }

    val badgeIconRes = if (verified == 3) {
        R.drawable.ic_arrow_badge_down_filled
    } else {
        R.drawable.ic_rosette_discount_check
    }
    
    Icon(
        painter = painterResource(id = badgeIconRes),
        contentDescription = "Verified",
        tint = badgeColor,
        modifier = modifier
            .offset(y = inlineOffsetY)
            .size(size.dp)
            .clickable { showDialog = true }
    )
    
    // Диалог с аннотацией
    if (showDialog) {
        Dialog(onDismissRequest = { showDialog = false }) {
            Surface(
                shape = RoundedCornerShape(12.dp),
                color = if (isDarkTheme) Color(0xFF2A2A2A) else Color.White,
                tonalElevation = 8.dp
            ) {
                Column(
                    modifier = Modifier.padding(16.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    Icon(
                        painter = painterResource(id = badgeIconRes),
                        contentDescription = null,
                        tint = badgeColor,
                        modifier = Modifier.size(32.dp)
                    )
                    Spacer(modifier = Modifier.height(12.dp))
                    Text(
                        text = annotationText,
                        fontSize = 14.sp,
                        color = if (isDarkTheme) Color.White.copy(alpha = 0.85f) else Color.Black.copy(alpha = 0.85f),
                        lineHeight = 20.sp,
                        textAlign = androidx.compose.ui.text.style.TextAlign.Center
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    TextButton(onClick = { showDialog = false }) {
                        Text("OK", color = if (isDarkTheme) Color.White else Color(0xFF1DA1F2))
                    }
                }
            }
        }
    }
}
