package com.lukouguoji.gjc.page.move

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityIntExpMoveBinding
import com.lukouguoji.gjc.viewModel.IntExpMoveViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.common.Constant
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.addOnItemClickListener
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.router.ARouterConstants

/**
 * 国际出港-出港移库
 */
@Route(path = ARouterConstants.ACTIVITY_URL_INT_EXP_MOVE)
class IntExpMoveActivity : BaseBindingActivity<ActivityIntExpMoveBinding, IntExpMoveViewModel>() {

    override fun layoutId() = R.layout.activity_int_exp_move
    override fun viewModelClass() = IntExpMoveViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("出港移库")
        binding.viewModel = viewModel
        binding.activity = this

        initRecyclerView()
        initListeners()
        observeData()
    }

    /**
     * 初始化RecyclerView
     */
    private fun initRecyclerView() {
        viewModel.pageModel.bindSmartRefreshLayout(
            binding.srl,
            binding.rv,
            viewModel,
            this
        )
        binding.rv.addOnItemClickListener(viewModel)
    }

    /**
     * 初始化监听器
     */
    private fun initListeners() {
        // 移库按钮
        binding.btnMove.setOnClickListener {
            showMoveConfirmDialog()
        }
    }

    /**
     * 观察数据变化
     */
    private fun observeData() {
        // 观察全选状态,更新图标透明度
        viewModel.isAllChecked.observe(this) { isAllChecked ->
            binding.checkIcon.alpha = if (isAllChecked) 1.0f else 0.5f
        }

        // 监听item选择变化事件,更新全选状态
        FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED).observe(this) {
            viewModel.onItemCheckChanged()
        }

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

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

    /**
     * 扫码运单号
     */
    fun scanWaybill() {
        ScanModel.startScan(this, Constant.RequestCode.WAYBILL)
    }

    /**
     * 显示移库确认对话框
     */
    private fun showMoveConfirmDialog() {
        val selectedItems = viewModel.getSelectedItems()

        if (selectedItems.isEmpty()) {
            showToast("请至少选择一条运单")
            return
        }

        AlertDialog.Builder(this)
            .setTitle("移库确认")
            .setMessage("确定要移库选中的 ${selectedItems.size} 条记录吗？")
            .setPositiveButton("确定") { _, _ ->
                viewModel.submitMove()
            }
            .setNegativeButton("取消", null)
            .show()
    }

    /**
     * 扫码回调
     */
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Constant.RequestCode.WAYBILL && resultCode == Activity.RESULT_OK) {
            val code = data?.getStringExtra(Constant.Result.CODED_CONTENT)
            viewModel.waybillNo.value = code
            viewModel.searchClick()
        }
    }

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