package com.lukouguoji.gjc.dialog

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.DialogGjcQueryFilterBinding
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import dev.DevUtils
import dev.utils.app.info.KeyValue

/**
 * 国际出港查询筛选抽屉
 */
class GjcQueryFilterDialogModel(
    val spCode: MutableLiveData<String>,           // 特码
    val flightNo: MutableLiveData<String>,         // 航班号
    val dest: MutableLiveData<String>,             // 目的港
    val awbType: MutableLiveData<String>,          // 运单类型
    val businessType: MutableLiveData<String>,     // 业务类型
    val goodsCn: MutableLiveData<String>,          // 品名（中）
    private val onConfirm: () -> Unit
) : BaseDialogModel<DialogGjcQueryFilterBinding>(DIALOG_TYPE_DRAWER) {

    // 运单类型列表
    val awbTypeList = MutableLiveData(
        listOf(
            KeyValue("全部", ""),
            KeyValue("主单", "1"),
            KeyValue("分单", "2")
        )
    )

    // 业务类型列表
    val businessTypeList = MutableLiveData(
        listOf(
            KeyValue("全部", ""),
            KeyValue("普货", "1"),
            KeyValue("特货", "2")
        )
    )

    override fun layoutId() = R.layout.dialog_gjc_query_filter

    override fun onBuild(builder: XPopup.Builder) {
        super.onBuild(builder)
        // 设置从右边弹出
        builder.popupPosition(PopupPosition.Right)

        // 设置抽屉宽度为 Activity contentView 宽度的1/3（横屏长边）
        val activity = DevUtils.getTopActivity()
        val activityWidth = activity.window.decorView.width
        builder.maxWidth(activityWidth / 3)
        builder.popupWidth(activityWidth / 3)
    }

    override fun onDialogCreated(context: Context) {
        binding.model = this
        binding.lifecycleOwner = context as? androidx.lifecycle.LifecycleOwner

        val titleColor = Color.parseColor("#666666")

        // 设置标题
        binding.root.findViewById<TextView>(R.id.title_name)?.text = "筛选条件"
        binding.root.findViewById<TextView>(R.id.title_name)?.setTextColor(titleColor)
        binding.root.findViewById<TextView>(R.id.tool_tv_back)?.setTextColor(titleColor)
        binding.root.findViewById<ImageView>(R.id.tool_iv_back)?.imageTintList = ColorStateList.valueOf(titleColor)
        binding.root.findViewById<View>(R.id.toolbar)?.setBackgroundColor(Color.WHITE)

        // 返回按钮
        binding.root.findViewById<View>(R.id.tool_back)?.setOnClickListener {
            dismiss()
        }
    }

    /**
     * 重置筛选条件
     */
    fun onResetClick() {
        spCode.value = ""
        flightNo.value = ""
        dest.value = ""
        awbType.value = ""
        businessType.value = ""
        goodsCn.value = ""
    }

    /**
     * 确认筛选
     */
    fun onConfirmClick() {
        dismiss()
        onConfirm()
    }
}
