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.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.defenseapplication.R
import com.example.defenseapplication.adapter.FamilySwitchAdapter
import com.example.defenseapplication.model.FamilyItem

/**
 * 家庭切换对话框
 */
class FamilySwitchDialog(
    context: Context,
    private val onFamilySelected: (String) -> Unit
) : Dialog(context, R.style.BottomDialogStyle) {

    private lateinit var adapter: FamilySwitchAdapter

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

        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 tvCancel = findViewById<TextView>(R.id.tvCancel)
        val tvConfirm = findViewById<TextView>(R.id.tvConfirm)
        val rvMessageList = findViewById<RecyclerView>(R.id.rvMessageList)

        val familyList = listOf(
            FamilyItem("1", context.getString(R.string.my_family)),
            FamilyItem("2", "张三的家庭")
        )

        adapter = FamilySwitchAdapter(familyList) { familyItem ->
        }

        rvMessageList.layoutManager = LinearLayoutManager(context)
        rvMessageList.adapter = adapter

        tvCancel.setOnClickListener {
            dismiss()
        }

        tvConfirm.setOnClickListener {
            val selectedFamily = adapter.getSelectedItem()
            onFamilySelected(selectedFamily.name)
            dismiss()
        }
    }
}