package com.lukouguoji.gjc.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityGjcWeighingRecordDetailsBinding
import com.lukouguoji.gjc.holder.GjcCheckInRecordViewHolder
import com.lukouguoji.gjc.viewModel.GjcWeighingRecordDetailsViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.GjcCheckInRecord
import com.lukouguoji.module_base.bean.GjcWeighingRecordBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.DeviceUtil

/**
 * 国际出港计重明细页
 *
 * 布局 id 不区分设备，平板/手机由资源限定符自动选择：
 *   layout-sw600dp/activity_gjc_weighing_record_details.xml（平板，列表内联编辑）
 *   layout/activity_gjc_weighing_record_details.xml（手机，单条记录经全屏「修改记录」覆层编辑）
 */
@Route(path = ARouterConstants.ACTIVITY_URL_GJC_WEIGHING_RECORD_DETAILS)
class GjcWeighingRecordDetailsActivity :
    BaseBindingActivity<ActivityGjcWeighingRecordDetailsBinding, GjcWeighingRecordDetailsViewModel>() {

    private lateinit var adapter: CommonAdapter

    // 当前编辑模式（供ViewHolder获取）
    var currentEditMode = false

    override fun layoutId() = R.layout.activity_gjc_weighing_record_details

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

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

        binding.viewModel = viewModel

        // 初始化RecyclerView
        val layoutManager = LinearLayoutManager(this)
        binding.recyclerView.layoutManager = layoutManager

        // 添加分割线
        val divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        binding.recyclerView.addItemDecoration(divider)

        adapter = CommonAdapter(
            this,
            R.layout.item_gjc_check_in_record,
            GjcCheckInRecordViewHolder::class.java
        )
        binding.recyclerView.adapter = adapter

        // 监听数据变化更新RecyclerView
        viewModel.recordList.observe(this) { records ->
            adapter.refresh(records)
        }

        // 监听编辑模式变化，更新当前编辑模式并刷新列表
        viewModel.isEditMode.observe(this) { isEditMode ->
            // 更新当前编辑模式状态
            currentEditMode = isEditMode

            // 刷新所有可见的 ViewHolder
            adapter.notifyDataSetChanged()
        }

        // 初始化数据
        viewModel.initOnCreated(intent)
    }

    // ==================== 手机版专用交互（纯 UI，复用既有 ViewModel 编辑链路） ====================

    /**
     * 手机版：打开「修改记录」覆层编辑指定记录。
     * 复用 [GjcWeighingRecordDetailsViewModel.onModifyClick] 的备份/编辑态逻辑，
     * 覆层显隐由 isEditMode 驱动（平板端该状态驱动列表内联编辑，互不影响）。
     */
    fun openPhoneEdit(record: GjcCheckInRecord) {
        viewModel.onModifyClick()
        binding.editRecord = record
    }

    /** 手机版：取消编辑（恢复备份并收起覆层） */
    fun closePhoneEdit() {
        viewModel.onCancelClick()
    }

    companion object {
        @JvmStatic
        fun start(context: Context, bean: GjcWeighingRecordBean) {
            val starter = Intent(context, GjcWeighingRecordDetailsActivity::class.java)
                .putExtra(Constant.Key.BEAN, bean)
            context.startActivity(starter)
        }
    }
}
