package com.swoosh.microblog.ui.components

import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

/**
 * Returns an appropriate icon tint color for a file based on MIME type or file name.
 * Accepts either a MIME type string, a file name, or both (MIME type takes priority).
 */
@Composable
fun fileTypeColor(mimeType: String? = null, fileName: String? = null): Color {
    // Try MIME type first
    if (mimeType != null) {
        val color = colorForMimeType(mimeType)
        if (color != null) return color
    }
    // Fall back to file extension
    if (fileName != null) {
        val color = colorForFileName(fileName)
        if (color != null) return color
    }
    return MaterialTheme.colorScheme.onSurfaceVariant
}

private fun colorForMimeType(mimeType: String): Color? = when {
    mimeType.contains("pdf") -> Color(0xFFD32F2F)
    mimeType.contains("word") || mimeType.contains("doc") -> Color(0xFF1565C0)
    mimeType.contains("text") -> Color(0xFF757575)
    mimeType.contains("spreadsheet") || mimeType.contains("excel") -> Color(0xFF2E7D32)
    mimeType.contains("presentation") || mimeType.contains("powerpoint") -> Color(0xFFE65100)
    else -> null
}

private fun colorForFileName(fileName: String): Color? {
    val lower = fileName.lowercase()
    return when {
        lower.endsWith(".pdf") -> Color(0xFFD32F2F)
        lower.endsWith(".doc") || lower.endsWith(".docx") -> Color(0xFF1565C0)
        lower.endsWith(".txt") || lower.endsWith(".csv") -> Color(0xFF757575)
        lower.endsWith(".xls") || lower.endsWith(".xlsx") -> Color(0xFF2E7D32)
        lower.endsWith(".ppt") || lower.endsWith(".pptx") -> Color(0xFFE65100)
        lower.endsWith(".zip") || lower.endsWith(".rar") || lower.endsWith(".gz") -> Color(0xFF6A1B9A)
        else -> null
    }
}
