package com.adealink.weparty.commonui.widget.operatebox

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.IdRes


data class OperateBoxItem<V1 : View, V2 : View>(
    @IdRes val id: Int, /*item唯一标识*/
    val itemView: () -> V1,

    var extraView: (() -> V2?)? = null //扩展控件
)

class OperateBoxItemView<V1 : View, V2 : View> @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : FrameLayout(context, attrs, defStyleAttr) {

    private var itemView: V1? = null
    private var extraView: V2? = null

    fun setItem(item: OperateBoxItem<V1, V2>, itemWidth: Int, itemHeight: Int) {
        removeAllViews()
        val itemView = item.itemView.invoke().also {
            this@OperateBoxItemView.itemView = it
        }
        addView(itemView, LayoutParams(itemWidth, itemHeight).apply {
            gravity = Gravity.CENTER
        })

        val extraView = item.extraView?.invoke().also {
            this@OperateBoxItemView.extraView = it
        }
        extraView?.let {
            addView(extraView)
        }
    }

    fun getItemView(): V1? {
        return itemView
    }

    fun getExtraView(): V2? {
        return extraView
    }
}