package com.casic.detector.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import com.casic.detector.bean.LabelBean
import com.casic.detector.databinding.ActivityObjectDetailBinding
import com.casic.detector.extensions.appendDownloadUrl
import com.casic.detector.utils.FileType
import com.casic.detector.utils.LocaleConstant
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.getScreenHeight
import com.pengxh.kt.lite.extensions.getScreenWidth
import com.pengxh.kt.lite.extensions.navigatePageTo
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.utils.Constant

class ObjectDetailActivity : KotlinBaseActivity<ActivityObjectDetailBinding>() {

    private val kTag = "ObjectDetailActivity"
    private val context: Context = this@ObjectDetailActivity
    private val gson by lazy { Gson() }
    private lateinit var labelBean: LabelBean

    override fun initViewBinding(): ActivityObjectDetailBinding {
        return ActivityObjectDetailBinding.inflate(layoutInflater)
    }

    override fun setupTopBarLayout() {

    }

    override fun observeRequestState() {

    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window.decorView.setBackgroundColor(Color.TRANSPARENT)
        window.setGravity(Gravity.CENTER)
        val params = window.attributes
        params.width = ((getScreenWidth() * 0.9).toInt())
        params.height = ((getScreenHeight() * 0.7).toInt())
        window.attributes = params

        val objectJson = intent.getStringExtra(Constant.INTENT_PARAM)!!
        try {
            labelBean = gson.fromJson(objectJson, object : TypeToken<LabelBean>() {}.type)

            binding.objectTypeView.text =
                LocaleConstant.POINT_TYPE_ARRAY[labelBean.objectType.toInt() - 1]
            binding.pipelineTypeView.text = labelBean.pipelineType
            binding.pipelineDiameterView.text = labelBean.pipelineDiameter
            binding.pipelineMaterialView.text = labelBean.pipelineMaterial
            binding.buryMethodView.text = labelBean.buryMethod
            binding.buryDeepView.text = labelBean.buryDeep
            binding.areaView.text = labelBean.area
            binding.lineView.text = labelBean.line
            binding.roadView.text = labelBean.road
            binding.constructTimeView.text = labelBean.constructTime
            binding.ownerView.text = labelBean.owner
            binding.identifierIdView.text = labelBean.identifierId
            binding.identifierTypeView.text = labelBean.identifierType
            binding.identifierDeepView.text = labelBean.identifierDeep
            binding.personView.text = labelBean.person
            binding.installTimeView.text = labelBean.installTime
            binding.remarkView.text = labelBean.remark
        } catch (e: NumberFormatException) {
            e.printStackTrace()
        }
    }

    override fun initEvent() {
        binding.closeView.setOnClickListener { finish() }

        binding.showImageView.setOnClickListener {
            //查数据库
            if (labelBean.imagePath.isNullOrEmpty()) {
                "此标识器无图片".show(context)
            } else {
                val realPaths: ArrayList<String> = ArrayList() //真实图片路径

                /**
                 * /images/markers/202307/0008818922_EVENT_20230704_160349_0_1.jpg,/images/markers/202307/0008818922_EVENT_20230704_160406_0_2.jpg
                 * */
                val imagePath = labelBean.imagePath
                if (imagePath.contains(",")) {
                    val list = imagePath.split(",")
                    list.forEach { path ->
                        val url = path.appendDownloadUrl(FileType.IMAGE)
                        realPaths.add(url)
                    }
                } else {
                    //只有一张图片
                    val url = imagePath.appendDownloadUrl(FileType.IMAGE)
                    realPaths.add(url)
                }
                navigatePageTo<BigImageActivity>(0, realPaths)
            }
        }
    }
}