package com.lukouguoji.gjc.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityGjcInspectionDetailsBinding
import com.lukouguoji.gjc.viewModel.GjcInspectionDetailsViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants

/**
 * 国际出港收运检查详情页
 */
@Route(path = ARouterConstants.ACTIVITY_URL_GJC_INSPECTION_DETAILS)
class GjcInspectionDetailsActivity :
    BaseBindingActivity<ActivityGjcInspectionDetailsBinding, GjcInspectionDetailsViewModel>() {

    override fun layoutId() = R.layout.activity_gjc_inspection_details

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

    override fun initOnCreate(savedInstanceState: Bundle?) {
        // 自定义Toolbar设置
        val toolbar: androidx.appcompat.widget.Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(false)
        supportActionBar?.setDisplayShowTitleEnabled(false)

        // 返回按钮点击事件
        findViewById<android.view.View>(R.id.tool_back).setOnClickListener {
            finish()
        }

        // 右侧文档按钮点击事件
        findViewById<android.widget.ImageView>(R.id.ivDocument).setOnClickListener {
            val maWbId = viewModel.dataBean.value?.maWbId ?: 0L
            GjcHandoverActivity.start(this, maWbId)
        }

        binding.viewModel = viewModel

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

        // 监听数据变化，动态创建附件列表
        viewModel.dataBean.observe(this) { bean ->
            binding.attachContainer.removeAllViews()

            val attachList = bean.attachList
            if (attachList.isNullOrEmpty()) {
                // 无附件时显示提示文字
                val textView = TextView(this).apply {
                    text = "无附件"
                    textSize = 14f
                    setTextColor(resources.getColor(R.color.text_gray_l, null))
                    setPadding(0, 8.dp, 0, 8.dp)
                }
                binding.attachContainer.addView(textView)
            } else {
                // 有附件时动态创建TextView
                attachList.forEachIndexed { index, attach ->
                    val textView = TextView(this).apply {
                        text = attach.name
                        textSize = 14f
                        setTextColor(resources.getColor(R.color.text_blue, null))
                        gravity = Gravity.CENTER_VERTICAL
                        setPadding(0, 8.dp, 16.dp, 8.dp)
                        setOnClickListener {
                            viewModel.onAttachClick(attach)
                        }
                    }
                    binding.attachContainer.addView(textView)
                }
            }
        }
    }

    // 扩展属性：dp转px
    private val Int.dp: Int
        get() = (this * (resources?.displayMetrics?.density ?: 3f)).toInt()


    companion object {
        @JvmStatic
        fun start(context: Context, maWbId: Long) {
            val starter = Intent(context, GjcInspectionDetailsActivity::class.java)
                .putExtra(Constant.Key.MAWB_ID, maWbId)
            context.startActivity(starter)
        }
    }
}
