package com.example.defenseapplication.view

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.DialogContainerSelectBinding

class ContainerSelectDialog(context: Context) {

    private val dialog: Dialog = Dialog(context)
    private val binding: DialogContainerSelectBinding
    private var selectedContainer: String = "容器4"
    private var onConfirmListener: ((String) -> Unit)? = null

    init {
        binding = DialogContainerSelectBinding.inflate(LayoutInflater.from(context))
        dialog.setContentView(binding.root)
        dialog.window?.apply {
            setGravity(Gravity.BOTTOM)
            setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }

        initListeners()
    }

    private fun initListeners() {
        binding.btnCancel.setOnClickListener {
            dialog.dismiss()
        }

        binding.btnConfirm.setOnClickListener {
            onConfirmListener?.invoke(selectedContainer)
            dialog.dismiss()
        }

        binding.itemContainer1.setOnClickListener {
            selectContainer("容器1")
        }

        binding.itemContainer2.setOnClickListener {
            selectContainer("容器2")
        }

        binding.itemContainer3.setOnClickListener {
            selectContainer("容器3")
        }

        binding.itemContainer4.setOnClickListener {
            selectContainer("容器4")
        }
    }

    private fun selectContainer(container: String) {
        selectedContainer = container
        updateSelectedStyle()
    }

    private fun updateSelectedStyle() {
        val bgSelected = R.drawable.bg_bottom_sheet_rounded
        val bgNormal = android.R.color.transparent

        binding.itemContainer1.setBackgroundResource(if (selectedContainer == "容器1") bgSelected else bgNormal)
        binding.itemContainer2.setBackgroundResource(if (selectedContainer == "容器2") bgSelected else bgNormal)
        binding.itemContainer3.setBackgroundResource(if (selectedContainer == "容器3") bgSelected else bgNormal)
        binding.itemContainer4.setBackgroundResource(if (selectedContainer == "容器4") bgSelected else bgNormal)

        val textViews = listOf(binding.itemContainer1.getChildAt(0) as android.widget.TextView,
            binding.itemContainer2.getChildAt(0) as android.widget.TextView,
            binding.itemContainer3.getChildAt(0) as android.widget.TextView,
            binding.itemContainer4.getChildAt(0) as android.widget.TextView)

        textViews.forEach { tv ->
            tv.setTextColor(if (tv.text == selectedContainer) dialog.context.getColor(R.color.black) else dialog.context.getColor(R.color.gray))
        }
    }

    fun setSelectedContainer(container: String) {
        selectedContainer = container
        updateSelectedStyle()
    }

    fun setOnConfirmListener(listener: (String) -> Unit) {
        this.onConfirmListener = listener
    }

    fun show() {
        updateSelectedStyle()
        dialog.show()
    }

    fun dismiss() {
        dialog.dismiss()
    }
}