package com.example.defenseapplication.view

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

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

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

        val window = window
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.BOTTOM
        window?.attributes = params
        window?.setBackgroundDrawableResource(android.R.color.transparent)
        window?.setDimAmount(0.5f)

        initViews()
    }

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

        val currentLang = LanguageUtil.currentLanguage

        if (currentLang == AppLanguage.SIMPLIFIED_CHINESE) {
            tvChinese.setTextColor(context.getColor(R.color.green))
        } else {
            tvEnglish.setTextColor(context.getColor(R.color.green))
        }

        llChinese.setOnClickListener {
            onLanguageSelected(AppLanguage.SIMPLIFIED_CHINESE)
            dismiss()
        }

        llEnglish.setOnClickListener {
            onLanguageSelected(AppLanguage.ENGLISH)
            dismiss()
        }

        tvCancel.setOnClickListener {
            dismiss()
        }
    }
}
