package com.example.defenseapplication.view

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.Window
import android.widget.LinearLayout
import android.widget.TextView
import com.example.defenseapplication.R
import com.example.defenseapplication.utils.LanguageUtil

/**
 * 语言选择对话框
 */
class LanguageDialog(
    context: Context,
    private val onLanguageSelected: (String) -> Unit
) : Dialog(context) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.dialog_language)

        // 设置对话框宽度为屏幕宽度的80%
        window?.setLayout(
            (context.resources.displayMetrics.widthPixels * 0.8).toInt(),
            LinearLayout.LayoutParams.WRAP_CONTENT
        )

        initViews()
    }

    private fun initViews() {
        val tvChinese = findViewById<TextView>(R.id.tvChinese)
        val tvEnglish = findViewById<TextView>(R.id.tvEnglish)
        val tvCancel = findViewById<TextView>(R.id.tvCancel)

        val currentLang = LanguageUtil.getCurrentLanguage(context)

        // 根据当前语言高亮显示
        if (currentLang == LanguageUtil.LANGUAGE_CHINESE) {
            tvChinese.setTextColor(context.getColor(R.color.green))
        } else {
            tvEnglish.setTextColor(context.getColor(R.color.green))
        }

        tvChinese.setOnClickListener {
            onLanguageSelected(LanguageUtil.LANGUAGE_CHINESE)
            dismiss()
        }

        tvEnglish.setOnClickListener {
            onLanguageSelected(LanguageUtil.LANGUAGE_ENGLISH)
            dismiss()
        }

        tvCancel.setOnClickListener {
            dismiss()
        }
    }
}
