package com.lukouguoji.module_base.ui.weight.phone

import android.content.Context
import android.os.Parcelable
import android.util.AttributeSet
import android.util.SparseArray
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.lukouguoji.module_base.R

/**
 * 手机版底部操作条（5.5 寸手持端公共组件）
 *
 * 结构：全选（圆形勾选框 + 文字）+ 已选统计 + 主操作按钮。
 * 对应设计稿各多选列表页底部的白色操作条。
 *
 * XML 用法：
 * ```xml
 * <com.lukouguoji.module_base.ui.weight.phone.PhoneBottomBar
 *     android:layout_width="match_parent"
 *     android:layout_height="wrap_content"
 *     allChecked="@{viewModel.isAllChecked}"
 *     countText='@{"已选 " + viewModel.selectedCount + " 项"}'
 *     actionText="@{`交接`}"
 *     setOnAllCheckClick="@{(v)-> viewModel.checkAllClick()}"
 *     setOnActionClick="@{(v)-> viewModel.completeHandover()}" />
 * ```
 */
class PhoneBottomBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    val llCheckAll: LinearLayout
    val ivCheckAll: ImageView
    val tvCount: TextView
    val tvAction: TextView

    /** 全选状态（驱动勾选框图标切换） */
    var allChecked: Boolean = false
        set(v) {
            field = v
            ivCheckAll.setImageResource(
                if (v) R.drawable.ic_phone_check_round_checked
                else R.drawable.ic_phone_check_round_unchecked
            )
        }

    /** 已选统计文案，如「已选 3 项」 */
    var countText: String = ""
        set(v) {
            field = v
            tvCount.text = v
        }

    /** 主按钮文字 */
    var actionText: String = ""
        set(v) {
            field = v
            tvAction.text = v
        }

    /**
     * 危险操作样式：浅红底 + 红字 + 垃圾桶图标（如 ULD 管理的批量删除）。
     * 默认 false 为主色实底按钮，既有页面不受影响。
     */
    var actionDanger: Boolean = false
        set(v) {
            field = v
            if (v) {
                tvAction.setBackgroundResource(R.drawable.bg_phone_btn_danger)
                tvAction.setTextColor(ContextCompat.getColor(context, R.color.phone_danger_text))
                val icon = ContextCompat.getDrawable(context, R.drawable.ic_phone_trash)?.apply {
                    val size = (16 * resources.displayMetrics.density).toInt()
                    setBounds(0, 0, size, size)
                }
                tvAction.setCompoundDrawables(icon, null, null, null)
                tvAction.compoundDrawablePadding = (4 * resources.displayMetrics.density).toInt()
            } else {
                tvAction.setBackgroundResource(R.drawable.bg_phone_btn_primary)
                tvAction.setTextColor(ContextCompat.getColor(context, R.color.white))
                tvAction.setCompoundDrawables(null, null, null, null)
            }
        }

    init {
        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
        setBackgroundColor(ContextCompat.getColor(context, R.color.white))
        elevation = 8 * resources.displayMetrics.density
        val padH = (16 * resources.displayMetrics.density).toInt()
        val padV = (14 * resources.displayMetrics.density).toInt()
        setPadding(padH, padV, padH, padV)

        inflate(context, R.layout.layout_phone_bottom_bar, this)

        llCheckAll = findViewById(R.id.ll_check_all)
        ivCheckAll = findViewById(R.id.iv_check_all)
        tvCount = findViewById(R.id.tv_count)
        tvAction = findViewById(R.id.tv_action)
    }

    /** 只保存/恢复自身状态，避免内部子控件 id 与其他复合组件实例串档 */
    override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>) {
        dispatchFreezeSelfOnly(container)
    }

    override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>) {
        dispatchThawSelfOnly(container)
    }
}
