package com.lukouguoji.aerologic.page.gnj.move.stash.list

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.aerologic.R
import com.lukouguoji.aerologic.databinding.ActivityGnjMoveStashListBinding
import com.lukouguoji.aerologic.page.gnj.move.stash.photo.GnjMoveStashPhotoActivity
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.bean.MoveStashBean
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

@Route(path = ARouterConstants.ACTIVITY_URL_GNJ_MOVE_STASH_LIST)
class GnjMoveStashListActivity :
    BaseBindingActivity<ActivityGnjMoveStashListBinding, GnjMoveStashListViewModel>() {
    override fun layoutId() = R.layout.activity_gnj_move_stash_list

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

    /** 筛选弹层显隐（仅手机 UI 状态，平板布局不绑定） */
    val filterPanelVisible = ObservableBoolean(false)

    override fun initOnCreate(savedInstanceState: Bundle?) {
        // 设计稿手机端标题为「进港移库」，平板保持原标题
        setBackArrow(if (DeviceUtil.isPhone()) "进港移库" else "国内进港移库")

        binding.viewModel = viewModel
        binding.activity = this

        viewModel.pageModel
            .bindSmartRefreshLayout(binding.srl, binding.rv, viewModel, getLifecycleOwner())

        if (DeviceUtil.isPhone()) {
            // 手机专属：Tab 角标 + 特码字典等额外请求
            viewModel.initPhoneExtras()

            // 卡片勾选变化 → 更新全选状态与已选计数
            FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED).observe(this) {
                viewModel.updateCheckAllStatus()
            }
        }

        // 拍照上传等子页面完成后刷新列表（平板无发射方，观察无副作用）
        FlowBus.with<String>(ConstantEvent.EVENT_REFRESH).observe(this) {
            viewModel.refresh()
        }

        viewModel.refresh()
    }

    // ==================== 筛选弹层（仅手机） ====================

    fun toggleFilterPanel() {
        filterPanelVisible.set(!filterPanelVisible.get())
    }

    /** 筛选：重置（只清空弹层内条件，不影响 Tab 与顶部搜索框） */
    fun resetFilter() {
        viewModel.flightDate.value = ""
        viewModel.flightNo.value = ""
        viewModel.spCode.value = ""
        viewModel.businessType.value = ""
    }

    fun confirmFilter() {
        filterPanelVisible.set(false)
        viewModel.refresh()
    }

    // ==================== 卡片长按「拍照」浮层（仅手机） ====================

    /** 卡片长按：展开操作浮层（View.OnLongClickListener 要求返回 Boolean） */
    fun showCardActionPhone(bean: MoveStashBean): Boolean {
        bean.actionMenuVisible.set(true)
        return true
    }

    /** 点浮层空白处关闭 */
    fun hideCardActionPhone(bean: MoveStashBean) {
        bean.actionMenuVisible.set(false)
    }

    /** 浮层「拍照」→ 拍照上传页 */
    fun openPhoto(bean: MoveStashBean) {
        bean.actionMenuVisible.set(false)
        GnjMoveStashPhotoActivity.start(this, bean.mawbId, bean.wbNo)
    }

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

}
