package rasel.lunar.launcher.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.View.OnClickListener
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.TableLayout
import android.widget.TableRow


class TableRadioGroup : TableLayout {
    var checkedRadioButtonId: Int = -1
        private set
    private var onCheckedChangeListener: OnCheckedChangeListener? = null
    private var maxColumns = -1 // Maximum number of columns (-1 for unlimited)
    private var maxRows = -1 // Maximum number of rows (-1 for unlimited)

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)

    fun setMaxColumns(maxColumns: Int) {
        this.maxColumns = maxColumns
    }

    fun setMaxRows(maxRows: Int) {
        this.maxRows = maxRows
    }

    override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams) {
        if (child is TableRow) {
            val numChildren = child.childCount
            for (i in 0 until numChildren) {
                val view = child.getChildAt(i)
                if (view is RadioButton) {
                    setupRadioButton(view)
                }
            }
        }
        super.addView(child, index, params)

        // Adjust the number of rows and columns if necessary
        adjustTableLayout()
    }

    private fun setupRadioButton(radioButton: RadioButton?) {
        if (radioButton == null) {
            return
        }

        radioButton.setOnClickListener(OnClickListener {
            if (checkedRadioButtonId != -1) {
                setCheckedStateForView(checkedRadioButtonId, false)
            }
            val id = radioButton.id
            setCheckedStateForView(id, true)
            checkedRadioButtonId = id
            if (onCheckedChangeListener != null) {
                onCheckedChangeListener!!.onCheckedChanged(
                    this@TableRadioGroup,
                    checkedRadioButtonId
                )
            }
        })
    }

    private fun setCheckedStateForView(viewId: Int, checked: Boolean) {
        val checkedView = findViewById<View>(viewId)
        if (checkedView != null && checkedView is RadioButton) {
            checkedView.isChecked = checked
        }
    }

    fun setOnCheckedChangeListener(listener: OnCheckedChangeListener?) {
        onCheckedChangeListener = listener
    }

    fun clearCheck() {
        setCheckedStateForView(checkedRadioButtonId, false)
        checkedRadioButtonId = -1
    }

    private fun adjustTableLayout() {
        if (maxColumns > 0 && maxRows > 0) {
            val childCount = childCount
            var rowCount = 0
            var currentRow: TableRow? = null
            for (i in 0 until childCount) {
                val child = getChildAt(i)
                if (child is TableRow) {
                    currentRow = child
                    rowCount++
                } else if (currentRow != null) {
                    val columnCount = currentRow.childCount
                    if (columnCount >= maxColumns) {
                        // Create a new row if the maximum column count is reached
                        if (rowCount < maxRows) {
                            val newRow = TableRow(context)
                            super.addView(newRow, getChildIndex(currentRow) + 1)
                            currentRow = newRow
                            rowCount++
                        } else {
                            // Remove extra children that exceed the maximum row count
                            removeView(child)
                            continue
                        }
                    }
                }
            }
        }
    }

    private fun getChildIndex(child: View): Int {
        val childCount = childCount
        for (i in 0 until childCount) {
            if (getChildAt(i) === child) {
                return i
            }
        }
        return -1
    }

    interface OnCheckedChangeListener {
        fun onCheckedChanged(group: TableRadioGroup?, checkedId: Int)
    }
}