package com.lukouguoji.gjc.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityGjcInspectionBinding
import com.lukouguoji.gjc.viewModel.GjcInspectionViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.impl.observe
import com.lukouguoji.module_base.ktx.getLifecycleOwner
import com.lukouguoji.module_base.router.ARouterConstants

/**
 * 国际出港收运检查列表页
 */
@Route(path = ARouterConstants.ACTIVITY_URL_GJC_INSPECTION)
class GjcInspectionActivity :
    BaseBindingActivity<ActivityGjcInspectionBinding, GjcInspectionViewModel>() {

    override fun layoutId() = R.layout.activity_gjc_inspection

    override fun viewModelClass() = GjcInspectionViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国际出港收运检查")

        binding.viewModel = viewModel

        // 绑定分页逻辑
        viewModel.pageModel
            .bindSmartRefreshLayout(binding.srl, binding.rv, viewModel, getLifecycleOwner())

        // 监听刷新事件
        FlowBus.with<String>(ConstantEvent.EVENT_REFRESH)
            .observe(this) {
                viewModel.refresh()
            }

        // 监听checkbox状态变化事件
        FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED)
            .observe(this) {
                viewModel.onItemCheckChanged()
            }

        // 监听全选状态变化，更新图标
        viewModel.isAllChecked.observe(this) { isAllChecked ->
            // 通过alpha值表示全选状态：全选时alpha=1.0，未全选时alpha=0.5
            binding.checkIcon.alpha = if (isAllChecked) 1.0f else 0.5f
        }

        // 初始化代理列表
        viewModel.initAgentList()

        // 初始加载
        viewModel.refresh()
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, GjcInspectionActivity::class.java)
            context.startActivity(starter)
        }
    }
}
