package com.lukouguoji.gjj.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.widget.Toolbar
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.ActivityIntImpQueryDetailsBinding
import com.lukouguoji.gjj.viewModel.IntImpQueryDetailsViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.base.CustomVP2Adapter
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.DeviceUtil

/**
 * 国际进港查询详情页面
 */
@Route(path = ARouterConstants.ACTIVITY_URL_GJJ_QUERY_INFO)
class IntImpQueryDetailsActivity :
    BaseBindingActivity<ActivityIntImpQueryDetailsBinding, IntImpQueryDetailsViewModel>() {

    override fun layoutId() = R.layout.activity_int_imp_query_details
    override fun viewModelClass() = IntImpQueryDetailsViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow(if (DeviceUtil.isPhone()) "进港货物" else "国际进港查询详情")
        binding.viewModel = viewModel

        // 初始化ViewModel(传入maWbId)
        viewModel.initOnCreated(intent)

        // 配置ViewPager2
        binding.vp.adapter = CustomVP2Adapter(
            viewModel.fragmentList,
            supportFragmentManager,
            lifecycle
        )
        binding.vp.isUserInputEnabled = false  // 禁用滑动
        binding.vp.offscreenPageLimit = 3      // 预加载3个Fragment

        // 监听Tab索引变化,切换Fragment
        viewModel.currentTab.observe(this) {
            binding.vp.setCurrentItem(it, false)
        }

        // 标题栏右侧添加日志查询图标
        val toolbar = findViewById<Toolbar>(com.lukouguoji.module_base.R.id.toolbar)
        val logIcon = ImageView(this).apply {
            setImageResource(com.lukouguoji.module_base.R.drawable.img_log)
            layoutParams = Toolbar.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                gravity = Gravity.END or Gravity.CENTER_VERTICAL
                marginEnd = 15
                width = 30.dp
                height = 30.dp
            }
            setOnClickListener {
                val wbNo = viewModel.maWbData.value?.get("wbNo") as? String ?: ""
                ARouter.getInstance()
                    .build(ARouterConstants.ACTIVITY_URL_LOG_DETAIL)
                    .withString(Constant.Key.KEY, wbNo)
                    .withString(Constant.Key.AWB_TYPE, "II")
                    .navigation()
            }
        }
        toolbar.addView(logIcon)

        // 加载详情数据
        viewModel.loadDetails()
    }

    private val Int.dp: Int
        get() = (this * resources!!.displayMetrics.density).toInt()

    companion object {
        @JvmStatic
        fun start(context: Context, prefix: String?, no: String?) {
            val starter = Intent(context, IntImpQueryDetailsActivity::class.java)
                .putExtra("prefix", prefix ?: "")
                .putExtra("no", no ?: "")
            context.startActivity(starter)
        }
    }
}
