package com.lukouguoji.gjc.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.ObservableBoolean
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityGjcWeighingListBinding
import com.lukouguoji.gjc.viewModel.GjcWeighingViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.bean.GjcWeighingBean
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
import com.lukouguoji.module_base.util.DeviceUtil

/**
 * 国际出港计重列表页
 *
 * 布局 id 不区分设备，平板/手机由资源限定符自动选择：
 *   layout-sw600dp/activity_gjc_weighing_list.xml（平板）
 *   layout/activity_gjc_weighing_list.xml（手机）
 */
@Route(path = ARouterConstants.ACTIVITY_URL_GJC_WEIGHING_LIST)
class GjcWeighingListActivity :
    BaseBindingActivity<ActivityGjcWeighingListBinding, GjcWeighingViewModel>() {

    override fun layoutId() = R.layout.activity_gjc_weighing_list

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

    /** 手机版筛选弹层显隐（仅 UI 状态，不涉及业务逻辑） */
    val filterPanelVisible = ObservableBoolean(false)

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow(if (DeviceUtil.isPhone()) "出港待计重" else "国际出港待计重")

        binding.viewModel = viewModel
        binding.activity = this

        // 手机版专属数据：放开默认日期、加载筛选弹层字典、启用三格统计
        // 平板端不调用，查询链路与请求次数与改造前完全一致
        if (DeviceUtil.isPhone()) {
            viewModel.initPhoneExtras()
        }

        // 初始化代理人列表（从API获取）
        viewModel.initAgentList()

        // 初始化特码列表（从API获取）
        viewModel.initSpecialCodeList()

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

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

        FlowBus.with<String>(ConstantEvent.EVENT_REFRESH)
            .observe(this) {
                viewModel.refresh()
            }

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

    // ==================== 手机版专用交互（纯 UI，复用既有 ViewModel 字段） ====================

    /** 手机版筛选弹层显隐切换 */
    fun toggleFilterPanel() {
        filterPanelVisible.set(!filterPanelVisible.get())
    }

    /** 手机版筛选：重置（只清弹层内条件，不影响顶部搜索框） */
    fun resetFilter() {
        viewModel.channelFilter.value = ""
        viewModel.carrierFilter.value = ""
        viewModel.awbTypeFilter.value = ""
        viewModel.agentCode.value = ""
    }

    /** 手机版筛选：确认 */
    fun confirmFilter() {
        filterPanelVisible.set(false)
        viewModel.searchClick()
    }

    /** 手机版卡片内「提前运抵」（单票，由 ViewHolder 调用） */
    fun preArriveClick(bean: GjcWeighingBean) {
        viewModel.arrivedAheadSingle(bean)
    }

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