package com.casic.common.detector.gd.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.common.detector.gd.bean.MarkerLocalBean
import com.casic.common.detector.gd.databinding.ActivityObjectDetailBinding
import com.casic.common.detector.gd.extensions.appendDownloadUrl
import com.casic.common.detector.gd.utils.DataBaseManager
import com.casic.common.detector.gd.utils.FileType
import com.casic.common.detector.gd.utils.LocaleConstant
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
import com.pengxh.kt.lite.widget.TitleBarView

class ObjectDetailActivity : KotlinBaseActivity<ActivityObjectDetailBinding>() {

    private val kTag = "ObjectDetailActivity"
    private val context: Context = this
    private lateinit var marker: MarkerLocalBean

    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 id = intent.getStringExtra(Constant.INTENT_PARAM_KEY)!!
        try {
            marker = DataBaseManager.get.queryMarkerById(id).first()
            binding.objectTypeView.text =
                LocaleConstant.POINT_TYPE_ARRAY[marker.objectType.toInt() - 1]
            binding.pipelineTypeView.text = marker.pipelineType
            binding.pipelineDiameterView.text = marker.pipelineDiameter
            binding.pipelineMaterialView.text = marker.pipelineMaterial
            binding.buryMethodView.text = marker.buryMethod
            binding.buryDeepView.text = marker.buryDepth
            binding.areaView.text = marker.area
            binding.lineView.text = marker.line
            binding.roadView.text = marker.road
            binding.constructTimeView.text = marker.constructTime
            binding.ownerView.text = marker.owner
            binding.identifierIdView.text = marker.markerId
            binding.identifierTypeView.text = marker.markerType
            binding.identifierDeepView.text = marker.markerDepth
            binding.personView.text = marker.installationDept
            binding.installTimeView.text = marker.updateTime
            binding.remarkView.text = marker.remark
        } catch (e: NoSuchElementException) {
            e.printStackTrace()
        }
    }

    override fun initEvent() {
        binding.titleView.setOnClickListener(object : TitleBarView.OnClickListener {
            override fun onLeftClick() {

            }

            override fun onRightClick() {
                finish()
            }
        })

        binding.showImageView.setOnClickListener {
            //查数据库
            if (marker.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 = marker.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)
            }
        }
    }
}