package com.casic.detector.view

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.casic.detector.R
import com.casic.detector.bean.LabelBean
import com.casic.detector.utils.LocaleConstant
import com.casic.detector.vm.TaskViewModel
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.pengxh.kt.lite.extensions.getScreenHeight
import com.pengxh.kt.lite.extensions.getScreenWidth
import com.pengxh.kt.lite.utils.Constant
import kotlinx.android.synthetic.main.activity_object_detail.*

class ObjectDetailActivity : AppCompatActivity() {

    private val gson by lazy { Gson() }
    private lateinit var taskViewModel: TaskViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_object_detail)

        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)!!
        val labelBean = gson.fromJson<LabelBean>(
            objectJson, object : TypeToken<LabelBean>() {}.type
        )

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

        closeView.setOnClickListener { finish() }

        //TODO 图片接口异常
        taskViewModel = ViewModelProvider(this)[TaskViewModel::class.java]
        taskViewModel.markerImageResult.observe(this) {

        }
        showImageView.setOnClickListener {
            taskViewModel.getLabelPicture(labelBean.id.toString())
        }
    }
}