package com.casic.electric.detector.view

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import com.casic.electric.detector.R
import com.casic.electric.detector.bean.SmallLabelBean
import com.casic.electric.detector.extensions.appendDownloadUrl
import com.casic.electric.detector.utils.DataBaseManager
import com.casic.electric.detector.utils.FileType
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 kotlinx.android.synthetic.main.activity_electric_marker_detail.*

class ElectricMarkerDetailActivity : KotlinBaseActivity() {

    private lateinit var smallLabel: SmallLabelBean

    override fun initData(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 electricMarkerId = intent.getStringExtra(Constant.INTENT_PARAM)!!

        smallLabel = DataBaseManager.get.queryElectricMarkerById(electricMarkerId)

        markerIdView.text = smallLabel.markerId
        locationNumberView.text = smallLabel.locationNumber
        locationNameView.text = smallLabel.locationName
        electricMarkerIdView.text = smallLabel.electricMarkerId
        cableNameView.text = smallLabel.cableName
        cableNumberView.text = smallLabel.cableNumber
        lineView.text = smallLabel.line
        voltageLevelView.text = smallLabel.voltageLevel
        cableTypeView.text = smallLabel.cableType
        cableSpecView.text = smallLabel.cableSpec
        cableBrandView.text = smallLabel.cableBrand
        cableStartView.text = smallLabel.cableStart
        cableEndView.text = smallLabel.cableEnd
        hasJointView.text = smallLabel.hasJoint
        jointCreatorView.text = smallLabel.jointCreator
        memo1View.text = smallLabel.memo1
        memo2View.text = smallLabel.memo2
        memo3View.text = smallLabel.memo3
    }

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

        showImageView.setOnClickListener {
            //查数据库
            if (smallLabel.imagePath.isNullOrEmpty()) {
                "此标识器无图片".show(this)
            } 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 = smallLabel.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)
            }
        }
    }

    override fun initLayoutView(): Int = R.layout.activity_electric_marker_detail

    override fun observeRequestState() {

    }

    override fun setupTopBarLayout() {

    }
}