package com.dano.test1

import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.*

object LocalizationHelper {

    /**
     * Extension function to set localized hint on EditText.
     */
    fun EditText.setLocalizedHint(languageID: String, key: String) {
        this.hint = LanguageManager.getText(languageID, key)
    }

    /**
     * Extension function to set localized text on Button.
     */
    fun Button.setLocalizedText(languageID: String, key: String) {
        this.text = LanguageManager.getText(languageID, key)
    }

    /**
     * Recursively localizes the view hierarchy by updating text or hint
     * of views according to their tag as localization key.
     * @param root The root view to start localization.
     * @param languageID The language identifier for localization.
     */
    fun localizeViewTree(root: View, languageID: String) {
        val key = root.tag as? String

        when (root) {
            is EditText -> {
                // Set hint for EditText if tag key is present
                key?.let { root.setLocalizedHint(languageID, it) }
            }
            is Button -> {
                // Set text for Button if tag key is present
                key?.let { root.setLocalizedText(languageID, it) }
            }
            is TextView -> {
                // Set text for TextView if tag key is present
                // Note: Button and others inherit from TextView, but handled separately above
                if (root !is Button) key?.let { root.text = LanguageManager.getText(languageID, it) }
            }
            is RadioButton -> {
                // Set text for RadioButton if tag key is present
                key?.let { root.text = LanguageManager.getText(languageID, it) }
            }
            is CheckBox -> {
                // Set text for CheckBox if tag key is present
                key?.let { root.text = LanguageManager.getText(languageID, it) }
            }
            is ViewGroup -> {
                // Recursively localize all child views
                for (i in 0 until root.childCount) {
                    localizeViewTree(root.getChildAt(i), languageID)
                }
            }
        }
    }
}
