@file:Suppress("HardCodedStringLiteral")

package eu.gaudian.translator.view.hints

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import dev.jeziellago.compose.markdowntext.MarkdownText
import eu.gaudian.translator.utils.Log

private const val TAG = "MarkdownHint"

/**
 * Sealed class representing different types of elements that can be used in a Hint.
 * Each subclass corresponds to a specific UI component or content type.
 */
sealed class HintElement {


    data class UIElement(val composable: @Composable () -> Unit) : HintElement()

    /**
     * A localized Markdown file element.
     * The file is loaded from assets based on the current device locale.
     * Follows Android's locale-qualified resource pattern:
     * - assets/hints/           - Default (English)
     * - assets/hints-de-rDE/    - German
     * - assets/hints-pt-rBR/    - Portuguese (Brazil)
     *
     * @param fileName The base filename without extension (e.g., "api_key_hint")
     */
    data class LocalizedMarkdown(val fileName: String) : HintElement()
}

/**
 * Composable to render a HintElement.
 */
@Composable
fun RenderHintElement(element: HintElement) {
    when (element) {

        is HintElement.UIElement -> {
            element.composable()
        }

        is HintElement.LocalizedMarkdown -> {
            LocalizedMarkdownContent(fileName = element.fileName)
            
            // Debug: Show filename when experimental features are enabled
            if (eu.gaudian.translator.view.LocalShowExperimentalFeatures.current) {
                androidx.compose.foundation.layout.Row(
                    modifier = Modifier.padding(top = 4.dp)
                ) {
                    Text(
                        text = "[DEBUG: ${element.fileName}]",
                        style = MaterialTheme.typography.labelSmall,
                        color = MaterialTheme.colorScheme.error
                    )
                }
            }
        }
    }
}

/**
 * Composable to render localized Markdown content.
 * Automatically loads the correct locale version based on device settings.
 * Falls back to English default if localized version is not available.
 */
@Composable
fun LocalizedMarkdownContent(
    fileName: String,
    modifier: Modifier = Modifier
) {
    val context = LocalContext.current
    val content = remember(fileName) {
        val locale = MarkdownHintLoader.getCurrentLocale(context)
        val suffix = MarkdownHintLoader.getLocaleSuffix(locale)

        // Try localized version (folder has suffix, filename doesn't)
        val localizedPath = "hints$suffix/$fileName.md"

        Log.d(TAG, "Loading hint: $fileName")
        Log.d(TAG, "Device locale: ${locale.language}_${locale.country}")
        Log.d(TAG, "Localized path: $localizedPath")

        val localized = MarkdownHintLoader.loadFromAssets(context, localizedPath)
        if (localized != null) {
            Log.d(TAG, "Found localized version at: $localizedPath")
            localized
        } else {
            // Fall back to English default in hints folder
            val defaultPath = "hints/$fileName.md"
            Log.d(TAG, "Localized not found, trying default: $defaultPath")
            val default = MarkdownHintLoader.loadFromAssets(context, defaultPath)
            if (default != null) {
                Log.d(TAG, "Found default version at: $defaultPath")
            } else {
                Log.e(TAG, "No hint found for: $fileName (tried: $localizedPath, $defaultPath)")
            }
            default
        }
    }

    Column(
        modifier = modifier
            .fillMaxWidth()
            .padding(vertical = 8.dp)
    ) {
        if (content != null) {
            MarkdownText(
                markdown = content,
                style = MaterialTheme.typography.bodyMedium.copy(
                    color = MaterialTheme.colorScheme.onSurface
                )
            )
        }
        // If content is null, nothing is rendered (empty hint)
    }
}


